js设计模式--工厂模式

简单工厂模式

	/*BicycleFactory  用来创建子对象*/
	var BicycleFactory = {
		 	createBicycle: function(model){
		 			var bicycle;
		 			switch(model){
		 					case 'type1':
		 							bicycle =  new Speedster();
		 					case 'type2':
		 							bicycle = new Lowrider();
		 					default:
		 							bicycle = new Bike();
		 			}
		 			/*所有的子对象都继承Bicycle接口,ensureImplements方法保证子对象实现所有接口方法*/
		 			Interface.ensureImplements(bicycle,Bicycle);
		 			return bicycle;
		 	}
	}
/*Bicycle接口*/
var Bicycle = new Interface('Bicycle',['assemble','wash','ride']);
var Speedster = function(){}
Speedster.prototype ={
	assemble: function(){},
	wash: function(){},
	ride: function(){}
}
/*定义自行车商店*/
var BicycleShop = function(){};
BicycleShop.prototype = {
	sellBicycle: function(model){
		var bicycle = BicycleFactory.createBicycle(model));
		bicycle.assemble();
		bicycle.wash();
		return bicycle;
	}
}
var shop = new BicycleShop();
var sellBicycle = shop.sellBicycle('type1')
	/*接口*/
 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 inter = arguments[i];
		if(inter.constructor !== Interface ){
			throw new Error('Function Interface.ensureImplements expects arguments two and above to be instance of Interface. ');
		}
		for(var j=0,mLen=inter.methods.length; j<mLen; j++){
			var method = inter.methods[j];
			if(!object[method] || typeof object[method] !== 'function'){
				throw new Error('object does not implement this '+ inter.name + ' interface.Method '+ method + ' was not found. ');
			}
		}
	}
}

简单工厂模式:就是把成员对象的创建工作转交给一个外部对象,外部对象可以是一个简单的命名空间对象(对象字面量)或者一个类的实例

工厂模式:是一个将成员对象实例化推迟到子类中进行的类,父类是一个抽象类

/*工厂模式*/
var Shop = function(){};
Shop.prototype ={
	sellBicycle: function(model){
		var bicycle = this.createBicycle(model);
		bicycle.assemble();
		bicycle.wash();
		return bicycle;
	},
	createBicycle: function(model){
		/*抽象方法不能直接调用,需要在子类中实现,直接调用抛出错误*/
		throw new Error('Unsupported operation on an abstract class.');
	}
}
var ChildShop = function(){};
extend(ChildShop,Shop);
ChildShop.prototype.createBicycle = function(model){
	var bicycle;
	switch(model){
			case 'type1':
					bicycle =  new Speedster();
			case 'type2':
					bicycle = new Lowrider();
			default:
					bicycle = new Bike();
	}
	/*所有的子对象都继承Bicycle接口,ensureImplements方法保证子对象实现所有接口方法*/
	Interface.ensureImplements(bicycle,Bicycle);
	return bicycle;
}
/*使用*/
var shop1 = new ChildShop();
var bike = shop1.sellBicycle('type1');
/*类继承代码*/
function extend(subClass,superClass){
	var F = new Function();
	F.prototype = superClass.prototype;
	subClass.prototype = new F();
	subClass.prototype.constructor = subClass;
	/*定义superClass属性 用来解除与父类的强关联*/
	subClass.superClass = superClass.prototype;
	if(subClass.prototype.constructor == Object.prototype.constructor){
		subClass.prototype.constructor = subClass;
	}
}

你可能感兴趣的:(js之设计模式)