const RamenOtaku = function(type) {
if (typeof RamenOtaku[type] !== "function") {
throw {
name: "訂餐失敗",
message: "查無品項"
};
}
return new RamenOtaku[type]();
};
const RamenOtakuShiromaru = function() {
this.name = "Shiromaru Ramen";
this.taste = "good to eat";
};
RamenOtakuShiromaru.extend(RamenOtaku);
RamenOtaku.prototype.eat = function() {
return `${this.name} is ${this.taste}.`;
};
RamenOtaku.shiromaru = RamenOtakuShiromaru;
//RamenOtaku:建立工廠
//RamenOtakuShiromaru:建立產品
//產品為工廠的 extend
//工廠底下建立一個方法
//設定這個工廠底下的shiromaru為RamenOtakuShiromaru
//注意RamenOtakuShiromaru內又有自身的function
//這邊有個Extend函數,是筆者的上課老師自行定義,故在這邊沒有顯示。
//Definition of class
class User {
constructor(typeOfUser){
this._canEditEverything = false;
if (typeOfUser === "administrator") {
this._canEditEverything = true;
}
} get canEditEverything() { return this._canEditEverything; }
}
//Instatiation
let u1 = new User("normalGuy");
let u2 = new User("administrator");
//Class
class User {
constructor(){
this._canEditEverything = false;
}
get canEditEverything() { return this._canEditEverything; }
}
//Sub-class
class Administrator extends User {
constructor() {
super();
this._canEditEverything = true;
}
}
//Instatiation
let u2 = new Administrator();
let result = u2.canEditEverything; //true
class PorscheCar {
constructor(params) {
this.engine = params.engine;
this.tyreModel = params.tyreModel;
this.gears = params.gears;
}
}
class PorscheCarFactory {
createCar(params) {
return new PorscheCar(params);
}
}
class FerrariCar {
constructor(params) {
this.engine = params.engine;
this.tyreModel = params.tyreModel;
this.gears = params.gears;
this.turbo = params.turbo;
}
}
class FerrariCarFactory {
createCar(params) {
return new FerrariCar(params);
}
}
class CarFactoryImpl {
constructor() {
this.porscheCar = new PorscheCarFactory();
this.ferrariCar = new FerrariCarFactory();
}
getPorsche() {
return this.porscheCar;
}
getFerrari() {
return this.ferrariCar;
}
}
const fact = new CarFactoryImpl();
let params = { engine: "raw", tyreModel: "basic", gears: null };
const concretePorsche = fact.getPorsche().createCar(params);
console.log(concretePorsche);
params = { engine: "rawraw", tyreModel: "basic", gears: null, turbo: true };
const concreteFerrari = fact.getFerrari().createCar(params);
console.log(concreteFerrari.turbo);