ES6设计模式之工厂模式

这个模式其实比较难理解,一般的理解可能存在问题,这是对对象的使用和对象生产分离,例子是不完整的看例子仔细体会呀

class Jianbing{ constructor(){ this.jian = null; }

orderCreater(){ this.jian = this.JianbingCreater(); this.cut(); this.box(); }

JianbingCreater(){ throw “you should override this function in you class!” }

cut(){ console.log(cut jianging from ${this.jian.name}); }

box(){ console.log(box by daizi with ${this.jian.color} tie); }

}

class ChenguJianbing extends Jianbing{ constructor(){ super() }

JianbingCreater(){ console.log(“ye!you are right!this is made in CHENGU.”); return { name:‘chen gu’, color:‘yellow’ } } }

class ZHONGGONGjiangbing extends Jianbing{ constructor(){ super() }

JianbingCreater(){ console.log(“ye!you are right!this is made in DONGZHONGGUONG.”); return { name:‘zhonggong’, color:‘red’ } } }

var CJ = new ChenguJianbing(); CJ.orderCreater(); var DZGJ = new ZHONGGONGjiangbing(); DZGJ.orderCreater();

猜你喜欢

转载自www.cnblogs.com/node-jili/p/10161442.html