场景:定义一个抽象类 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;
}
}