This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BallFactory { | |
constructor() { | |
this.createBall = function(type) { | |
let ball; | |
if (type === 'football' || type === 'soccer') ball = new Football(); | |
else if (type === 'basketball') ball = new Basketball(); | |
ball.roll = function() { | |
return `The ${this._type} is rolling.`; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using Object.create as was recommended by ES5 standard | |
const car = { | |
noOfWheels: 4, | |
start() { | |
return 'started'; | |
}, | |
stop() { | |
return 'stopped'; | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database { | |
constructor(data) { | |
if (Database.exists) { | |
return Database.instance; | |
} | |
this._data = data; | |
Database.instance = this; | |
Database.exists = true; | |
return this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// old interface | |
class OldCalculator { | |
constructor() { | |
this.operations = function(term1, term2, operation) { | |
switch (operation) { | |
case 'add': | |
return term1 + term2; | |
case 'sub': | |
return term1 - term2; | |
default: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Component { | |
constructor(name) { | |
this._name = name; | |
} | |
getNodeName() { | |
return this._name; | |
} | |
// abstract methods that need to be overridden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book { | |
constructor(title, author, price) { | |
this._title = title; | |
this._author = author; | |
this.price = price; | |
} | |
getDetails() { | |
return `${this._title} by ${this._author}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let currentId = 0; | |
class ComplaintRegistry { | |
registerComplaint(customer, type, details) { | |
const id = ComplaintRegistry._uniqueIdGenerator(); | |
let registry; | |
if (type === 'service') { | |
registry = new ServiceComplaints(); | |
} else { | |
registry = new ProductComplaints(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// flyweight class | |
class Icecream { | |
constructor(flavour, price) { | |
this.flavour = flavour; | |
this.price = price; | |
} | |
} | |
// factory for flyweight objects | |
class IcecreamFactory { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Target | |
function networkFetch(url) { | |
return `${url} - Response from network`; | |
} | |
// Proxy | |
// ES6 Proxy API = new Proxy(target, handler); | |
const cache = []; | |
const proxiedNetworkFetch = new Proxy(networkFetch, { | |
apply(target, thisArg, args) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CumulativeSum { | |
constructor(intialValue = 0) { | |
this.sum = intialValue; | |
} | |
add(value) { | |
this.sum += value; | |
return this; | |
} | |
} |