js 추상 공장 모드 B
4799 단어 추상 공장 모델
//
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 clone(object) {
function F() {
}
F.prototype = object;
return new F;
}
//
var AbstractFactory = (function() {
//
function checkImplements(object) {
var isContain = false;
var v_method = '';
if (arguments.length < 2) {
throw new Error("Function AbstractFactory.checkImplements called with "
+ arguments.length + "arguments, but expected at least 2.");
}
for (var i = 1; i < arguments.length; i++) {
var v_AbstractClass = arguments[i];
for (key in v_AbstractClass) {
if (!object[key] && typeof object[key] !== 'function'
&& key != 'getFactory') {
isContain = true;
v_method = key;
break;
}
}
}
if (isContain) {
throw new Error("Function AbstractFactory.ensureImplements: object "
+ "does not implement the AbstractFactory "
+ "AbstractCalss. AbstractMethod "
+ v_method
+ " was not found.");
}
}
//
function chooesFactory(mode) {
var factory = null;
switch (mode) {
case 'giant' :
factory = GiantFactory;
break;
case 'cnforever' :
factory = CnforeverFactory;
break;
case 'merida' :
factory = MeridaFactory;
break;
default :
throw new Error("mode not found");
}
checkImplements(factory, AbstractFactory)
return factory;
}
return {
getFactory : function(mode) {
return chooesFactory(mode);
},
//
createBicycle : function() {
throw new Error('Unsupported operation on an abstract class:createBicycle');
}
}
})();
//
var GiantFactory = clone(AbstractFactory);
GiantFactory = {
//
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 bicycle = AbstractFactory.getFactory(mode).createBicycle();
bicycle.assemble();
bicycle.wash();;
return bicycle;
}
}
var bicycleShop = new BicycleShop();
bicycleShop.sellBicycle('giant');
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
js 추상 공장 모드 BMethod " + method + " was not found."); } } } }; // 原形继承 function clone(object) { function F() { } F.prototype = object;...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.