js设计模式-工厂模式(抽象工厂)
场景:定义一个抽象类 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;
}
}
js设计模式-工厂模式(抽象工厂)的更多相关文章
- .Net简单工厂模式,工厂模式,抽象工厂模式实例
1.定义 简单工厂模式:是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现. 工厂模式:定义一个用于创建对象的接口, ...
- Head First 设计模式 --4 工厂模式 抽象工厂模式
(用到了依赖倒置原则) 我们写的代码中,有的时候可能会出现根据外面给定的不同的参数在方法中根据参数实例化不同的实例,就是会根据不同的参数会new出不同的实例.如果这么写了,这段代码会非常的脆弱,一旦出 ...
- 《Head First 设计模式》学习笔记——工厂模式 + 抽象工厂模式
设计模式 工厂模式:定义一个创建对象的接口,但由子类决定要实例化的是哪一个.工厂方法让类把实例化推迟到子类. 所谓的"决定",并非指模式同意子类本身在执行时做决定,而是指在编写创建 ...
- 设计模式 -创建型模式 ,python工厂模式 抽象工厂模式(1)
工厂模式 import xml.etree.ElementTree as etree import json class JSONConnector: def __init__(self, filep ...
- Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式
一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...
- JAVA常用设计模式(一、抽象工厂模式)
抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最 ...
- Java设计模式—工厂方法模式&抽象工厂模式
工厂方法模式与抽象工厂模式都是设计模式中重要而且常见的模式. 工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 通用类图如下: 在 ...
- [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式
注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ...
- 大话设计模式Python实现- 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类 下面是一个抽象工厂的demo: #!/usr/bin/env pyth ...
- python 设计模式之工厂模式 Factory Pattern (简单工厂模式,工厂方法模式,抽象工厂模式)
十一回了趟老家,十一前工作一大堆忙成了狗,十一回来后又积累了一大堆又 忙成了狗,今天刚好抽了一点空开始写工厂方法模式 我看了<Head First 设计模式>P109--P133 这25页 ...
随机推荐
- Java内存机制,内存地址
问题一:String str1 = "abc"; String str2 = "abc"; System.out.println(str1==str2); // ...
- 多种效果进度指示层效果iOS源码项目
该源码是一个多种效果进度指示层效果源码案例,源码KVNProgress,KVNProgress提供多种效果的进度指示层,有半透明效果,也支持全屏显示.指示层还高度支持自定义,可以按自己的需求定制.效果 ...
- dubbo之异步调用
异步调用 基于 NIO 的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小. 在 consumer.xml 中配置: <dubbo:reference ...
- PHP安装环境搭建
一. 安装PHP运行服务器 xampps-x64 二.安装PHP程序编辑软件(Zend Studo需要破解) 安装后打开,再关闭 把com.zend.php.core_10.6.0.v20140128 ...
- Mysql正则
摘自:http://www.runoob.com/mysql/mysql-regexp.html 模式 描述 ^ 匹配输入字符串的开始位置.如果设置了 RegExp 对象的 Multiline 属性, ...
- POJ_3020_最小路径覆盖
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8721 Accepted: 4330 ...
- 迭代器与index遍历
迭代器用于链式组织的序列. index用于线性组织的序列.
- day007 列表类型、元祖类型、 字典类型、 集合类型的内置方法
目录 列表数据类型的内置方法 作用 定义方式 优先掌握的方法 需要掌握的方法 元祖类型的内置方法 作用 定义方式 优先掌握的方法(参考列表方法) 字典类型的内置方法 作用 定义方式 优先掌握的方法 需 ...
- 【剑指Offer】50、数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...
- html表单练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...