主题
创建型模式
创建型设计模式。
单例模式
确保一个类只有一个实例。
javascript
class Singleton {
constructor() {
if (Singleton.instance) {
return Singleton.instance
}
Singleton.instance = this
}
}工厂模式
创建对象而不指定具体的类。
javascript
class Factory {
createProduct(type) {
switch(type) {
case 'A': return new ProductA()
case 'B': return new ProductB()
}
}
}建造者模式
逐步构建复杂对象。
原型模式
通过克隆创建对象。