场景:定义一个抽象类 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设计模式-工厂模式(抽象工厂)的更多相关文章

  1. .Net简单工厂模式,工厂模式,抽象工厂模式实例

    1.定义   简单工厂模式:是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现. 工厂模式:定义一个用于创建对象的接口, ...

  2. Head First 设计模式 --4 工厂模式 抽象工厂模式

    (用到了依赖倒置原则) 我们写的代码中,有的时候可能会出现根据外面给定的不同的参数在方法中根据参数实例化不同的实例,就是会根据不同的参数会new出不同的实例.如果这么写了,这段代码会非常的脆弱,一旦出 ...

  3. 《Head First 设计模式》学习笔记——工厂模式 + 抽象工厂模式

    设计模式 工厂模式:定义一个创建对象的接口,但由子类决定要实例化的是哪一个.工厂方法让类把实例化推迟到子类. 所谓的"决定",并非指模式同意子类本身在执行时做决定,而是指在编写创建 ...

  4. 设计模式 -创建型模式 ,python工厂模式 抽象工厂模式(1)

    工厂模式 import xml.etree.ElementTree as etree import json class JSONConnector: def __init__(self, filep ...

  5. Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式

    一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...

  6. JAVA常用设计模式(一、抽象工厂模式)

    抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最 ...

  7. Java设计模式—工厂方法模式&抽象工厂模式

    工厂方法模式与抽象工厂模式都是设计模式中重要而且常见的模式.       工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 通用类图如下: 在 ...

  8. [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式

    注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ...

  9. 大话设计模式Python实现- 抽象工厂模式

    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类 下面是一个抽象工厂的demo: #!/usr/bin/env pyth ...

  10. python 设计模式之工厂模式 Factory Pattern (简单工厂模式,工厂方法模式,抽象工厂模式)

    十一回了趟老家,十一前工作一大堆忙成了狗,十一回来后又积累了一大堆又 忙成了狗,今天刚好抽了一点空开始写工厂方法模式 我看了<Head First 设计模式>P109--P133 这25页 ...

随机推荐

  1. Android读写文件

    1.从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = getResour ...

  2. CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层

    前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...

  3. python 处理中文 读取数据库输出全是问号

    ref:http://www.cnblogs.com/zhoujie/archive/2013/06/07/problem1.html 1.python连接mssql数据库编码问题 python一直对 ...

  4. Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建

    一.java+selenium+firefox 1.环境准备:JDK1.8 2.安装firefox浏览器v59 3.下载驱动:https://github.com/mozilla/geckodrive ...

  5. react-draft-wysiwyg富文本

      import { EditorState, convertToRaw } from 'draft-js'; import { Editor } from 'react-draft-wysiwyg' ...

  6. String类练习统计一个字符串中大小写字母及数字字符个数

    public class StringPractice { public static void main(String[] args) { //创建一个文本扫描器 Scanner sc = new ...

  7. 基于分布式框架 Jepsen 的 X-Cluster 正确性测试

    转自:https://mp.weixin.qq.com/s/iOe1VjG1CrHalr_I1PKdKw 原创 2017-08-27 严祥光(祥光) 阿里巴巴数据库技术 1 概述 AliSQL X-C ...

  8. echarts在地图上绘制散点图(任意点)

    项目需求:在省份地图上绘制散点图,散点位置不一定是哪个城市或哪个区县,即任意点 通过查询官网文档,找到一个与需求类似的Demo:https://www.echartsjs.com/gallery/ed ...

  9. 环境变量、system(day10)

    一.环境变量 bash下的环境变量. 每个进程都默认从父进程继承环境变量 bash本身就是一个程序,这个程序运行的时候,bash进程 可以定义只能之自己这个进程中使用的变量,这种变量称为自定义变量. ...

  10. Pyhton高级-Socket

    1,UDP实现收发数据 import socket def send_msg(udp_socket): """获取键盘数据,并将其发送给对方""&qu ...