场景:定义一个抽象类 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. DeltaFish 校园物资共享平台 第二次小组会议

    软工第二周小组会议 会议地点:三教讨论区 会议时间:9:00 ~ 10:00 与会人员:软工小组成员 请假人员:刘鼎乾 整理人:艾寅中 会议记录 一.小组分工 在经过一周的调研后,组长根据调研结果和对 ...

  2. MacOS 升级后pod 出现的问题

    -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad ...

  3. set statistics profile on实例

    set statistics profile on实例 1.SQL语句实例   SQL 代码   复制 SET STATISTICS PROFILE ON GO SELECT COUNT(b.[Sal ...

  4. Linux下Shell脚本输出带颜色文字

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例: echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置作用如下: 背景色为蓝色 ...

  5. 【sqli-labs】 less29 GET- Error based -Impidence mismatch -Having a WAF in front of web application (GET型基于错误的带有WAF注入)

    这关有点意思,有一点需要事先注意,这关玩的是login.php而不是默认的index.php 再注入之前需要先了解一下HPP(HTTP Parameter Pollution),详情参照这篇 http ...

  6. CDR服装设计-旗袍款式图

    在服装行业中的服装款式设计.图案设计和面料设计等方面,CorelDRAW是一款常用绘图设计软件,用CorelDRAW绘制款式图比手绘更容易表达服装结构.比例.图案.色彩等要素,服装款图主要目的是为了更 ...

  7. Python 文件操作 day2

    一.打开文件的模式1:读.写.追加 1.r读:读模式,只能读,不能写,打开不存在的文件会报错:可省略'r',因为不写默认是读模式 f = open('filename',encoding='utf-8 ...

  8. DATEPART()

    定义和用法 DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datep ...

  9. 【VIP视频网站项目二】搭建爱奇艺优酷腾讯视频官网首页轮播图效果及实现原理分析

    这个是实现的效果,基本上轮播效果和主流网站的一致,但是我也在上面优化了一些效果, 可以在线预览效果:https://vip.52tech.tech/ 目前项目代码已经全部开源:项目地址:https:/ ...

  10. vue-router 懒加载

    懒加载:也叫延迟加载,即在需要的时候进行加载,按需加载. 那vue 为什么需要懒加载呢? 使用 vue-cli构建的项目,在默认情况下,执行 npm run build  会将所有的 js代码打包为一 ...