首页 / 设计模式 / 设计模式简单工厂之我见
设计模式简单工厂之我见
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了设计模式简单工厂之我见,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3252字,纯文字阅读大概需要5分钟。
内容图文
最近开始学习JavaScript的设计模式。首先接触到的是简单工厂Simple Factory,有点小小的感悟。归结起来就是:明明可以 new A(),为什么偏偏要 createObject("A") 呢?
关于简单工厂这种设计模式,网上有很多文章讲了实现思路。用JavaScript可以实现如下:
/* * 版本1:普通实现 ************************ */ ( function v1() { // 具体类A function A() { } // 具体类B function B() { } // 对象创建函数(工厂函数) function createObject(tag) { switch (tag) { case "A": returnnew A(); case "B": returnnew B(); default: return {}; } } // 客户代码 -----------------------------------var a = createObject("A"); assert(a instanceof A); var b = createObject("B"); assert(b instanceof B); })();
问题来了:这里的代码明明可以 new A(),为什么偏偏要 createObject("A") 呢?
我认为答案在于,A、B这些类可能只存在于库代码中,并没有向客户代码开放,所以客户代码根本就不能访问到这些类。
为什么不开放呢?降低耦合度。在完成相同功能的条件下,客户代码知道得越少越好。
于是,库代码和客户代码的这种隔离,导致只能采用间接的办法来创建库中类的实例,导出一个创建函数createObject() 就是一种很简单的途径。
如下代码所示,库代码由变量library封装,而客户代码在client()中:
/* * 版本2:库代码与用户代码分离 ************************ */ ( function v2() { // 库代码 var library = (function () { // 具体类Afunction A() { } // 具体类Bfunction B() { } // 对象创建函数(工厂函数)function createObject(tag) { switch (tag) { case "A": returnnew A(); case "B": returnnew B(); default: return {}; } } return {createObject: createObject}; })(); // 用户代码 ----------------------------------- (function client() { var a = library.createObject("A");
assert(utils.typeof(a) === "A"); var b = library.createObject("B"); assert(utils.typeof(b) === "B"); })(); })();
注意,客户代码只能使用库library导出的标识符createObject(),再也不能直接使用具体类的标识符A、B了。A、B已经变成了内部类。
通过引入一层间接性,把标记耦合(类A、B)降为数据耦合(参数"A"、"B"),既有效地隐藏了库代码的具体实现,又使得客户代码可以创建库中具体类的实例。
示例中用到的工具函数:
![技术分享](/upload/getfiles/default/2022/11/11/20221111035045591.jpg)
![技术分享](/upload/getfiles/default/2022/11/11/20221111035045609.jpg)
/* * * Return the type of o as a string: * -If o is null, return "null", if o is NaN, return "nan". * -If typeof returns a value other than "object" return that value. * (Note that some implementations identify regexps as functions.) * -If the class of o is anything other than "Object", return that. * -If o has a constructor and that constructor has a name, return it. * -Otherwise, just return "Object". * */ function type(o) { var t, c, n; // type, class, name// Special case for the null value:if (o === null) return "null"; // Another special case: NaN is the only value not equal to itself:if (o !== o) return "nan"; // Use typeof for any value other than "object".// This identifies any primitive value and also functions.if ((t = typeof o) !== "object") return t; // Return the class of the object unless it is "Object".// This will identify most native objects.if ((c = classof(o)) !== "Object") return c; // Return the object‘s constructor name, if it has oneif (o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n; // We can‘t determine a more specific type, so return "Object"return "Object"; } // Return the class of an object.function classof(o) { return Object.prototype.toString.call(o).slice(8, -1); } function assert(predicate) { if (predicate) { try { thrownew Error(); } catch (e) { var loc = e.stack.replace(/Error\n/).split(/\n/)[1].replace(/^\s+|\s+$/, ""); console.info("assert passed:" + loc); } } else { try { thrownew Error(); } catch (e) { console.log("Stack:" + e.stack); loc = e.stack.replace(/Error\n/).split(/\n/)[1].replace(/^\s+|\s+$/, ""); console.error("assert failed:" + loc); } } }
原文:http://www.cnblogs.com/xxfcz/p/5280731.html
内容总结
以上是互联网集市为您收集整理的设计模式简单工厂之我见全部内容,希望文章能够帮你解决设计模式简单工厂之我见所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。