这里拿Pro Javascript Design Pattern中的例子作为case。假如一家自行车店销售各种自行车:

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

/* Speedster class. */
var Speedster = function () { }
Speedster.prototype = {
    assemble: function () {
        console.log("Speedster Bicycle assembled.");
    },
    wash: function () {
        console.log("Speedster Bicycle washed.");
    },
    ride: function () {
        console.log("Speedster Bicycle ride.");
    },
    repair: function () {
        console.log("Speedster Bicycle repaired.");
    }
};
Speedster.prototype.constructor = Speedster;

/* Lowrider class. */
var Lowrider = function () { }
Lowrider.prototype = {
    assemble: function () {
        console.log("Lowrider Bicycle assembled.");
    },
    wash: function () {
        console.log("Lowrider Bicycle washed.");
    },
    ride: function () {
        console.log("Lowrider Bicycle ride.");
    },
    repair: function () {
        console.log("Lowrider Bicycle repaired.");
    }
};
Lowrider.prototype.constructor = Lowrider;

/* ComfortCruiser class. */
var ComfortCruiser = function () { }
ComfortCruiser.prototype = {
    assemble: function () {
        console.log("ComfortCruiser Bicycle assembled.");
    },
    wash: function () {
        console.log("ComfortCruiser Bicycle washed.");
    },
    ride: function () {
        console.log("ComfortCruiser Bicycle ride.");
    },
    repair: function () {
        console.log("ComfortCruiser Bicycle repaired.");
    }
};
ComfortCruiser.prototype.constructor = ComfortCruiser;

/* 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();
                break;
        }

Interface.ensureImplements(bicycle, [Bicycle]);
        bicycle.assemble();
        bicycle.wash();

return bicycle;
    }
};
BicycleShop.prototype.constructor = BicycleShop;

/* test */
(function () {
    var bicycle = new BicycleShop().sellBicycle("The Lowrider");
    bicycle.ride();
    bicycle.repair();

})();

其中,

Bicycle为自行车接口,之后3个类为类型的自行车,在BicycleShop的SellBicycle中是先根据条件创建具体自行车型,然后组装清洗。

改进第一步是将上述SellBicycle方法利用Simple Factory进行改进:

/* Bicycle Factory */
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();
                break;
        }
        Interface.ensureImplements(bicycle, [Bicycle]);

return bicycle;
    }
};

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

return bicycle;
    }
};

BicycleShop.prototype.constructor = BicycleShop;

可以看到,多了一个Bicycle构造工厂, BicycleShop的职责只负责组装和清洗了。 当增加了新的车型,只需要更新BicycleFactory,不需要修改BicycleShop。

好,当车型比较少时完全可以满足需要。但是,当车店需要区分品牌和车型时就不行了。我们继续,利用Factory Method来进行改进。因为改动较大,贴出完整代码如下:

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

/* Acme Speedster class. */
var AcmeSpeedster = function () { }
AcmeSpeedster.prototype = {
    assemble: function () {
        console.log("Acme Speedster Bicycle assembled.");
    },
    wash: function () {
        console.log("Acme Speedster Bicycle washed.");
    },
    ride: function () {
        console.log("Acme Speedster Bicycle ride.");
    },
    repair: function () {
        console.log("Acme Speedster Bicycle repaired.");
    }
};
AcmeSpeedster.prototype.constructor = AcmeSpeedster;

/* Acme Lowrider class. */
var AcmeLowrider = function () { }
AcmeLowrider.prototype = {
    assemble: function () {
        console.log("Acme Lowrider Bicycle assembled.");
    },
    wash: function () {
        console.log("Acme Lowrider Bicycle washed.");
    },
    ride: function () {
        console.log("Acme Lowrider Bicycle ride.");
    },
    repair: function () {
        console.log("Acme Lowrider Bicycle repaired.");
    }
};
AcmeLowrider.prototype.constructor = AcmeLowrider;

