深入理解JavaScript系列(28):设计模式之工厂模式详解
作者:junjie 时间:2024-05-06 10:08:17
介绍
与创建型模式类似,工厂模式创建对象(视为工厂里的产品)时无需指定创建对象的具体类。
工厂模式定义一个用于创建对象的接口,这个接口由子类决定实例化哪一个类。该模式使一个类的实例化延迟到了子类。而子类可以重写接口方法以便创建的时候指定自己的对象类型。
这个模式十分有用,尤其是创建对象的流程赋值的时候,比如依赖于很多设置文件等。并且,你会经常在程序里看到工厂方法,用于让子类类定义需要创建的对象类型。
正文
下面这个例子中,是应用了工厂方法对第26章构造函数模式代码的改进版本:
var Car = (function () {
var Car = function (model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
};
return function (model, year, miles) {
return new Car(model, year, miles);
};
})();
var tom = new Car("Tom", 2009, 20000);
var dudu = new Car("Dudu", 2010, 5000);
不好理解的话,我们再给一个例子:
var productManager = {};
productManager.createProductA = function () {
console.log('ProductA');
}
productManager.createProductB = function () {
console.log('ProductB');
}
productManager.factory = function (typeType) {
return new productManager[typeType];
}
productManager.factory("createProductA");
如果还不理解的话,那我们就再详细一点咯,假如我们想在网页面里插入一些元素,而这些元素类型不固定,可能是图片,也有可能是连接,甚至可能是文本,根据工厂模式的定义,我们需要定义工厂类和相应的子类,我们先来定义子类的具体实现(也就是子函数):
var page = page || {};
page.dom = page.dom || {};
//子函数1:处理文本
page.dom.Text = function () {
this.insert = function (where) {
var txt = document.createTextNode(this.url);
where.appendChild(txt);
};
};
//子函数2:处理链接
page.dom.Link = function () {
this.insert = function (where) {
var link = document.createElement('a');
link.href = this.url;
link.appendChild(document.createTextNode(this.url));
where.appendChild(link);
};
};
//子函数3:处理图片
page.dom.Image = function () {
this.insert = function (where) {
var im = document.createElement('img');
im.src = this.url;
where.appendChild(im);
};
};
那么我们如何定义工厂处理函数呢?其实很简单:
page.dom.factory = function (type) {
return new page.dom[type];
}
使用方式如下:
var o = page.dom.factory('Link');
o.url = 'http://www.cnblogs.com';
o.insert(document.body);
至此,工厂模式的介绍相信大家都已经了然于心了,我就不再多叙述了。
总结
什么时候使用工厂模式
以下几种情景下工厂模式特别有用:
1.对象的构建十分复杂
2.需要依赖具体环境创建不同实例
3.处理大量具有相同属性的小对象
什么时候不该用工厂模式
不滥用运用工厂模式,有时候仅仅只是给代码增加了不必要的复杂度,同时使得测试难以运行下去。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你如何使用Python实现二叉树结构及三种遍历
![](https://img.aspxhome.com/file/2023/1/133321_0s.png)
Python实现将Word表格嵌入到Excel中
![](https://img.aspxhome.com/file/2023/8/133858_0s.png)
实例解析js中try、catch、finally的执行规则
Python读写Json涉及到中文的处理方法
Python变量作用域LEGB用法解析
Python设计模式之桥接模式原理与用法实例分析
![](https://img.aspxhome.com/file/2023/2/125032_0s.png)
php获取客户端IP及URL的方法示例
Python3 实现文件批量重命名示例代码
![](https://img.aspxhome.com/file/2023/7/72127_0s.jpg)
python爬虫 爬取超清壁纸代码实例
![](https://img.aspxhome.com/file/2023/3/97383_0s.png)
Python编程中对文件和存储器的读写示例
模拟实现 Range 的 insertNode() 方法
[多图]新:60个国外创意404页面设计
![](https://img.aspxhome.com/file/UploadPic/up/9-404.gif)
Python+OpenCV让电脑帮你玩微信跳一跳
![](https://img.aspxhome.com/file/2023/2/66812_0s.jpg)
PHP行为型模式之责任链模式
Window.Open详解
Python的互斥锁与信号量详解
python光学仿真面向对象光学元件类的实现
pytorch 输出中间层特征的实例
Python将多个excel表格合并为一个表格
![](https://img.aspxhome.com/file/2023/8/81818_0s.jpg)