javascript 简单工厂

简单工厂


简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

例如:

自行车商店 ,客户要求不同的类型(编号)的车,返回不同自行车的实例

   //定义一个接口类
    var Interface = function (name,methods) {
        if(arguments.length!=2){
            throw new Error('必须输入两个参数,当前个数'+arguments.length);
        };
        this.name = name; //接口名
        this.methods = []; //函数名
        for(var i=0;i<methods.length;i++){
            if(typeof methods[i] !== 'string'){
                throw new Error('方法名参数必须为string');
            }else{
                this.methods.push(methods[i]);
            }

        }
    };
    //定义静态方法,检验实现接口和实现类 参数object为实现类对象

    Interface.ensureImplements = function (object) {
        if(arguments.length<2){
            throw new Error('必须输入两个参数,当前个数' + arguments.length);
            return;
        };
        for(var i =1;i<arguments.length;i++){
            var interface = arguments[i];
            if(interface.constructor != Interface){
                throw new Error("接口没有实现");
            };
            for(var j=0;j<interface.methods.length;j++){
                var method = interface.methods[j];
                if(!object[method] || typeof object[method]!=='function'){
                    throw new Error("接口名:"+interface.name+"方法名:"+method+"没找到");
                }
            }
        }

    };
    //封装的继承的方法
    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;
        }
    }


   //自行车类 
    function Bicycle(name,price,type,style) {
        this.name = name || "一般车";
        this.price = price || 300;
        this.type = type || 1103;
        this.style = style;
        this.getInfo =function () {
            return "型号:"+this.type + "名称:"+this.name+"售价:"+ this.price;
        }
        this.styleChose = function () {
            return "样式"+this.style+"种";
        }

    }
	//好车
    function GoodBicke(name,price,type,style) {
        GoodBicke.superclass.constructor.call(this);
        this.name = name || "超好车";
        this.price = price || 10000;
        this.type = type || 1102;
        this.speed = 50;
        this.style = style || 20;
        this.run = function () {
            return "最大速度"+this.speed;
        }
    }
    //一般车
    function NormalBicke(type,name,price,style) {
        NormalBicke.superclass.constructor.call(this);
        this.name = name || "平价车";
        this.price = price || 500;
        this.type = type;
        this.style = style || 100;
        this.speed = 20;
        this.run = function () {
            return "最大速度"+this.speed;
        }

    }
    //坏车
    function BadBicke(name,price,type,style) {
        BadBicke.superclass.constructor.call(this);
        this.name = name;
        this.price = price;
        this.type = type;
        this.style = style;
        this.speed = 10;
        this.run = function () {
            return "最大速度"+this.speed;
        }

    }
	//继承自行车类
    extend(GoodBicke,Bicycle);
    extend(NormalBicke,Bicycle);
    extend(BadBicke,Bicycle);

    /*
    定义接口
    * */

    var Bicke = new Interface('Bicke',['run','styleChose','getInfo']);
    //自行车商店类
    function shop() {};
    shop.prototype={
    //卖自行车
        sellBicke:function (type) {
            var bicke = null;
            switch (type){
                case 1101:
                    bicke = new GoodBicke();
                    break;
                case 1102:
                    bicke = new BadBicke('差车',100,type);
                    break;
                default:
                    bicke = new NormalBicke(type);
            }

            Interface.ensureImplements(bicke,Bicke);

            return bicke;
        }
    }
  //测试::
  
    var person = new shop;
    var b = person.sellBicke(1101);
    console.log(b.getInfo());
    console.log(b.run());

猜你喜欢

转载自blog.csdn.net/zshsats/article/details/82979494