/* Acme ComfortCruiser class. */
var AcmeComfortCruiser = function () { }
AcmeComfortCruiser.prototype = {
    assemble: function () {
        console.log("Acme ComfortCruiser Bicycle assembled.");
    },
    wash: function () {
        console.log("Acme ComfortCruiser Bicycle washed.");
    },
    ride: function () {
        console.log("Acme ComfortCruiser Bicycle ride.");
    },
    repair: function () {
        console.log("Acme ComfortCruiser Bicycle repaired.");
    }
};
AcmeComfortCruiser.prototype.constructor = AcmeComfortCruiser;

/* General Speedster class. */
var GeneralSpeedster = function () { }
GeneralSpeedster.prototype = {
    assemble: function () {
        console.log("General Speedster Bicycle assembled.");
    },
    wash: function () {
        console.log("General Speedster Bicycle washed.");
    },
    ride: function () {
        console.log("General Speedster Bicycle ride.");
    },
    repair: function () {
        console.log("General Speedster Bicycle repaired.");
    }
};
GeneralSpeedster.prototype.constructor = GeneralSpeedster;

/* General Lowrider class. */
var GeneralLowrider = function () { }
GeneralLowrider.prototype = {
    assemble: function () {
        console.log("General Lowrider Bicycle assembled.");
    },
    wash: function () {
        console.log("General Lowrider Bicycle washed.");
    },
    ride: function () {
        console.log("General Lowrider Bicycle ride.");
    },
    repair: function () {
        console.log("General Lowrider Bicycle repaired.");
    }
};
GeneralLowrider.prototype.constructor = GeneralLowrider;

/* General ComfortCruiser class. */
var GeneralComfortCruiser = function () { }
GeneralComfortCruiser.prototype = {
    assemble: function () {
        console.log("General ComfortCruiser Bicycle assembled.");
    },
    wash: function () {
        console.log("General ComfortCruiser Bicycle washed.");
    },
    ride: function () {
        console.log("General ComfortCruiser Bicycle ride.");
    },
    repair: function () {
        console.log("General ComfortCruiser Bicycle repaired.");
    }
};
GeneralComfortCruiser.prototype.constructor = GeneralComfortCruiser;

/* BicycleShop class. */
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("BicycleShop is abstract, not implements createBicycle.");
    }
};
BicycleShop.prototype.constructor = BicycleShop;

/* Acme Bicycle Shop */
var AcmeBicycleShop = function () { };
extend(AcmeBicycleShop, BicycleShop);
AcmeBicycleShop.prototype.createBicycle = function (model) {
    var bicycle;
    switch (model) {
        case "The Speedster":
            bicycle = new AcmeSpeedster();
            break;
        case "The Lowrider":
            bicycle = new AcmeLowrider();
            break;
        case "The Comfort Cruiser":
        default:
            bicycle = new AcmeComfortCruiser();
            break;
    }
    Interface.ensureImplements(bicycle, [Bicycle]);

return bicycle;
};

/* General Bicycle Shop */
var GeneralBicycleShop = function () { };
extend(GeneralBicycleShop, BicycleShop);
GeneralBicycleShop.prototype.createBicycle = function (model) {
    var bicycle;
    switch (model) {
        case "The Speedster":
            bicycle = new GeneralSpeedster();
            break;
        case "The Lowrider":
            bicycle = new GeneralLowrider();
            break;
        case "The Comfort Cruiser":
        default:
            bicycle = new GeneralComfortCruiser();
            break;
    }
    Interface.ensureImplements(bicycle, [Bicycle]);

return bicycle;
};

/* test */
(function () {
    var bicycle = new GeneralBicycleShop().sellBicycle("The Lowrider");
    bicycle.ride();
    bicycle.repair();

})();

重点在于,我们将

BicycleShop变更为一个抽象类,即将CreateBicycle()方法作为抽象方法。然后,AcmeBicycleShop 和 GeneralBicycleShop作为2个品牌的自行车店均继承于它,各自实现CreateBicycle()方法。

另外,附件还附有2个例子,download

转载于:https://www.cnblogs.com/Langzi127/archive/2012/09/20/2696103.html

