工厂模式
特点
- 将实例化操作进行单独封装,可以将内部实现细节进行封装,外部只需要调用,而不考虑具体实现。
- 实例化对象的原型不是创建类,而是创建对象调用的内部封装类。
demo
clss Creator{
create(name){
return new Product(name);
}
}
class Product{
constructor(name){
this.name = name;
}
fun1(){
console.log('fun1');
}
fun2(){
console.log('fun2');
}
}
const creator = new Creator();
const p1 = creator.create('p1');
const p2 = creator.create('p2');