您现在的位置: 365建站网 > 365文章 > javascript 工厂模式

javascript 工厂模式

文章来源:365jz.com     点击数:213    更新时间:2009-09-14 10:29   参与评论

/* 简单工厂 */

/* BicycleShop class */

var BicycleShop = function() {};
BicycleShop.prototype = {
    sellBicycle: function(model) {
        var bicycle;
       
        switch(model) {
            case 'The SpeedSter':
                bicycle = new SpeedSter();
             break;
            case 'The Lowrider':
                bicycle = new Lowrider();
             break;
         case 'The Comfort Cruiser':
            default:
                bicycle = new ComfortCruiser();
        }
       
        Interface.ensureImplements(bicycle, Bicycle);
        bicycle.assemble();
        bicycle.wash();
       
        return bicycle;
    }
};

/* The Bicycle interface. */
var Bicycle = new Interface('Bicycle', ['assemble','wash','ride','repaid']);

/* Speedster class. */
var Speedster = function() {//implements Bicycle
    //...
};

Speedster.prototype = {
    accemble: function() {
   
    },
    wash: function() {
   
    },
    ride: function() {
   
    },
    repaid: function() {
       
    }
};

var californiaCruisers = new BicycleShop();
var yourNewBike = californiaCruisers.sellBicyCle('The Speedster');


/* BicycleFactory namespace. */

var BicycleFactory ={
    createBicycle: function(model) {
        var bicycle;
       
        switch(model) {
            case 'The SpeedSter':
                bicycle = new SpeedSter();
             break;
            case 'The Lowrider':
                bicycle = new Lowrider();
             break;
         case 'The Comfort Cruiser':
            default:
                bicycle = new ComfortCruiser();
        }
        Interface.ensureImplements(bicycle, Bicycle);
        return bicycle;
    }
};

/* BicycleShop class, improved */
var BicycleShop = function() {};
BicycleShop.prototype = {
    var bicycle = BicycleFactory.createBicycle(model);
   
    bicycle.assemble();
    bicycle.wash();
   
    return bicycle;
};


/* 真正的工厂模式 */

/* BicycleShop class(abstract). */

var BicycleShop = function() {};
BicycleShop.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.');
    }
};


/* AcmeBicycleShop class. */
var AcmeBicycleShop = function() {};
extend(AcmeBicycleShop, BicycleShop);//类式继承
AcmeBicycleShop.prototype.createBicycle = function(model) {
    var bicycle;
    switch(model) {
        case 'The SpeedSter':
            bicycle = new SpeedSter();
            break;
        case 'The Lowrider':
            bicycle = new Lowrider();
            break;
        case 'The Comfort Cruiser':
        default:
            bicycle = new ComfortCruiser();
        }
    }
   
    Interface.ensureImplements(bicycle, Bicycle);
    return bicycle;
};

 

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (213人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号