赞
踩
工厂模式是一种创建型设计模式,它可以帮助我们根据需求创建对象。
const BicycleShop = function () {};
BicycleShop.prototype = {
sellBicycle(model) {
let bicycle;
switch (model) {
case "BMW":
bicycle = new BMW();
break;
case "BYD":
bicycle = new BYD();
break;
default:
bicycle = new YaDi();
}
return bicycle;
},
};
function BMW() {
this.name = "BMW";
}
BMW.prototype = {
run() {
console.log("BMW 正在运行");
},
};
function BYD() {
this.name = "BYD";
}
BYD.prototype = {
run() {
console.log("BYD 正在运行");
},
};
function YaDi() {
this.name = "YaDi";
}
YaDi.prototype = {
run() {
console.log("YaDi 正在运行");
},
};
//出手宝马
const bicycleShop = new BicycleShop();
bicycleShop.sellBicycle("BMW").run();
// 升级版
const BicycleShop1 = function () {};
const BicycleFactory = {
createBicycle(model) {
let bicycle;
switch (model) {
case "BMW":
bicycle = new BMW();
break;
case "BYD":
bicycle = new BYD();
break;
case "LuYuan":
bicycle = new LuYuan();
break;
default:
bicycle = new YaDi();
}
return bicycle;
},
};
BicycleShop1.prototype = {
sellBicycle(model) {
let bicycle = BicycleFactory.createBicycle(model);
return bicycle;
},
};
//此时我们要添加新的车型,就不用改变BicycleShop1,只需要在BicycleFactory中添加即可
function LuYuan() {
this.name = "LuYuan";
}
LuYuan.prototype = {
run() {
console.log("LuYuan 正在运行");
},
};
//出手luyuan
const bicycleShop1 = new BicycleShop1();
bicycleShop1.sellBicycle("LuYuan").run();
const Bicycle = function () {};
Bicycle.prototype = {
sellBicycle(model) {
const bicycle = this.createBycicle(model);
return bicycle;
},
createBycicle(model) {
throw new Error("子类必须实现createBycicle方法");
},
};
const BWMBicycle = function () {};
extend(BWMBicycle, Bicycle);
BWMBicycle.prototype.createBycicle = function (model) {
let bicycle;
switch (model) {
case "BWM":
bicycle = new BMW();
break;
case "Yadi":
bicycle = new YaDi();
break;
default:
throw new Error("没有该型号的自行车");
}
return bicycle;
};
const YADIBicycle = function () {};
extend(YADIBicycle, Bicycle);
YADIBicycle.prototype.createBycicle = function (model) {
let bicycle;
switch (model) {
case "BWM":
bicycle = new BMW();
break;
case "Yadi":
bicycle = new YaDi();
break;
default:
throw new Error("没有该型号的自行车");
}
return bicycle;
};
function BMW() {
this.name = "BMW";
}
BMW.prototype = {
run() {
console.log("BMW 正在运行");
},
};
function BYD() {
this.name = "BYD";
}
BYD.prototype = {
run() {
console.log("BYD 正在运行");
},
};
function YaDi() {
this.name = "YaDi";
}
YaDi.prototype = {
run() {
console.log("YaDi 正在运行");
},
};
function extend(subClass, superClass) {
var F = function () {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
//子类增加了一个属性,直接指向了父类的原型对象,prorotype
subClass.superclass = superClass.prototype;
//正常情况下,每个类的constructor属性都是指向自己,
//保证父类的constructor属性指向父类
if (superClass.prototype.constructor === Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
const bwm = new BWMBicycle();
const yadi = new YADIBicycle();
const bwmBicycle = bwm.sellBicycle("BWM");
console.log("声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/844881
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。