js 抽象工厂模式A


var Interface = function(name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expected exactly 2.");
}

this.name = name;
this.methods = [];
for (var i = 0, len = methods.length; i < len; i++) {
if (typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
Interface.ensureImplements = function(object) {
if (arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with "
+ arguments.length + "arguments, but expected at least 2.");
}

for (var i = 1, len = arguments.length; i < len; i++) {
var v_interface = arguments[i];
if (v_interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments "
+ "two and above to be instances of Interface.");
}

for (var j = 0, methodsLen = v_interface.methods.length; j < methodsLen; j++) {
var method = v_interface.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + v_interface.name
+ " interface. Method " + method + " was not found.");
}
}
}
};

function extend(subClass, superClass) {
var F = function() {
};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;

subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}

var AbstractFactory = function() {
};
AbstractFactory.prototype = {
chooesFactory : function(mode) {
var factory = null;
switch (mode) {
case 'giant' :
factory = new GiantFactory();
break;
case 'cnforever' :
factory = new CnforeverFactory();
break;
case 'merida' :
factory = new MeridaFactory();
break;
default :
throw new Error("mode not found");
}
return factory;
},
createBicycle : function() {
throw new Error('Unsupported operation on an abstract class:createBicycle');
}
};
var GiantFactory = function() {
};
extend(GiantFactory, AbstractFactory);
GiantFactory.prototype = {
createBicycle : function() {
var giantBicycle = new GiantBicycle();
Interface.ensureImplements(giantBicycle, Bicycle);
return giantBicycle;
}
};
var Bicycle = new Interface("Bicycle", ['assemble', 'wash', 'ride', 'repair']);
var GiantBicycle = function() {

};
GiantBicycle.prototype = {
assemble : function() {
alert('清洗');
},
wash : function() {
alert('组装');
},
ride : function() {

},
repair : function() {

}
};

var BicycleShop = function() {
};
BicycleShop.prototype = {
sellBicycle : function(mode) {
var abstractFactory = new AbstractFactory();
var factory = abstractFactory.chooesFactory(mode);
var bicycle = factory.createBicycle();
bicycle.assemble();
bicycle.wash();;
return bicycle;
}
}

var bicycleShop = new BicycleShop();
bicycleShop.sellBicycle('giant');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值