aardio设计模式之简单工厂模式
本帖最后由 axuanup 于 2024-1-23 09:32 编辑//简单工厂模式
import console;
//运算类
class operation{
ctor(){};
numberA = 0;
numberB = 0;
getResult = function(){
var result = 0;
return result;
}
}
//加法类
class operationAdd{
ctor(){
this = ..operation();
};
getResult = function(){
var result = 0;
result = this.numberA + this.numberB;
return result;
}
}
//减法类
class operationSub{
ctor(){
this = ..operation();
};
getResult = function(){
var result = 0;
result = this.numberA - this.numberB;
return result;
}
}
//乘法类
class operationMul{
ctor(){
this = ..operation();
};
getResult = function(){
var result = 0;
result = this.numberA * this.numberB;
return result;
}
}
//除法类
class operationDiv{
ctor(){
this = ..operation();
};
getResult = function(){
var result = 0;
result = this.numberA / this.numberB;
return result;
}
}
//简单运算工厂类
class operationFactory{
ctor(operate){
var oper = null;
select(operate) {
case "+" {
oper = ..operationAdd();
}
case "-"{
oper = ..operationSub();
}
case "*" {
oper = ..operationMul();
}
case "/"{
oper = ..operationDiv();
}
}
return oper;
};
}
var oper = operationFactory("*")
oper.numberA = 8;
oper.numberB = 2;
var result = oper.getResult()
console.log(result)
console.pause(true);
页:
[1]