Created
February 20, 2021 19:05
-
-
Save nchDev-js/e5745132b26cb3cad3b199cc140d5c10 to your computer and use it in GitHub Desktop.
Design Patterns / BEHAVIORAL
This file contains hidden or 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 Account { | |
| pay(orderPrice) { | |
| if (this.canPay(orderPrice)) { | |
| console.log(`Paid ${orderPrice} using ${this.name}`); | |
| } else if (this.incomer) { | |
| console.log(`Cannot pay using ${this.name}`); | |
| this.incomer.pay(orderPrice); | |
| } else { | |
| console.log("Unfortunately, not enough money"); | |
| } | |
| } | |
| canPay(amount) { | |
| return this.balance >= amount; | |
| } | |
| setNext(account) { | |
| this.incomer = account; | |
| } | |
| } | |
| class Master extends Account { | |
| constructor(balance) { | |
| super(); | |
| this.name = "Master Card"; | |
| this.balance = balance; | |
| } | |
| } | |
| class Paypal extends Account { | |
| constructor(balance) { | |
| super(); | |
| this.name = "Paypal"; | |
| this.balance = balance; | |
| } | |
| } | |
| class Qiwi extends Account { | |
| constructor(balance) { | |
| super(); | |
| this.name = "Qiwi"; | |
| this.balance = balance; | |
| } | |
| } |
This file contains hidden or 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 Driver { | |
| constructor(command) { | |
| this.command = command; | |
| } | |
| execute() { | |
| this.command.execute(); | |
| } | |
| } | |
| class Engine { | |
| constructor() { | |
| this.state = false; | |
| } | |
| on() { | |
| this.state = true; | |
| } | |
| off() { | |
| this.state = false; | |
| } | |
| } | |
| class OnStartCommand { | |
| constructor(engine) { | |
| this.engine = engine; | |
| } | |
| execute() { | |
| this.engine.on(); | |
| } | |
| } | |
| class onSwitchOffCommand { | |
| constructor(engine) { | |
| this.engine = engine; | |
| } | |
| execute() { | |
| this.engine.off(); | |
| } | |
| } |
This file contains hidden or 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 ArrayIterator { | |
| constructor(el) { | |
| this.index = 0; | |
| this.elements = el; | |
| } | |
| next() { | |
| return this.elements[this.index++]; | |
| } | |
| hasNext() { | |
| return this.index < this.elements.length; | |
| } | |
| } | |
| class ObjectIterator { | |
| constructor(el) { | |
| this.index = 0; | |
| (this.keys = Object.keys(el)), (this.elements = el); | |
| } | |
| next() { | |
| return this.elements[this.keys[this.index++]]; | |
| } | |
| hasNext() { | |
| return this.index < this.keys.length; | |
| } | |
| } |
This file contains hidden or 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 OfficialDealer { | |
| constructor() { | |
| this.customers = []; | |
| } | |
| orderAuto(customer, auto, info) { | |
| const name = customer.getName(); | |
| console.log(`Order name: ${name}. Order auto is ${auto}`); | |
| console.log(`Additional info: ${info}`); | |
| this.addToCustomersList(name); | |
| } | |
| addToCustomersList(name) { | |
| this.customers.push(name); | |
| } | |
| getCustomerList() { | |
| return this.customers; | |
| } | |
| }; | |
| class Customer { | |
| constructor(name, dealerMediator) { | |
| this.name = name; | |
| this.dealerMediator = dealerMediator; | |
| } | |
| getName() { | |
| return this.name; | |
| } | |
| makeOrder(auto, info) { | |
| this.dealerMediator.orderAuto(this, auto, info) | |
| } | |
| }; |
This file contains hidden or 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 Memento { | |
| constructor(value) { | |
| this.value = value; | |
| } | |
| } | |
| const creator = { | |
| save: (val) => new Memento(val), | |
| restore: (memento) => memento.value, | |
| }; | |
| class Caretaker { | |
| constructor() { | |
| this.values = []; | |
| } | |
| addMemento(memento) { | |
| this.values.push(memento); | |
| } | |
| getMemento(index) { | |
| return this.values[index]; | |
| } | |
| } |
This file contains hidden or 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 AutoNews { | |
| constructor() { | |
| this.news = ""; | |
| this.actions = []; | |
| } | |
| setNews(text) { | |
| this.news = text; | |
| this.notifyAll(); | |
| } | |
| notifyAll() { | |
| return this.actions.forEach((subs) => subs.inform(this)); | |
| } | |
| register(observer) { | |
| this.actions.push(observer); | |
| } | |
| unregister(observer) { | |
| this.actions = this.actions.filter((el) => !(el instanceof observer)); | |
| } | |
| } | |
| class Jack { | |
| inform(message) { | |
| console.log(`Jack has been informed about: ${message.news}`); | |
| } | |
| } | |
| class Max { | |
| inform(message) { | |
| console.log(`Max has been informed about: ${message.news}`); | |
| } | |
| } |
This file contains hidden or 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 OrderStatus { | |
| constructor(name, nextStatus) { | |
| this.name = name; | |
| this.nextStatus = nextStatus; | |
| } | |
| next() { | |
| return new this.nextStatus(); | |
| } | |
| } | |
| class WaitingForPayment extends OrderStatus { | |
| constructor() { | |
| super("waitingForPayment", Shipping); | |
| } | |
| } | |
| class Shipping extends OrderStatus { | |
| constructor() { | |
| super("shipping", Delivered); | |
| } | |
| } | |
| class Delivered extends OrderStatus { | |
| constructor() { | |
| super("delivered", Delivered); | |
| } | |
| } | |
| class Order { | |
| constructor() { | |
| this.state = new WaitingForPayment(); | |
| } | |
| nextState() { | |
| this.state = this.state.next(); | |
| } | |
| } |
This file contains hidden or 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
| function baseStrategy(amount) { | |
| return amount; | |
| }; | |
| function premiumStrategy(amount) { | |
| return amount * 0.85; | |
| }; | |
| function platinumStrategy(amount) { | |
| return amount * 0.65; | |
| }; | |
| class AutoCart { | |
| constructor(discount) { | |
| this.discount = discount; | |
| this.amount = 0; | |
| } | |
| checkout() { | |
| return this.discount(this.amount); | |
| } | |
| setAmount(amount) { | |
| this.amount = amount; | |
| } | |
| }; |
This file contains hidden or 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 Builder { | |
| build() { | |
| this.addEngine(); | |
| this.installChassis(); | |
| this.addElectronic(); | |
| this.collectAccessories(); | |
| } | |
| }; | |
| class TeslaBuilder extends Builder { | |
| addEngine() { | |
| console.log('Add Electronic Engine'); | |
| } | |
| installChassis() { | |
| console.log('Install Tesla chassis'); | |
| } | |
| addElectronic() { | |
| console.log('Add special electronic'); | |
| } | |
| collectAccessories() { | |
| console.log('Collect Accessories'); | |
| } | |
| } | |
| class BmwBuilder extends Builder { | |
| addEngine() { | |
| console.log('Add BMW Engine'); | |
| } | |
| installChassis() { | |
| console.log('Install BMW chassis'); | |
| } | |
| addElectronic() { | |
| console.log('Add electronic'); | |
| } | |
| collectAccessories() { | |
| console.log('Collect Accessories'); | |
| } | |
| } |
This file contains hidden or 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 Auto { | |
| accept(visitor) { | |
| visitor(this); | |
| } | |
| } | |
| class Tesla extends Auto { | |
| info() { | |
| return "It is a Tesla car!"; | |
| } | |
| } | |
| class Bmw extends Auto { | |
| info() { | |
| return "It is a BMW car!"; | |
| } | |
| } | |
| class Audi extends Auto { | |
| info() { | |
| return "It is an Audi car!"; | |
| } | |
| } | |
| function exportVisitor(auto) { | |
| if (auto instanceof Tesla) | |
| auto.export = console.log(`Exported data: ${auto.info()}`); | |
| if (auto instanceof Bmw) | |
| auto.export = console.log(`Exported data: ${auto.info()}`); | |
| if (auto instanceof Audi) | |
| auto.export = console.log(`Exported data: ${auto.info()}`); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment