场景:定义一个抽象类 AbsProducer(生产商),该生产商有两个行为,一个生产,一个出售,其中生产方法为抽象方法,由具体的厂家(工厂)去实现,出售的产品均是电子产品(返回的对象为电子产品对象,即对象都要实现电子产品的接口)。其中有联想和apple两个工厂,根据传入的参数生产旗下不同的电子产品。具体代码实现如下:

  1 /*定义一个抽象类 AbsProducer(生产商),该生产商有两个行为,一个生产,一个出售,其中生产方法为抽象方法,由具体的厂家去实现*/
2 var AbsProducer = function(){};
3 AbsProducer.prototype = {
4
5 sell:function(name){
6 var product = this.create(model);
7 product.showName();
8 return product;
9 },
10 create:function(name){
11 throw new Error("抽象类不支持该操作");
12 }
13 }

联想工厂:LenovoFactory.js

 var LenovoFactory = function(){};
extend(LenovoFactory,AbsProducer);
LenovoFactory.prototype.create = function(name){
var product;
switch(name){
case "phone":
product = new LenovoPhone();
break;
case "computer":
product = new LenovoComputer();
break;
default:
product = new LenovoPad();
}
Interface.ensureImplements(product,ElectronicProduct);
product.showName();
return product;
} /*Lenovo phone class*/
function LenovoPhone(){};
LenovoPhone.prototype = {
showName:function(){
console.log("我是联想厂商生产的手机,取名为Lenovo-phone");
},
showCpu:function(){
console.log("联想手机cpu一般般咯");
},
showSysType:function(){
console.log("姑且认为联想手机系统为WP吧");
}
}; function LenovoComputer(){};
LenovoComputer.prototype = {
showName:function(){
console.log("我是联想厂商生产的电脑,型号为Y系列");
},
showCpu:function(){
console.log("联想电脑cpu,四代I7处理器");
},
showSysType:function(){
console.log("联想电脑系统为正版win7");
}
}; function LenovoPad(){};
LenovoPad.prototype = {
showName:function(){
console.log("我是联想厂商生产的平板");
},
showCpu:function(){
console.log("联想平板,cpu是多少,不是很清楚额...");
},
showSysType:function(){
console.log("联想平板系统为win8家庭版");
}
};

苹果工厂:AppleFactory.js

 var AppleFactory = function(){};
extend(AppleFactory,AbsProducer);
AppleFactory.prototype.create = function(name){
var product;
switch(name){
case "phone":
product = new Iphone();
break;
case "computer":
product = new Mac();
break;
default:
product = new IPad();
}
Interface.ensureImplements(product,ElectronicProduct);
product.showName();
return product;
}; function Iphone(){};
Iphone.prototype = {
showName:function(){
console.log("我是苹果公司生产的手机,取名为Iphone");
},
showCpu:function(){
console.log("iphone手机CPU是基于ARM架构重新设计的");
},
showSysType:function(){
console.log("iphone系统为IOS9");
}
};
function Mac(){};
Mac.prototype = {
showName:function(){
console.log("我是苹果公司生产的电脑,取名为Mac");
},
showCpu:function(){
console.log("mac cpu还不错吧");
},
showSysType:function(){
console.log("mac系统为OS X");
}
};
function IPad(){};
IPad.prototype = {
showName:function(){
console.log("我是苹果公司生产的ipad,取名为ipad");
},
showCpu:function(){
console.log("ipad cpu很厉害咯");
},
showSysType:function(){
console.log("ipad 系统为IOS系统");
}
}

调用:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>工厂模式</title>
</head>
<body>
</body>
</html>
<script type="text/javascript" src="Interface.js" ></script>
<script type="text/javascript" src="AbsProducer.js" ></script>
<script type="text/javascript" src="LenovoFactory.js" ></script>
<script type="text/javascript" src="AppleFactory.js" ></script>
<script type="text/javascript">
/*定义了一个ElectronicProduct电子产品的接口,该接口有以下几个名称*/
var ElectronicProduct = new Interface("ElectronicProduct",["showName","showCpu","showSysType"]);
//这里你想要哪个品牌的电子产品,直接new一个该品牌的工厂即可。
var factory = new AppleFactory();
var product = factory.create("phone");
product.showSysType();
product.showCpu();
</script>

公共类:Interface.js

 // Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expected exactly 2.");
} this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
}; // Static class method. Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " +
arguments.length + "arguments, but expected at least 2.");
} for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments "
+ "two and above to be instances of Interface.");
} for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. Method " + method + " was not found.");
}
}
}
}; 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;
}
}

最新文章

  1. myeclipse转到函数定义的方法去
  2. 利用dbms_metadata.get_ddl查看DDL语句
  3. [转]z-order引出的问题
  4. Android隐式启动匹配:action,category,data
  5. 控制语句(if-else+循环+switch)汇编规则
  6. 常用DOM笔记
  7. SharePoint 2010 PowerShell 系列 之 备份、还原、部署 .WSP
  8. linux IO 内核参数调优 之 原理和参数介绍
  9. 【转载】MySQL之权限管理
  10. nodejs内存溢出
  11. Ubuntu16.04更新源
  12. java数据库导入excel数据
  13. 个人博客作业Week7(阅读文章,心得体会)
  14. NSURLResponse下载
  15. git报错fatal: I don&#39;t handle protocol &#39;​https&#39;处理
  16. leetcode 167 two sum II
  17. POJ3666 线性dp_离散化_贪心
  18. Openwrt TF Card Auto Mount&amp;Check (4)
  19. Windows10中以管理员身份打开命令提示符
  20. ubuntu16.04 安装 caffe cuda 相关流程

热门文章

  1. jqurey事件 ready方法用法
  2. OpenCv:椭圆上点的计算方程
  3. js DOM 节点树 设置 style 样式属性
  4. jQuery实现菜单全选/不选
  5. javascript中的计算题
  6. javaee 用Buffered进行对象的写入和读取
  7. boost asio异步读写网络聊天程序客户端 实例详解
  8. Python学习教程(Python学习视频_Python学些路线):Day06 函数和模块的使用
  9. css image-set 让浏览器自动切换1x,2x图片
  10. AtCoder Grand Contest 021完整题解