aardio设计模式之简单的工厂模式

axuanup 3月前 534

//简单工厂模式
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);


最新回复 (6)
  • netfox 3月前
    1 2

    用美女顶贴,是对作者最大的尊重!

  • 光庆 3月前
    0 3
    同上
  • aardio 3月前
    0 4
    光庆 同上
    +1
  • zpzlj178 3月前
    0 5
  • tanzh 3月前
    0 6
    不错不错
  • Viewer8122 3月前
    0 7
    谢谢分享~!
返回