├── LICENSE ├── README.md ├── docs └── Factory.md └── src ├── AbstractFactory.js ├── Adapter.js ├── Bridge.js ├── Builder.js ├── Chain of Responsibility.js ├── Command.js ├── Composite.js ├── Decorator.js ├── Facade.js ├── Factory.js ├── Flyweight.js ├── Interpreter.js ├── Iterator.js ├── Mediator.js ├── Memento.js ├── Observer.js ├── Prototype.js ├── Proxy.js ├── Signleton.js ├── State.js ├── Strategy.js ├── Template Method.js └── Visitor.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Ya Hui Liang(Ryou) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ECMAScript-Design-Patterns 2 | 使用es6实现的设计模式 3 | 4 | # Environment 5 | Nodejs v6.3.0+ 6 | 7 | # 设计模式分为三种类型,共24种(沙盒模式暂时没时间写代码)。 8 | - 创建型模式:单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。 9 | - 结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式、沙盒模式。 10 | - 行为型模式:模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)、访问者模式。 11 | 12 | ## 创建型模式: 13 | - [Signleton](src/Signleton.js)(单例模式):保证一个类仅有一个实例,并提供一个访问它的全局访问点。 14 | - [AbstractFactory](src/AbstractFactory.js)(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 15 | - [Builder](src/Builder.js)(建造者模式):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。 16 | - [Factory](src/Factory.js)(工厂模式):定义一个用于创建对象的接口,让子类决定将哪一个类实例化。Factory Method使一个类的实例化延迟到其子类。 17 | - [Prototype](src/Prototype.js)(原型模式):用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。 18 | 19 | ## 结构型模式: 20 | - [Adapter](src/Adapter.js)(适配器模式):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 21 | - [Bridge](src/Bridge.js)(桥接模式):将抽象部分与它的实现部分分离,使它们都可以独立地变化。 22 | - [Composite](src/Composite.js)(组合模式):将对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。 23 | - [Decorator](src/Decorator.js)(装饰模式):动态地给一个对象添加一些额外的职责。就扩展功能而言, 它比生成子类方式更为灵活。 24 | - [Facade](src/Facade.js)(外观模式):为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。 25 | - [Flyweight](src/Flyweight.js)(享元模式):运用共享技术有效地支持大量细粒度的对象。 26 | - [Proxy](src/Proxy.js)(代理模式):为其他对象提供一个代理以控制对这个对象的访问。 27 | - [Sandbox]() (沙盒模式):这是在JS技术中非常重要的一种设计模式,在早期的插件以及前端的封装中起到决定性的作用,我们可以通过沙盒模式,对内部的数据进行封装与保护,只能通过外部的接口提供的有限功能才能对这些数据进行访问和操作。 28 | 29 | ## 行为型模式: 30 | - [Chain of Responsibility](src/Chain%20of%20Responsibility.js)(责任链模式):为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。 31 | - [Command](src/Command.js)(命令模式):将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。 32 | - [Interpreter](src/Interpreter.js)(解析器模式):给定一个语言, 定义它的文法的一种表示,并定义一个解释器, 该解释器使用该表示来解释语言中的句子。 33 | - [Iterator](src/Iterator.js)(迭代器模式):提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。 34 | - [Mediator](src/Mediator.js)(中介模式):用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。 35 | - [Memento](src/Memento.js)(备忘录模式):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到保存的状态。 36 | - [Observer](src/Observer.js)(观察者模式):定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。 37 | - [State](src/State.js)(状态模式):允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类。 38 | - [Strategy](src/Strategy.js)(策略模式):定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。本模式使得算法的变化可独立于使用它的客户。 39 | - [Template Method](src/Template%20Method.js)(模板方法模式):定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 40 | - [Visitor](src/Visitor.js)(访问者模式):表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。 41 | 42 | # LICENCE 43 | MIT 44 | -------------------------------------------------------------------------------- /docs/Factory.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/AbstractFactory.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class AbstractFactory { 4 | constructor() { 5 | console.log("AbstractFactory class created"); 6 | } 7 | 8 | createProductA(product) { 9 | console.log("AbstractFactory.createProductA created"); 10 | } 11 | 12 | createProductB(product) { 13 | console.log("AbstractFactory.createProductB created"); 14 | } 15 | } 16 | 17 | class ConcreteFactory1 extends AbstractFactory { 18 | constructor() { 19 | super(); 20 | console.log("ConcreteFactory1 class created"); 21 | } 22 | 23 | createProductA(product) { 24 | console.log('ConcreteFactory1 createProductA'); 25 | return new ProductA1(); 26 | } 27 | 28 | createProductB(product) { 29 | console.log('ConcreteFactory1 createProductB'); 30 | return new ProductB1(); 31 | } 32 | } 33 | 34 | class ConcreteFactory2 extends AbstractFactory { 35 | constructor() { 36 | super(); 37 | console.log("ConcreteFactory2 class created"); 38 | } 39 | 40 | createProductA(product) { 41 | console.log('ConcreteFactory2 createProductA'); 42 | return new ProductA2(); 43 | } 44 | 45 | createProductB(product) { 46 | console.log('ConcreteFactory2 createProductB'); 47 | return new ProductB2(); 48 | } 49 | } 50 | 51 | class AbstractProductA { 52 | constructor() { 53 | console.log('AbstractProductA class created'); 54 | } 55 | } 56 | 57 | class AbstractProductB { 58 | constructor() { 59 | console.log('AbstractProductB class created'); 60 | } 61 | } 62 | 63 | class ProductA1 extends AbstractProductA { 64 | constructor() { 65 | super(); 66 | console.log('ProductA1 class created'); 67 | } 68 | } 69 | 70 | class ProductA2 extends AbstractProductA { 71 | constructor() { 72 | super(); 73 | console.log('ProductA2 class created'); 74 | } 75 | } 76 | 77 | class ProductB1 extends AbstractProductB { 78 | constructor() { 79 | super(); 80 | console.log('ProductB1 class created'); 81 | } 82 | } 83 | 84 | class ProductB2 extends AbstractProductB { 85 | constructor() { 86 | super(); 87 | console.log('ProductB2 class created'); 88 | } 89 | } 90 | 91 | var factory1 = new ConcreteFactory1(); 92 | var productB1 = factory1.createProductB(); 93 | var productA1 = factory1.createProductA(); 94 | 95 | var factory2 = new ConcreteFactory2(); 96 | var productA2 = factory2.createProductA(); 97 | var productB2 = factory2.createProductB(); -------------------------------------------------------------------------------- /src/Adapter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Target { 4 | constructor(type) { 5 | console.log('Target Class created!'); 6 | let result = undefined; 7 | 8 | switch (type) { 9 | case 'adapter': 10 | result = new AdapterImpl(); 11 | break 12 | default: 13 | result = undefined; 14 | } 15 | return result; 16 | } 17 | 18 | request() { 19 | console.log('Target.request invoked'); 20 | } 21 | } 22 | 23 | class Adapter { 24 | constructor() { 25 | console.log('Adapter Class created'); 26 | } 27 | 28 | specificRequest() { 29 | console.log('Adapter.specificRequest invoked'); 30 | } 31 | } 32 | 33 | class AdapterImpl extends Adapter { 34 | constructor() { 35 | super() 36 | console.log('AdapterImpl Class created'); 37 | } 38 | 39 | request() { 40 | console.log('AdapterImpl.request invoked'); 41 | return this.specificRequest(); 42 | } 43 | } 44 | 45 | var f = new Target("adapter"); 46 | f.request(); 47 | -------------------------------------------------------------------------------- /src/Bridge.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Abstraction { 4 | constructor() { 5 | console.log('Abstraction Class created'); 6 | } 7 | 8 | operation() { 9 | console.log('Abstraction.operation invoked'); 10 | this.imp.operationImp(); 11 | } 12 | } 13 | 14 | class RefinedAbstraction extends Abstraction { 15 | constructor() { 16 | super() 17 | console.log('RefinedAbstraction Class created'); 18 | } 19 | 20 | setImp(imp) { 21 | console.log('RefinedAbstraction.setImp invoked'); 22 | this.imp = imp 23 | } 24 | } 25 | 26 | class Implementor { 27 | constructor() { 28 | console.log('Implementor Class created'); 29 | } 30 | 31 | operationImp() { 32 | console.log('Implementor.operationImp invoked'); 33 | } 34 | } 35 | 36 | class ConcreteImplementorA extends Implementor { 37 | constructor() { 38 | super() 39 | console.log('ConcreteImplementorA Class created'); 40 | } 41 | 42 | operationImp() { 43 | console.log('ConcreteImplementorA.operationImp invoked'); 44 | } 45 | } 46 | 47 | class ConcreteImplementorB extends Implementor { 48 | constructor() { 49 | super() 50 | console.log('ConcreteImplementorB Class created'); 51 | } 52 | 53 | operationImp() { 54 | console.log('ConcreteImplementorB.operationImp invoked'); 55 | } 56 | } 57 | 58 | var abstraction = new RefinedAbstraction(); 59 | abstraction.setImp(new ConcreteImplementorA()); 60 | abstraction.operation(); 61 | abstraction.setImp(new ConcreteImplementorB()); 62 | abstraction.operation(); -------------------------------------------------------------------------------- /src/Builder.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Builder { 4 | constructor() { 5 | console.log('Builder Class created!'); 6 | } 7 | 8 | buildPart(partName) { 9 | console.log('Builder.buildPart invoked!'); 10 | } 11 | } 12 | 13 | class ConcreteBuilder extends Builder { 14 | constructor() { 15 | super(); 16 | console.log('ConcreteBuilder Class created!'); 17 | } 18 | 19 | buildPart(partName) { 20 | super.buildPart(partName); 21 | console.log('ConcreteBuilder.buildPart invoked!'); 22 | this.product = new Product(partName); 23 | } 24 | getResult() { 25 | console.log('ConcreteBuilder.getResult invoked!'); 26 | return this.product; 27 | } 28 | } 29 | 30 | class Product { 31 | constructor(material) { 32 | console.log("Product class created"); 33 | this.data = material 34 | } 35 | } 36 | 37 | class Director { 38 | constructor() { 39 | this.structure = ['Prod1', 'Prod2', 'Prod3']; 40 | console.log("Director class created"); 41 | } 42 | 43 | construct() { 44 | console.log("Director.Construct created"); 45 | for (var prodName in this.structure) { 46 | let builder = new ConcreteBuilder(); 47 | builder.buildPart(this.structure[prodName]); 48 | builder.getResult(); 49 | } 50 | } 51 | } 52 | 53 | let director = new Director(); 54 | director.construct(); -------------------------------------------------------------------------------- /src/Chain of Responsibility.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | class Handler { 3 | constructor() { 4 | console.log('Handler Class created'); 5 | } 6 | 7 | handleRequest() { 8 | console.log('Handler.handleRequest invoked'); 9 | } 10 | } 11 | 12 | class ConcreteHandler1 extends Handler { 13 | constructor() { 14 | super(); 15 | console.log('ConcreteHandler1 Class created'); 16 | } 17 | 18 | setSuccessor(successor) { 19 | this.successor = successor; 20 | console.log('ConcreteHandler1.setSuccessor invoked'); 21 | } 22 | 23 | handleRequest(request) { 24 | console.log('ConcreteHandler1.handleRequest invoked'); 25 | if (request === 'run') 26 | console.log('ConcreteHandler1 has handled the request'); 27 | else { 28 | console.log('ConcreteHandler1 calls his successor'); 29 | this.successor.handleRequest(request); 30 | } 31 | } 32 | } 33 | 34 | class ConcreteHandler2 extends Handler { 35 | constructor() { 36 | super(); 37 | console.log('ConcreteHandler2 Class created'); 38 | } 39 | 40 | handleRequest(request) { 41 | console.log('ConcreteHandler2.handleRequest invoked'); 42 | } 43 | } 44 | 45 | let handle1 = new ConcreteHandler1(); 46 | let handle2 = new ConcreteHandler2(); 47 | handle1.setSuccessor(handle2); 48 | handle1.handleRequest('run'); 49 | handle1.handleRequest('stay'); -------------------------------------------------------------------------------- /src/Command.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Invoker { 4 | constructor() { 5 | console.log('Invoker Class created'); 6 | } 7 | 8 | storeCommand(command) { 9 | this.command = command; 10 | console.log('Invoker.storeCommand invoked'); 11 | } 12 | } 13 | 14 | class Command { 15 | constructor() { 16 | console.log('Command Class created'); 17 | } 18 | 19 | execute() { 20 | console.log('Command.execute invoked'); 21 | } 22 | } 23 | 24 | class ConcreteCommand extends Command { 25 | constructor(receiver, state) { 26 | super(); 27 | this.receiver = receiver; 28 | console.log('ConcreteCommand Class created'); 29 | } 30 | 31 | execute() { 32 | console.log('ConcreteCommand.execute invoked'); 33 | this.receiver.action(); 34 | } 35 | } 36 | 37 | class Receiver { 38 | constructor() { 39 | console.log('Receiver Class created'); 40 | } 41 | 42 | action() { 43 | console.log('Receiver.action invoked'); 44 | } 45 | } 46 | 47 | var invoker = new Invoker(); 48 | var receiver = new Receiver(); 49 | var command = new ConcreteCommand(receiver); 50 | invoker.storeCommand(command); 51 | invoker.command.execute(); -------------------------------------------------------------------------------- /src/Composite.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Component { 4 | constructor() { 5 | console.log('Component Class created'); 6 | } 7 | 8 | operation() { 9 | console.log('Component.operation invoked'); 10 | } 11 | 12 | add(Component) { 13 | console.log('Component.add invoked'); 14 | } 15 | 16 | remove(Component) { 17 | console.log('Component.remove invoked'); 18 | } 19 | 20 | getChild(key) { 21 | console.log('Component.getChild invoked'); 22 | } 23 | } 24 | 25 | class Leaf extends Component { 26 | constructor(name) { 27 | super(); 28 | this.name = name; 29 | console.log('Leaf Class created'); 30 | } 31 | 32 | operation() { 33 | console.log('Leaf.operation invoked'); 34 | console.log(this.name); 35 | } 36 | } 37 | 38 | class Composite extends Component { 39 | constructor(name) { 40 | super(); 41 | this.name = name; 42 | this.children = []; 43 | console.log('Composite Class created'); 44 | } 45 | 46 | operation() { 47 | console.log('Composite operation for: ' + this.name) 48 | for (var i in this.children) { 49 | this.children[i].operation(); 50 | } 51 | } 52 | 53 | add(Component) { 54 | console.log('Composite.add invoked'); 55 | this.children.push(Component); 56 | } 57 | 58 | remove(Component) { 59 | console.log('Composite.remove invoked'); 60 | for (var i in this.children) { 61 | if (this.children[i] === Component) { 62 | this.children.splice(i, 1); 63 | } 64 | } 65 | } 66 | 67 | getChild(key) { 68 | console.log('Composite.getChild invoked'); 69 | return this.children[key]; 70 | } 71 | } 72 | 73 | var composite1 = new Composite('C1'); 74 | composite1.add(new Leaf('L1')); 75 | composite1.add(new Leaf('L2')); 76 | var composite2 = new Composite('C2'); 77 | composite2.add(composite1); 78 | composite1.getChild(1).operation(); 79 | composite2.operation(); -------------------------------------------------------------------------------- /src/Decorator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Component { 4 | constructor() { 5 | console.log('Component Class created'); 6 | } 7 | 8 | operation() { 9 | console.log('Component.operation invoked'); 10 | } 11 | } 12 | 13 | class ConcreteComponent extends Component { 14 | constructor() { 15 | super(); 16 | console.log('ConcreteComponent Class created'); 17 | } 18 | 19 | operation() { 20 | console.log('ConcreteComponent.operation invoked'); 21 | } 22 | } 23 | 24 | class Decorator extends Component { 25 | constructor(component) { 26 | super(); 27 | this.component = component; 28 | console.log('Decorator Class created'); 29 | } 30 | 31 | operation() { 32 | console.log('Decorator.operation invoked'); 33 | this.component.operation() 34 | } 35 | } 36 | 37 | class ConcreteDecoratorA extends Decorator { 38 | constructor(component, sign) { 39 | super(component); 40 | this.addedState = sign; 41 | console.log('ConcreteDecoratorA Class created'); 42 | } 43 | 44 | operation() { 45 | super.operation(); 46 | console.log('ConcreteDecoratorA.operation invoked'); 47 | console.log(this.addedState) 48 | } 49 | } 50 | 51 | class ConcreteDecoratorB extends Decorator { 52 | constructor(component, sign) { 53 | super(component); 54 | this.addedState = sign; 55 | console.log('ConcreteDecoratorB Class created'); 56 | } 57 | 58 | operation() { 59 | super.operation(); 60 | console.log('ConcreteDecoratorB.operation invoked'); 61 | console.log(this.addedState + this.addedState + this.addedState + this.addedState + this.addedState); 62 | } 63 | 64 | addedBehavior() { 65 | this.operation(); 66 | console.log('ConcreteDecoratorB.operation invoked'); 67 | } 68 | } 69 | 70 | var component = new ConcreteComponent(); 71 | var decoratorA = new ConcreteDecoratorA(component, 'decoratorA'); 72 | var decoratorB = new ConcreteDecoratorB(component, 'decoratorB'); 73 | console.log('component: '); 74 | component.operation(); 75 | console.log('decoratorA: '); 76 | decoratorA.operation(); 77 | console.log('decoratorB: '); 78 | decoratorB.addedBehavior(); -------------------------------------------------------------------------------- /src/Facade.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Facade { 4 | constructor() { 5 | console.log("Facade class created"); 6 | } 7 | 8 | gotoPage(dp) { 9 | switch (dp) { 10 | case "Facade": 11 | console.log("This is the Facade"); 12 | break; 13 | case "AbstractFactory": 14 | console.log("This is the AbstractFactory"); 15 | break; 16 | case "Builder": 17 | console.log("This is the Builder"); 18 | break; 19 | case "Factory": 20 | console.log("This is the Factory"); 21 | break; 22 | case "Prototype": 23 | console.log("This is the Prototype"); 24 | break; 25 | case "Singleton": 26 | console.log("This is the Singleton"); 27 | break; 28 | case "Adapter": 29 | console.log("This is the Adapter"); 30 | break; 31 | case "Bridge": 32 | console.log("This is the Bridge"); 33 | break; 34 | case "Composite": 35 | console.log("This is the Composite"); 36 | break; 37 | case "Decorator": 38 | console.log("This is the Decorator"); 39 | break; 40 | case "Flyweight": 41 | console.log("This is the Flyweight"); 42 | break; 43 | case "Proxy": 44 | console.log("This is the Proxy"); 45 | break; 46 | case "ChainofResponsibility": 47 | console.log("This is the ChainofResponsibility"); 48 | break; 49 | case "Command": 50 | console.log("This is the Command"); 51 | break; 52 | case "Interpreter": 53 | console.log("This is the Interpreter"); 54 | break; 55 | case "Iterator": 56 | console.log("This is the Iterator"); 57 | break; 58 | case "Mediator": 59 | console.log("This is the Mediator"); 60 | break; 61 | case "Memento": 62 | console.log("This is the Memento"); 63 | break; 64 | case "Observer": 65 | console.log("This is the Observer"); 66 | break; 67 | case "State": 68 | console.log("This is the State"); 69 | break; 70 | case "Strategy": 71 | console.log("This is the Strategy"); 72 | break; 73 | case "TemplateMethod": 74 | console.log("This is the TemplateMethod"); 75 | break; 76 | case "Visitor": 77 | console.log("This is the Visitor"); 78 | break; 79 | default: 80 | console.log("nothing to be matched"); 81 | } 82 | } 83 | } 84 | 85 | let facade = new Facade(); 86 | facade.gotoPage('Facade'); 87 | facade.gotoPage('AbstractFactory'); 88 | facade.gotoPage('Builder'); 89 | facade.gotoPage('Factory'); 90 | facade.gotoPage('Prototype'); 91 | facade.gotoPage('Singleton'); 92 | facade.gotoPage('Adapter'); 93 | facade.gotoPage('Bridge'); 94 | facade.gotoPage('Composite'); 95 | facade.gotoPage('Decorator'); 96 | facade.gotoPage('Flyweight'); 97 | facade.gotoPage('Proxy'); 98 | facade.gotoPage('ChainofResponsibility'); 99 | facade.gotoPage('Command'); 100 | facade.gotoPage('Interpreter'); 101 | facade.gotoPage('Iterator'); 102 | facade.gotoPage('Mediator'); 103 | facade.gotoPage('Memento'); 104 | facade.gotoPage('Observer'); 105 | facade.gotoPage('State'); 106 | facade.gotoPage('Strategy'); 107 | facade.gotoPage('TemplateMethod'); 108 | facade.gotoPage('Visitor'); -------------------------------------------------------------------------------- /src/Factory.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Product { 4 | constructor() { 5 | console.log('Product Class created'); 6 | } 7 | } 8 | 9 | class ConcreteProduct extends Product { 10 | constructor() { 11 | super(); 12 | console.log('ConcreteProduct Class created'); 13 | } 14 | } 15 | 16 | class Creator { 17 | constructor() { 18 | console.log('Creator Class created'); 19 | } 20 | 21 | factoryMethod() { 22 | console.log('Creator.factoryMethod created'); 23 | } 24 | 25 | anOperation() { 26 | console.log('Creator.anOperation created'); 27 | this.product = this.factoryMethod(); 28 | console.log(this.product instanceof ConcreteProduct); 29 | } 30 | } 31 | 32 | class ConcreteCreator extends Creator { 33 | 34 | constructor() { 35 | super(); 36 | console.log('ConcreteCreator Class created'); 37 | } 38 | 39 | factoryMethod() { 40 | return new ConcreteProduct(); 41 | } 42 | } 43 | 44 | var factory = new ConcreteCreator(); 45 | factory.anOperation(); 46 | -------------------------------------------------------------------------------- /src/Flyweight.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class FlyweightFactory { 4 | constructor() { 5 | this.flyweights = {}; 6 | console.log('FlyweightFactory Class created'); 7 | } 8 | 9 | getFlyweight(key) { 10 | console.log('FlyweightFactory.getFlyweight invoked'); 11 | if (this.flyweights[key]) { 12 | return this.flyweights[key]; 13 | } else { 14 | this.flyweights[key] = new ConcreteFlyweight(key); 15 | return this.flyweights[key]; 16 | } 17 | } 18 | 19 | createGibberish(keys) { 20 | console.log('FlyweightFactory.createGibberish invoked'); 21 | return new UnsharedConcreteFlyweight(keys, this); 22 | } 23 | } 24 | 25 | class Flyweight { 26 | constructor() { 27 | console.log('Flyweight Class created'); 28 | } 29 | 30 | operation(extrinsicState) { 31 | console.log('Flyweight.operation invoked'); 32 | } 33 | } 34 | 35 | class ConcreteFlyweight extends Flyweight { 36 | constructor(key) { 37 | super(); 38 | this.intrinsicState = key; 39 | console.log('ConcreteFlyweight Class created'); 40 | } 41 | 42 | operation(extrinsicState) { 43 | console.log('ConcreteFlyweight.operation invoked'); 44 | return extrinsicState + this.intrinsicState; 45 | } 46 | } 47 | 48 | class UnsharedConcreteFlyweight extends Flyweight { 49 | constructor(keys, flyweights) { 50 | super(); 51 | this.flyweights = flyweights; 52 | this.keys = keys; 53 | console.log('UnsharedConcreteFlyweight Class created'); 54 | } 55 | 56 | operation(extrinsicState) { 57 | console.log('UnsharedConcreteFlyweight.operation invoked'); 58 | var key, word = ''; 59 | for (var i = 0; i < extrinsicState; i++) { 60 | key = this.keys[Math.floor(Math.random() * (this.keys.length))]; 61 | word = this.flyweights.getFlyweight(key).operation(word); 62 | } 63 | console.log('UnsharedConcreteFlyweight Operation: '); 64 | console.log(word); 65 | } 66 | } 67 | 68 | 69 | var flyweights = new FlyweightFactory(); 70 | var gibberish = flyweights.createGibberish(['-', '+', '*']); 71 | gibberish.operation(5); -------------------------------------------------------------------------------- /src/Interpreter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Context { 4 | constructor(input) { 5 | this.sum = 0; 6 | this.list = []; 7 | console.log('Context Class created'); 8 | } 9 | 10 | add(eps) { 11 | console.log('Context.add invoked'); 12 | this.list.push(eps); 13 | } 14 | 15 | getList() { 16 | console.log('Context.getList invoked'); 17 | return this.list; 18 | } 19 | 20 | getSum() { 21 | console.log('Context.getSum invoked'); 22 | return this.sum; 23 | } 24 | 25 | setSum(_sum) { 26 | this.sum = _sum; 27 | console.log('Context.setSum invoked'); 28 | } 29 | } 30 | 31 | class AbstractExpression { 32 | constructor() { 33 | console.log('AbstractExpression Class created'); 34 | } 35 | 36 | interpret(context) { 37 | console.log('AbstractExpression.interpret invoked'); 38 | } 39 | } 40 | 41 | class PlusExpression extends AbstractExpression { 42 | constructor(name) { 43 | super(); 44 | this.name = name; 45 | console.log('PlusExpression Class created'); 46 | } 47 | 48 | interpret(context) { 49 | console.log('PlusExpression.interpret invoked'); 50 | var sum = context.getSum(); 51 | sum++; 52 | context.setSum(sum); 53 | } 54 | } 55 | 56 | class MinusExpression extends AbstractExpression { 57 | constructor() { 58 | super(); 59 | this.name = '+'; 60 | console.log('MinusExpression Class created'); 61 | } 62 | 63 | interpret(context) { 64 | console.log('MinusExpression.interpret invoked'); 65 | var sum = context.getSum(); 66 | sum--; 67 | context.setSum(sum) 68 | } 69 | } 70 | 71 | var context = new Context(); 72 | context.setSum(20); 73 | 74 | context.add(new PlusExpression()); 75 | context.add(new PlusExpression()); 76 | context.add(new PlusExpression()); 77 | 78 | context.add(new MinusExpression()); 79 | context.add(new MinusExpression()); 80 | 81 | var list = context.getList(); 82 | for (var i = 0; i < list.length; i++) { 83 | var expression = list[i]; 84 | expression.interpret(context); 85 | } 86 | 87 | console.log("Result:" + context.getSum()); -------------------------------------------------------------------------------- /src/Iterator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Iterator { 4 | constructor() { 5 | console.log('Iterator Class created'); 6 | } 7 | 8 | first() { 9 | console.log('Iterator.first invoked'); 10 | } 11 | 12 | next() { 13 | console.log('Iterator.next invoked'); 14 | } 15 | 16 | isDone() { 17 | console.log('Iterator.isDone invoked'); 18 | } 19 | 20 | currentItem() { 21 | console.log('Iterator.currentItem invoked'); 22 | } 23 | } 24 | 25 | class ConcreteIterator extends Iterator { 26 | constructor(aggregate) { 27 | super(); 28 | this.index = 0; 29 | this.aggregate = aggregate; 30 | console.log('ConcreteIterator Class created'); 31 | } 32 | 33 | first() { 34 | console.log('ConcreteIterator.first invoked'); 35 | return this.aggregate.list[0]; 36 | } 37 | 38 | next() { 39 | console.log('ConcreteIterator.next invoked'); 40 | this.index += 1; 41 | return this.aggregate.list[this.index]; 42 | } 43 | 44 | currentItem() { 45 | console.log('ConcreteIterator.currentItem invoked'); 46 | return this.aggregate.list[this.index]; 47 | } 48 | } 49 | 50 | class Aggregate { 51 | constructor() { 52 | console.log('Aggregate Class created'); 53 | } 54 | 55 | createIterator() { 56 | console.log('Aggregate.CreateIterator invoked'); 57 | } 58 | } 59 | 60 | class ConcreteAggregate extends Aggregate { 61 | constructor(list) { 62 | super(); 63 | this.list = list; 64 | console.log('ConcreteAggregate Class created'); 65 | } 66 | 67 | createIterator() { 68 | console.log('ConcreteAggregate.CreateIterator invoked'); 69 | this.iterator = new ConcreteIterator(this); 70 | } 71 | } 72 | 73 | var aggregate = new ConcreteAggregate([0, 1, 2, 3, 4, 5, 6, 7]); 74 | aggregate.createIterator(); 75 | console.log(aggregate.iterator.first()); 76 | console.log(aggregate.iterator.next()); 77 | console.log(aggregate.iterator.currentItem()); -------------------------------------------------------------------------------- /src/Mediator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Mediator { 4 | constructor() { 5 | console.log('Mediator Class created'); 6 | } 7 | 8 | colleagueChanged(colleague) { 9 | console.log('Mediator.colleagueChanged invoked'); 10 | } 11 | } 12 | 13 | class ConcreteMediator extends Mediator { 14 | constructor() { 15 | super(); 16 | console.log('ConcreteMediator Class created'); 17 | this.colleague1 = new ConcreteColleague1(this); 18 | this.colleague2 = new ConcreteColleague2(this); 19 | } 20 | 21 | colleagueChanged(colleague) { 22 | console.log('ConcreteMediator.colleagueChanged invoked'); 23 | switch (colleague) { 24 | case this.colleague1: 25 | console.log('ConcreteColleague1 has Changed -> change ConcreteColleague2.feature: '); 26 | this.colleague2.setFeature('new feature 2'); 27 | break 28 | case this.colleague2: 29 | console.log('ConcreteColleague2 has Changed, but do nothing'); 30 | break 31 | default: 32 | console.log('Do nothing'); 33 | } 34 | } 35 | } 36 | 37 | class Colleague { 38 | constructor() { 39 | console.log('Colleague Class created'); 40 | } 41 | 42 | changed() { 43 | console.log('Colleague.changed invoked'); 44 | this.mediator.colleagueChanged(this); 45 | } 46 | } 47 | 48 | class ConcreteColleague1 extends Colleague { 49 | constructor(mediator) { 50 | super(); 51 | console.log('ConcreteColleague1 Class created'); 52 | this.mediator = mediator; 53 | this.feature = "feature 1"; 54 | } 55 | 56 | setFeature(feature) { 57 | console.log('ConcreteColleague1.setFeature invoked'); 58 | console.log('ConcreteColleague1 Feature has changed from ' + this.feature + ' to ' + feature) 59 | this.feature = feature; 60 | this.changed(); 61 | } 62 | } 63 | 64 | class ConcreteColleague2 extends Colleague { 65 | constructor(mediator) { 66 | super(); 67 | console.log('ConcreteColleague2 Class created'); 68 | this.mediator = mediator; 69 | this.feature = "feature 2"; 70 | } 71 | 72 | setFeature(feature) { 73 | console.log('ConcreteColleague2.setFeature invoked'); 74 | console.log('ConcreteColleague2 Feature has changed from ' + this.feature + ' to ' + feature); 75 | this.feature = feature; 76 | this.changed(); 77 | } 78 | } 79 | 80 | var mediator = new ConcreteMediator(); 81 | mediator.colleague1.setFeature("new feature 1"); -------------------------------------------------------------------------------- /src/Memento.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Originator { 4 | constructor() { 5 | console.log('Originator Class created'); 6 | this.state = 'a'; 7 | console.log('Originator created. State= ' + this.state); 8 | } 9 | 10 | setMemento(Memento) { 11 | console.log('Originator.setMemento invoked'); 12 | this.state = Memento.getState(); 13 | console.log('Originator.setMemento State= ' + this.state); 14 | } 15 | 16 | createMemento(state) { 17 | console.log('Originator.createMemento invoked'); 18 | return new Memento(state); 19 | } 20 | } 21 | 22 | class Memento { 23 | constructor(state) { 24 | console.log('Memento Class created'); 25 | this.state = state; 26 | console.log('Memento created. State= ' + this.state); 27 | } 28 | 29 | getState() { 30 | console.log('Memento.getState invoked'); 31 | return this.state; 32 | } 33 | 34 | setState(state) { 35 | console.log('Memento.setState invoked'); 36 | this.state = state; 37 | } 38 | } 39 | 40 | class Caretaker { 41 | constructor() { 42 | console.log('Caretaker Class created'); 43 | this.mementos = []; 44 | } 45 | 46 | addMemento(memento) { 47 | console.log('Caretaker.addMemento invoked'); 48 | this.mementos.push(memento) 49 | } 50 | 51 | setMemento() { 52 | console.log('Caretaker.setMemento invoked'); 53 | return this.mementos[this.mementos.length - 1] 54 | } 55 | } 56 | 57 | let caretaker = new Caretaker(); 58 | let originator = new Originator(); 59 | caretaker.addMemento(originator.createMemento('b')); 60 | originator.setMemento(caretaker.setMemento()); 61 | console.log(originator.state); -------------------------------------------------------------------------------- /src/Observer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Subject { 4 | constructor() { 5 | console.log('Subject Class created'); 6 | } 7 | 8 | attach(observer) { 9 | this.observers.push(observer); 10 | console.log('Subject.attach invoked'); 11 | } 12 | 13 | dettach(observer) { 14 | console.log('Subject.dettach invoked'); 15 | for (var i in this.observers) { 16 | if (this.observers[i] === observer) { 17 | this.observers.splice(i, 1); 18 | } 19 | } 20 | } 21 | 22 | notify() { 23 | console.log('Subject.notify invoked'); 24 | for (var i in this.observers) { 25 | this.observers[i].update(this); 26 | } 27 | } 28 | } 29 | 30 | class ConcreteSubject extends Subject { 31 | constructor() { 32 | super(); 33 | this.subjectState = null; 34 | this.observers = []; 35 | console.log('ConcreteSubject Class created'); 36 | } 37 | 38 | getState() { 39 | console.log('ConcreteSubject.getState invoked'); 40 | return this.subjectState; 41 | } 42 | 43 | setState(state) { 44 | console.log('ConcreteSubject.setState invoked'); 45 | this.subjectState = state; 46 | this.notify(); 47 | } 48 | } 49 | 50 | class Observer { 51 | constructor() { 52 | console.log('Observer Class created'); 53 | } 54 | 55 | update() { 56 | console.log('Observer.update invoked'); 57 | } 58 | } 59 | 60 | class ConcreteObserver extends Observer { 61 | constructor() { 62 | super(); 63 | this.observerState = ''; 64 | console.log('ConcreteObserver Class created'); 65 | } 66 | 67 | update(Subject) { 68 | console.log('ConcreteObserver.update invoked'); 69 | this.observerState = Subject.getState(); 70 | console.log('Observer new state: ' + this.observerState); 71 | } 72 | } 73 | 74 | var observer1 = new ConcreteObserver(); 75 | var observer2 = new ConcreteObserver(); 76 | var subject = new ConcreteSubject(); 77 | subject.attach(observer1); 78 | subject.attach(observer2); 79 | subject.setState('state 1'); -------------------------------------------------------------------------------- /src/Prototype.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Prototype { 4 | constructor(prototype) { 5 | console.log("Prototype Class created"); 6 | } 7 | 8 | setFeature(key, val) { 9 | this[key] = val 10 | } 11 | 12 | clone() { 13 | console.log("Prototype.clone invoked"); 14 | } 15 | } 16 | 17 | class ConcretePrototype1 extends Prototype { 18 | constructor() { 19 | super(); 20 | console.log("ConcretePrototype1 created"); 21 | this.feature = "feature 1" 22 | } 23 | 24 | clone() { 25 | console.log('ConcretePrototype1.clone invoked'); 26 | let clone = new ConcretePrototype1(); 27 | let keys = Object.keys(this); 28 | 29 | keys.forEach(k => clone.setFeature(k, this[k])); 30 | 31 | console.log("ConcretePrototype1 cloned"); 32 | return clone; 33 | } 34 | } 35 | 36 | class ConcretePrototype2 extends Prototype { 37 | constructor() { 38 | super(); 39 | console.log("ConcretePrototype2 created"); 40 | this.feature = "feature 2" 41 | } 42 | 43 | clone() { 44 | console.log('ConcretePrototype2.Clone function'); 45 | let clone = new ConcretePrototype2(); 46 | let keys = Object.keys(this); 47 | 48 | keys.forEach(k => clone.setFeature(k, this[k])); 49 | console.log("ConcretePrototype2 cloned"); 50 | return clone; 51 | } 52 | } 53 | 54 | var proto1 = new ConcretePrototype1(); 55 | proto1.setFeature('feature', "feature 11"); 56 | var clone1 = proto1.clone(); 57 | console.log(clone1.feature); 58 | console.log(typeof clone1); 59 | console.log(clone1 === proto1); 60 | 61 | var proto2 = new ConcretePrototype2(); 62 | proto2.setFeature('feature', "feature 22"); 63 | var clone2 = proto2.clone(); 64 | console.log(clone2.feature); 65 | console.log(typeof clone2); 66 | console.log(clone2 === proto2); -------------------------------------------------------------------------------- /src/Proxy.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Subject { 4 | constructor() { 5 | console.log('Subject Class created'); 6 | } 7 | 8 | request() { 9 | console.log('Subject.request invoked'); 10 | } 11 | } 12 | 13 | class RealSubject extends Subject { 14 | constructor() { 15 | super() 16 | console.log('RealSubject Class created'); 17 | } 18 | 19 | request() { 20 | console.log('RealSubject.request invoked'); 21 | } 22 | } 23 | 24 | class Proxy extends Subject { 25 | constructor() { 26 | super() 27 | console.log('Proxy Class created'); 28 | } 29 | 30 | request() { 31 | this.realSubject = new RealSubject(); 32 | this.realSubject.request(); 33 | } 34 | } 35 | 36 | var proxy = new Proxy() 37 | proxy.request() -------------------------------------------------------------------------------- /src/Signleton.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Singleton { 4 | constructor(data) { 5 | if (Singleton.prototype.Instance === undefined) { 6 | this.data = data; 7 | Singleton.prototype.Instance = this; 8 | } 9 | return Singleton.prototype.Instance; 10 | } 11 | } 12 | 13 | let ob1 = new Singleton("one"); 14 | let ob2 = new Singleton("two"); 15 | let ob3 = new Singleton("Three"); 16 | ob2.init = 'Object flg'; 17 | 18 | console.log(ob1 === ob2); 19 | console.log(ob1 === ob3); 20 | 21 | console.log(ob1); 22 | console.log(ob2); 23 | console.log(ob3); -------------------------------------------------------------------------------- /src/State.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Context { 4 | constructor(state) { 5 | console.log("Context Class created"); 6 | switch (state) { 7 | case "A": 8 | this.state = new ConcreteStateA() 9 | break 10 | case "B": 11 | this.state = new ConcreteStateB() 12 | break 13 | default: 14 | this.state = new ConcreteStateA() 15 | } 16 | } 17 | 18 | request() { 19 | console.log('Context.request invoked'); 20 | this.state.handle(this); 21 | } 22 | } 23 | 24 | class State { 25 | constructor() { 26 | console.log("State Class created"); 27 | } 28 | 29 | handle() { 30 | console.log('State.handle invoked'); 31 | } 32 | } 33 | 34 | class ConcreteStateA extends State { 35 | constructor() { 36 | super(); 37 | console.log("ConcreteStateA Class created"); 38 | } 39 | 40 | handle(context) { 41 | console.log('ConcreteStateA.handle invoked'); 42 | } 43 | } 44 | 45 | class ConcreteStateB extends State { 46 | constructor() { 47 | super(); 48 | console.log("ConcreteStateB Class created"); 49 | } 50 | 51 | handle(context) { 52 | console.log('ConcreteStateB.handle invoked'); 53 | } 54 | } 55 | 56 | let context = new Context("A") 57 | context.request() -------------------------------------------------------------------------------- /src/Strategy.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Context { 4 | constructor(type) { 5 | console.log('Context Class created!'); 6 | switch (type) { 7 | case "A": 8 | this.strategy = new ConcreteStrategyA() 9 | break 10 | case "B": 11 | this.strategy = new ConcreteStrategyB() 12 | break 13 | default: 14 | this.strategy = new ConcreteStrategyA() 15 | } 16 | } 17 | 18 | contextInterface() { 19 | console.log('Context.contextInterface invoked'); 20 | this.strategy.algorithmInterface() 21 | } 22 | } 23 | 24 | class Strategy { 25 | constructor() { 26 | console.log('Strategy Class created!'); 27 | } 28 | 29 | algorithmInterface() { 30 | console.log('Strategy.algorithmInterface invoked'); 31 | } 32 | } 33 | 34 | class ConcreteStrategyA extends Strategy { 35 | constructor() { 36 | super(); 37 | console.log('ConcreteStrategyA Class created!'); 38 | } 39 | 40 | algorithmInterface() { 41 | console.log('ConcreteStrategyA.algorithmInterface invoked'); 42 | } 43 | } 44 | 45 | class ConcreteStrategyB extends Strategy { 46 | constructor() { 47 | super(); 48 | console.log('ConcreteStrategyB Class created!'); 49 | } 50 | 51 | algorithmInterface() { 52 | console.log('ConcreteStrategyB.algorithmInterface invoked'); 53 | } 54 | } 55 | 56 | let contextA = new Context("A"); 57 | contextA.contextInterface(); 58 | let contextB = new Context("B"); 59 | contextB.contextInterface(); -------------------------------------------------------------------------------- /src/Template Method.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class AbstractClass { 4 | constructor() { 5 | console.log('AbstractClass Class created!'); 6 | } 7 | 8 | templateMethod() { 9 | console.log('AbstractClass.templateMethod invoked'); 10 | this.primitiveOperation1(); 11 | this.primitiveOperation2(); 12 | } 13 | 14 | primitiveOperation1() { 15 | console.log('AbstractClass.primitiveOperation1 invoked'); 16 | } 17 | 18 | primitiveOperation2() { 19 | console.log('AbstractClass.primitiveOperation2 invoked'); 20 | } 21 | } 22 | 23 | class ConcreteClass extends AbstractClass { 24 | constructor() { 25 | super(); 26 | console.log('ConcreteClass Class created!'); 27 | } 28 | 29 | primitiveOperation1() { 30 | console.log('ConcreteClass.primitiveOperation1 invoked'); 31 | } 32 | 33 | primitiveOperation2() { 34 | console.log('ConcreteClass.primitiveOperation2 invoked'); 35 | } 36 | } 37 | 38 | let obj = new ConcreteClass(); 39 | obj.templateMethod(); -------------------------------------------------------------------------------- /src/Visitor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class Visitor { 4 | constructor() { 5 | console.log('Visitor Class created!'); 6 | } 7 | 8 | visitConcreteElementA(ConcreteElementA) { 9 | console.log('Visitor.visitConcreteElementA invoked'); 10 | } 11 | 12 | visitConcreteElementB(ConcreteElementB) { 13 | console.log('Visitor.visitConcreteElementB invoked'); 14 | } 15 | } 16 | 17 | class ConcreteVisitor1 extends Visitor { 18 | constructor() { 19 | super(); 20 | console.log('ConcreteVisitor1 Class created!'); 21 | } 22 | 23 | visitConcreteElementA(ConcreteElementA) { 24 | console.log('ConcreteVisitor1.visitConcreteElementA invoked'); 25 | } 26 | 27 | visitConcreteElementB(ConcreteElementB) { 28 | console.log('ConcreteVisitor1.visitConcreteElementB invoked'); 29 | } 30 | } 31 | 32 | class ConcreteVisitor2 extends Visitor { 33 | constructor() { 34 | super(); 35 | console.log('ConcreteVisitor2 Class created!'); 36 | } 37 | 38 | visitConcreteElementA(ConcreteElementA) { 39 | console.log('ConcreteVisitor2.visitConcreteElementA invoked'); 40 | } 41 | 42 | visitConcreteElementB(ConcreteElementB) { 43 | console.log('ConcreteVisitor2.visitConcreteElementB invoked'); 44 | } 45 | } 46 | 47 | class ObjectStructure { 48 | constructor() { 49 | console.log('ObjectStructure Class created!'); 50 | } 51 | } 52 | 53 | class Element { 54 | constructor() { 55 | console.log('Element Class created!'); 56 | } 57 | 58 | Accept(visitor) { 59 | console.log('Element.visitConcreteElementB invoked'); 60 | } 61 | } 62 | 63 | class ConcreteElementA extends Element { 64 | constructor() { 65 | super(); 66 | console.log('ConcreteElementA Class created!'); 67 | } 68 | 69 | accept(visitor) { 70 | console.log('ConcreteElementA.accept invoked'); 71 | visitor.visitConcreteElementA(this); 72 | } 73 | 74 | operationA() { 75 | console.log('ConcreteElementA.operationA invoked'); 76 | } 77 | } 78 | 79 | class ConcreteElementB extends Element { 80 | constructor() { 81 | super(); 82 | console.log('ConcreteElementB Class created!'); 83 | } 84 | 85 | accept(visitor) { 86 | console.log('ConcreteElementB.accept invoked'); 87 | visitor.visitConcreteElementB(this); 88 | } 89 | 90 | operationB() { 91 | console.log('ConcreteElementB.operationB invoked'); 92 | } 93 | } 94 | 95 | let visitor1 = new ConcreteVisitor1(); 96 | let visitor2 = new ConcreteVisitor2(); 97 | let elementA = new ConcreteElementA(); 98 | let elementB = new ConcreteElementB(); 99 | elementA.accept(visitor1); 100 | elementB.accept(visitor2); --------------------------------------------------------------------------------