You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict';classAbstractFactory{constructor(){}createProductA(product){}createProductB(product){}}classConcreteFactory1extendsAbstractFactory{constructor(){super()facade.log("ConcreteFactory1 class created");}createProductA(product){facade.log('ConcreteFactory1 createProductA')returnnewProductA1()}createProductB(product){facade.log('ConcreteFactory1 createProductB')returnnewProductB1()}}classConcreteFactory2extendsAbstractFactory{constructor(){super()facade.log("ConcreteFactory2 class created");}createProductA(product){facade.log('ConcreteFactory2 createProductA')returnnewProductA2()}createProductB(product){facade.log('ConcreteFactory2 createProductB')returnnewProductB2()}}classAbstractProductA{constructor(){}}classAbstractProductB{constructor(){}}classProductA1extendsAbstractProductA{constructor(){super()facade.log('ProductA1 created')}}classProductA2extendsAbstractProductA{constructor(){super()facade.log('ProductA2 created')}}classProductB1extendsAbstractProductB{constructor(){super()facade.log('ProductB1 created')}}classProductB2extendsAbstractProductB{constructor(){super()facade.log('ProductB2 created')}}functioninit_AbstractFactory(){varfactory1=newConcreteFactory1()varproductB1=factory1.createProductB()varfactory2=newConcreteFactory2()varproductA2=factory2.createProductA()}
建造者
'use strict';classDirector{constructor(){this.structure=['Maze','Wall','Door'];facade.log("Director class created");}Construct(){for(varallinthis.structure){letbuilder=newConcreteBuilder()builder.BuildPart(this.structure[all]);builder.GetResult()}}}classBuilder{constructor(){}BuildPart(){}}classConcreteBuilderextendsBuilder{constructor(){super()facade.log("ConcreteBuilder class created");}BuildPart(rawmaterial){facade.log("ConcreteBuilder BuildPart()");varmaterial=rawmaterialthis.product=newProduct(material)}GetResult(){facade.log(JSON.stringify(this.product))returnthis.product}}classProduct{constructor(material){facade.log("Product class created");this.data=material}}functioninit_Builder(){letdirector=newDirector()director.Construct()}
'use strict';let_singleton=nullclassSingleton{constructor(data){if(!_singleton){this.data=data_singleton=this}elsereturn_singletonfacade.log("Singleton class created")}SingletonOperation(){facade.log('SingletonOperation')}GetSingletonData(){returnthis.data}}functioninit_Singleton(){varsingleton1=newSingleton("data1")varsingleton2=newSingleton("data2")facade.log(singleton1.GetSingletonData())facade.log(singleton2.GetSingletonData())facade.log(singleton1instanceofSingleton)facade.log(singleton2instanceofSingleton)facade.log(singleton1===singleton2)}
'use strict';classHandler{constructor(){}HandleRequest(){}}classConcreteHandler1extendsHandler{constructor(){super()facade.log('ConcreteHandler1 created')}setSuccessor(successor){this.successor=successor}HandleRequest(request){if(request==='run')facade.log('ConcreteHandler1 has handled the request')else{facade.log('ConcreteHandler1 calls his successor')this.successor.HandleRequest(request)}}}classConcreteHandler2extendsHandler{constructor(){super()facade.log('ConcreteHandler2 created')}HandleRequest(request){facade.log('ConcreteHandler2 has handled the request')}}functioninit_ChainofResponsibility(){lethandle1=newConcreteHandler1()lethandle2=newConcreteHandler2()handle1.setSuccessor(handle2)handle1.HandleRequest('run')handle1.HandleRequest('stay')}
'use strict';classMediator{constructor(){}ColleagueChanged(colleague){}}classConcreteMediatorextendsMediator{constructor(){super()facade.log('ConcreteMediator created')this.colleague1=newConcreteColleague1(this)this.colleague2=newConcreteColleague2(this)}ColleagueChanged(colleague){switch(colleague){casethis.colleague1:
facade.log('ConcreteColleague1 has Changed -> change ConcreteColleague2.feature: ')this.colleague2.setFeature('new feature 2')breakcasethis.colleague2:
facade.log('ConcreteColleague2 has Changed, but do nothing')breakdefault:
facade.log('Do nothing')}}}classColleague{constructor(){}Changed(){this.mediator.ColleagueChanged(this)}}classConcreteColleague1extendsColleague{constructor(mediator){super()facade.log('ConcreteColleague1 created')this.mediator=mediatorthis.feature="feature 1"}setFeature(feature){facade.log('ConcreteColleague1 Feature has changed from '+this.feature+' to '+feature)this.feature=featurethis.Changed()}}classConcreteColleague2extendsColleague{constructor(mediator){super()facade.log('ConcreteColleague2 created')this.mediator=mediatorthis.feature="feature 2"}setFeature(feature){facade.log('ConcreteColleague2 Feature has changed from '+this.feature+' to '+feature)this.feature=featurethis.Changed()}}functioninit_Mediator(){varmediator=newConcreteMediator()mediator.colleague1.setFeature("new feature 1")}
es6-design-patterns
在EcmaScript 6中实现的软件设计模式
在这个项目中,我们介绍EcmaScript 6中的软件设计模式。
图像由StarUML从staruml-design-patterns项目中的.mdj文件生成。
内容
创作模式
结构模式
行为模式
创作模式
抽象工厂
建造者
工厂方法
原型模式
单例模式
结构模式
适配器模式
桥接模式
组合模式
装饰器模式
外观模式
享元模式
代理模式
行为模式
责任链模式
命令模式
解释器模式
迭代器模式
中介者模式
备忘录模式
观察者模式
状态模式
策略模式
模板方法模式
访问者模式
The text was updated successfully, but these errors were encountered: