Skip to content

Instantly share code, notes, and snippets.

View drenther's full-sized avatar
🏠
Working from home

Soumyajit Pathak drenther

🏠
Working from home
View GitHub Profile
@drenther
drenther / Factory.js
Created July 1, 2018 15:19
Creational Pattern - Factory
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.`;
};
@drenther
drenther / Prototype.js
Created July 1, 2018 15:22
Creational Pattern - Prototype
// using Object.create as was recommended by ES5 standard
const car = {
noOfWheels: 4,
start() {
return 'started';
},
stop() {
return 'stopped';
},
};
@drenther
drenther / Singleton.js
Created July 1, 2018 15:24
Creational Pattern - Singleton
class Database {
constructor(data) {
if (Database.exists) {
return Database.instance;
}
this._data = data;
Database.instance = this;
Database.exists = true;
return this;
}
@drenther
drenther / Adapter.js
Created July 1, 2018 15:38
Structural Pattern - Adapter
// old interface
class OldCalculator {
constructor() {
this.operations = function(term1, term2, operation) {
switch (operation) {
case 'add':
return term1 + term2;
case 'sub':
return term1 - term2;
default:
@drenther
drenther / Composite.js
Last active November 29, 2018 23:30
Structural Pattern - Composite
class Component {
constructor(name) {
this._name = name;
}
getNodeName() {
return this._name;
}
// abstract methods that need to be overridden
@drenther
drenther / Decorator.js
Created July 1, 2018 17:59
Structural Pattern - Decorator
class Book {
constructor(title, author, price) {
this._title = title;
this._author = author;
this.price = price;
}
getDetails() {
return `${this._title} by ${this._author}`;
}
@drenther
drenther / Facade.js
Created July 1, 2018 18:02
Structural Pattern - Facade
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();
@drenther
drenther / Flyweight.js
Last active November 29, 2018 23:30
Structural Pattern - Flyweight
// flyweight class
class Icecream {
constructor(flavour, price) {
this.flavour = flavour;
this.price = price;
}
}
// factory for flyweight objects
class IcecreamFactory {
@drenther
drenther / Proxy.js
Last active November 29, 2018 23:30
Structural Pattern - Proxy
// 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) {
@drenther
drenther / ChainOfResponsibility.js
Created July 1, 2018 18:52
Behavioural Pattern - Chain of Responsibility
class CumulativeSum {
constructor(intialValue = 0) {
this.sum = intialValue;
}
add(value) {
this.sum += value;
return this;
}
}