创建型设计模式
是一类处理对象创建的设计模式,通过某种方式控制对象的创建来避免基本对象创建时可能导致设计上的问题或增加设计上的复杂度。
1)工厂模式
class Product { constructor(options) { this.name = options.name; this.time = options.time; this.init(); } init() { console.log(`产品名:${this.name} 保质期:${this.time}`); } } class Factory { create(options) { return new Product(options); } } let factory = new Factory(); let product1 = factory.create({ name: "面包", time: "1个月" });
2)单例模式 (一个类只有一个实例)
function SingleObject() { this.name = "单例"; } SingleObject.getInstance = function() { if (!this.instance) { this.instance = new SingleObject(); } return this.instance; }; var obj1 = SingleObject.getInstance(); var obj2 = SingleObject.getInstance(); console.log(obj1 === obj2);
结构型设计模式
关注于如何将类或对象组合成更大、更复杂的结构,以简化设计。
1)适配器模式
2)装饰器模式
3)代理模式
4)外观模式
行为型设计模式
用于不同对象之间职责划分或者算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模式并加以实现。
1)观察者模式
2)迭代器模式
3)状态模式
原文:https://www.cnblogs.com/zhoudawei/p/11989267.html