javascript 簡單工廠
阿新 • • 發佈:2018-12-14
簡單工廠
簡單工廠模式(Simple Factory Pattern):又稱為靜態工廠方法(Static Factory Method)模式,它屬於類建立型模式。在簡單工廠模式中,可以根據引數的不同返回不同類的例項。簡單工廠模式專門定義一個類來負責建立其他類的例項,被建立的例項通常都具有共同的父類。
例如:
自行車商店 ,客戶要求不同的型別(編號)的車,返回不同自行車的例項
//定義一個介面類
var Interface = function (name,methods) {
if(arguments.length!=2){
throw new Error('必須輸入兩個引數,當前個數'+arguments.length);
};
this.name = name; //介面名
this.methods = []; //函式名
for(var i=0;i<methods.length;i++){
if(typeof methods[i] !== 'string'){
throw new Error('方法名引數必須為string');
}else{
this.methods.push(methods[i]);
}
}
};
//定義靜態方法,檢驗實現介面和實現類 引數object為實現類物件
Interface.ensureImplements = function (object) {
if(arguments.length<2){
throw new Error('必須輸入兩個引數,當前個數' + arguments.length);
return;
};
for(var i =1;i<arguments.length;i++){
var interface = arguments[i];
if(interface.constructor != Interface){
throw new Error("介面沒有實現");
};
for(var j=0;j<interface.methods.length;j++){
var method = interface.methods[j];
if(!object[method] || typeof object[method]!=='function'){
throw new Error("介面名:"+interface.name+"方法名:"+method+"沒找到");
}
}
}
};
//封裝的繼承的方法
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;
}
}
//自行車類
function Bicycle(name,price,type,style) {
this.name = name || "一般車";
this.price = price || 300;
this.type = type || 1103;
this.style = style;
this.getInfo =function () {
return "型號:"+this.type + "名稱:"+this.name+"售價:"+ this.price;
}
this.styleChose = function () {
return "樣式"+this.style+"種";
}
}
//好車
function GoodBicke(name,price,type,style) {
GoodBicke.superclass.constructor.call(this);
this.name = name || "超好車";
this.price = price || 10000;
this.type = type || 1102;
this.speed = 50;
this.style = style || 20;
this.run = function () {
return "最大速度"+this.speed;
}
}
//一般車
function NormalBicke(type,name,price,style) {
NormalBicke.superclass.constructor.call(this);
this.name = name || "平價車";
this.price = price || 500;
this.type = type;
this.style = style || 100;
this.speed = 20;
this.run = function () {
return "最大速度"+this.speed;
}
}
//壞車
function BadBicke(name,price,type,style) {
BadBicke.superclass.constructor.call(this);
this.name = name;
this.price = price;
this.type = type;
this.style = style;
this.speed = 10;
this.run = function () {
return "最大速度"+this.speed;
}
}
//繼承自行車類
extend(GoodBicke,Bicycle);
extend(NormalBicke,Bicycle);
extend(BadBicke,Bicycle);
/*
定義介面
* */
var Bicke = new Interface('Bicke',['run','styleChose','getInfo']);
//自行車商店類
function shop() {};
shop.prototype={
//賣自行車
sellBicke:function (type) {
var bicke = null;
switch (type){
case 1101:
bicke = new GoodBicke();
break;
case 1102:
bicke = new BadBicke('差車',100,type);
break;
default:
bicke = new NormalBicke(type);
}
Interface.ensureImplements(bicke,Bicke);
return bicke;
}
}
//測試::
var person = new shop;
var b = person.sellBicke(1101);
console.log(b.getInfo());
console.log(b.run());