`

js 简单工厂模式

 
阅读更多
/**
 * 简单工厂
 */
// Constructor.
//定义接口
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]);        
    }    
};    

// Static class method.
//用来检测是否实现接口
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 interface = arguments[i];
        if(interface.constructor !== Interface) {
            throw new Error("Function Interface.ensureImplements expects arguments "   
              + "two and above to be instances of Interface.");
        }
        
        for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
            var method = interface.methods[j];
            if(!object[method] || typeof object[method] !== 'function') {
                throw new Error("Function Interface.ensureImplements: object " 
                  + "does not implement the " + interface.name 
                  + " interface. Method " + method + " was not found.");
            }
        }
    } 
};
// 创建接口
var Bicycle = new Interface("Bicycle",['assemble','wash','ride','repair']);

// 创建工厂
var BicyleFactory = {
    createBicyle : function(mode){
    	var bicycle = null;
        switch(mode){
            case 'yonghe' :
                bicycle = new Yonghe();
                break;
            case 'aima' :
                bicycle = new Aima();
                break;
            case 'aoda':
                bicycle = new Aoda();
                break;
            default :
                throw new Error("mode not found");
        }
        Interface.ensureImplements(bicycle,Bicycle);// 检查实现.
        return bicycle;
    }
};
// 定义商店,提供各种Bicyle
var BicycleShop = function(){};
BicycleShop.prototype ={
    sellBicycle : function(mode){
         var bicycle = BicycleFactory.createBicycle(mode);
         bicycle.assemble();
         bicycle.wash();
         return bicycle;
    }
}

// YongheBicyle
var Yonghe = function(){
    
};
// 实现接口
Yonghe.prototype={
    assemble : function(){
        alert('清洗');
    },
    wash : function(){
        alert('组装');
    },
    ride : function(){
    
    },
    repair : function(){
    
    }
};

// var Aima = function(){
//    
// };
// var Aoda = function(){
//
// };
var bicycleShop = new BicycleShop();
bicycleShop.sellBicycle('giant');
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics