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();