Javascript中Factory的应用相关推荐

  1. JavaScript中的工厂函数vs构造函数vs class

    原文链接:JavaScript Factory Functions vs Constructor Functions vs Classes 作者:Eric Elliott 译者:sunny 转载需提前 ...

  2. javascript中的闭包closure详解

    文章目录 简介 函数中的函数 Closure闭包 使用闭包实现private方法 闭包的Scope Chain 闭包常见的问题 闭包性能的问题 总结 简介 闭包closure是javascript中一 ...

  3. Javascript中的AES加密和Java中的解密

    AES代表高级加密系统,它是一种对称加密算法,很多时候我们需要在客户端加密一些纯文本(例如密码)并将其发送到服务器,然后由服务器解密以进行进一步处理.AES加密和解密更加容易在相同的平台(例如Andr ...

  4. 现代JavaScript中的精美图案:制冰厂

    I've been working with JavaScript on and off since the late nineties. I didn't really like it at fir ...

  5. JavaScript 中的设计模式

    目录 1. 单例模式 2. 策略模式 3. 代理模式 4. 装饰者模式 5. 组合模式 6. 工厂模式 7. 访问者模式 8. 发布订阅模式 9. 观察者模式 10. 参考链接 设计模式(Design ...

  6. JavaScript 中的模块化

    封面图说明:© Michael J. Kochniss | mjk-photo.de | instagram.com/mjk_photo 在早期,JavaScript 程序主要用来实现一些页面上的动画 ...

  7. 理解JavaScript中部分设计模式

    理解JavaScript中部分设计模式 什么是设计模式 在软件工程中,设计模式是软件设计中常见问题可重用的方案.设计模式代表着经验丰富的软件开发人员使用的最佳实践.设计模式可以被认为是编程模板. 为什 ...

  8. 浅析 JavaScript 中的 函数 uncurrying 反柯里化

    柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函数接受剩下的参数,这中间可嵌套多层这样的接受部分参数函数,直至返回最后结果. 因此柯里化的过程是 ...

  9. JavaScript中,this的绑定规则

    对于 JavaScript 新手来说,this 是非常基础同时也难以理解的知识点. 比如下面的代码,this 指向就有三种方式. 在<你不知道的 JavaScript>一书中,我总算比较清 ...

最新文章

  1. UC伯克利开源照片“隐写术”StegaStamp,打印照片能当二维码用!| 技术头条
  2. Docker hello workd
  3. javaScript语法基础
  4. python 分类变量xgboost_用于可解释机器学习的四个Python库
  5. 三角函数公式大全(速查手册)
  6. IEEE COMMUNICATIONS LETTERS 写作Latex模板
  7. c语言编程星号输出图形的步骤,使用C语言打印不同星号图案
  8. tas5707php,TAS5707PHPR 立体声数字音频功率放大器
  9. 【隐私计算笔谈】MPC系列专题(五):Beaver三元组和BMR协议
  10. 0.《新概念51单片机C语言教程》(郭天祥)学习笔记
  11. 计算机休眠后黑屏打不开,电脑待机后黑屏打不开怎么办
  12. 短信与社交app的好处
  13. 如何查看Eclipse是32位还是64位?
  14. 图片搜索引擎 - WebCrawler
  15. matlab中wav转txt6,WAV转TXT专家下载
  16. 通达信股票交易数据接口--API量化交易
  17. 江湖聊天室php,workerman+thinkphp制作简易聊天室
  18. OpenCV截取图像的某一区域
  19. TDSQL是什么:腾讯如何打造一款金融级分布式数据库
  20. stm32 f103c8t6系列之 iic模式 点亮096 oled

热门文章

  1. day36 excel入门
  2. stdlib 标准C 模板库函数
  3. visual studio客户端windows模式下调出cmd命令行
  4. 链表(2)----双链表基本操作
  5. ESP01s + MQTT + uni-app(Vue) 入门物联网继电器开关实践
  6. 华科计算机专业考研专业课难度,我来谈谈我所经历的华中科技大学计算机考研复试...
  7. Redis + Caffeine实现多级缓存
  8. dbeaver7闪退解决方案
  9. VS创建Qt工程,INK : fatal error LNK1181: 无法打开输入文件“xx.lib”
  10. 1秒内通关扫雷?他创造属于自己的世界记录!Python实现自动扫雷