JS中的工厂模式

时间:2023-01-18 08:25:58
.一个栗子:

var BicycleShop = function(){};
BicycleShop.prototype = {
sellBicycle : function( model ){
var bicycle;
switch(model){
case "The Speedster":
bicycle = new Speedster();
break;
case "The Lowrider":
bicycle = new Lowrider();
break;
case "The Cruiser":
default:
bicycle = new Cruiser();
break;
}
return bicycle;
}
} .简单工厂模式: // 以上方式很管用,但是一旦说我需要添加一些自行车款式的时候我就必须修改 BicycleShop里的 switch部分,那么只要是修改就有可能带来BUG。所以,将这部分生成实例的代码单独的提出来分工交给一个简单的工厂对象是一个很不错的方法。
// BicycleFactory对象里有一个createBicycle参数,这个参数为(bycicle)
var BicycleFactory = {
createBicycle : function( model ){
var bicycle;
switch( model ){
case "The Speedster":
bicycle = new Speedster();
break;
case "The Lowrider":
bicycle = new Lowrider();
break;
case "The Cruiser":
default:
bicycle = new Cruiser();
break;
}
return bycicle;
}
} // BicycleFactory是一个脱离于BicycleShop的单体。降低耦合度的效果显而易见。当需要添加新的类型的时候,不需要动 BicycleShop 只需修改工厂单体对象就可以。
// 在BicycleShop中调用BicycleFactory.createBicycle()方法
var BicycleShop = function(){};
BicycleShop.prototype = {
sellBicycle : function( model ){
var bicycle = BicycleFactory.createBicycle( model );
return bicycle;
}
}
// 以上就是一个很好的 简单工厂模式 的实例。该模式将成员对象的创建工作交给一个外部对象实现,该外部对象可以是一个简单的命名空间,也可以是一个类的实例。 .工厂模式: //commonInterface相当于一个接口,让所有对象都能通过同一个方法名.commonInterface(),而访问到自己新建对象的函数
var MyApi = function(){};
MyApi.prototype={
commonInterface: function( model ){
var bicycle = this.createBicycle( model ); //注意这里的this.createBicycle( model )
return bicycle;
}
} var A_Class = function(){};
A_Class.prototype={
commonInterface: function( model ){
var bicycle = this.createBicycle( model ); //注意这里的this.createBicycle( model )
return bicycle;
}
}
//extend( A_Class , commonInterface ); // extend(A,B)即将B的所有方法拷贝到A上去 A_Class.prototype.createBicycle = function( model ){
var bicycle;
switch( model ){
case "The Speedster":
bicycle = new Speedster_A();
break;
case "The Lowrider":
bicycle = new Lowrider_A();
break;
case "The Cruiser":
default:
bicycle = new Cruiser_A();
break;
}
return bicycle;
}
function Speedster_A(){console.log()}
function Lowrider_A (){console.log()}
function Cruiser_A (){console.log()} var B_Class = function(){};
B_Class.prototype={
commonInterface: function( model ){
var bicycle = this.createBicycle( model ); //注意这里的this.createBicycle( model )
return bicycle;
}
}
//extend( generalClass , basicClass ); B_Class.prototype.createBicycle = function( model ){
var bicycle;
switch( model ){
case "The Speedster":
bicycle = new Speedster_B();
break;
case "The Lowrider":
bicycle = new Lowrider_B();
break;
case "The Cruiser":
default:
bicycle = new Cruiser_B();
break;
}
return bicycle;
}
function Speedster_B(){console.log("AAAAAAAA")}
function Lowrider_B (){console.log("BBBBBBBB")}
function Cruiser_B (){console.log("CCCCCCCC")} var temp = new A_Class();
var test = temp.commonInterface("The Cruiser"); // var temp = new B_Class();
var test = temp.commonInterface("The Cruiser");// CCCCCCCC   // 工厂模式的优点: . 动态实现
  例如自行车的例子,创建一些用不同方式实现统一接口的对象,那么可以使用一个工厂方法或者简单工厂对象来简化实现过程。选择可以是明确进行的也可以是隐含的。 . 节省设置开销
如果对象要进行复杂的并且彼此相关的设置的时候,那么工厂模式可以很显著的减少每种对象的代码量。将特定的设置代码提取出来会使得代码有极大地提升。并且能优化结构便于维护。 . 用于许多小型对象组成一个大对象。 . 工厂模式之利
主要好处就是可以消除对象间的耦合,通过使用工程方法而不是new关键字。将所有实例化的代码集中在一个位子防止代码重复。 . 工厂模式之弊
大多数类最好使用new关键字和构造函数,可以让代码更加简单易读。而不必去查看工厂方法来知道。