aardio 簡單工廠模式

Axuanup發表於2024-05-26
 1 //calculate 簡單工廠模式
 2 
 3 //簡單工廠模式
 4 import console;
 5 
 6 //運算類
 7 class operation{
 8     ctor(){};
 9     numberA = 0;
10     numberB = 0;
11     getResult = function(){
12         var result = 0;
13         return result;  
14     }
15 }
16 
17 //加法類
18 class operationAdd{
19     ctor(){
20         this = ..operation();
21     };
22     getResult = function(){
23         var result = 0;
24         result = this.numberA + this.numberB;
25         return result;  
26     }
27     
28 }
29 
30 //減法類
31 class operationSub{
32     ctor(){
33         this = ..operation();
34     };
35     getResult = function(){
36         var result = 0;
37         result = this.numberA - this.numberB;
38         return result;  
39     }
40 }
41 
42 //乘法類
43 class operationMul{
44     ctor(){
45         this = ..operation();
46     };
47     getResult = function(){
48         var result = 0;
49         result = this.numberA * this.numberB;
50         return result;  
51     }
52 }
53 
54 //除法類
55 class operationDiv{
56     ctor(){
57         this = ..operation();
58     };
59     getResult = function(){
60         var result = 0;
61         result = this.numberA / this.numberB;
62         return result;  
63     }
64 }
65 
66 //簡單運算工廠類
67 class operationFactory{
68     ctor(operate){
69         var oper = null;
70         select(operate) {
71             case "+" {
72                 oper = ..operationAdd();
73             }
74             case "-"{
75                 oper = ..operationSub();
76             }
77             case "*" {
78                 oper = ..operationMul();
79             }
80             case "/"{
81                 oper = ..operationDiv();
82             }
83         }
84         return oper; 
85     };
86 }
87 
88 var oper = operationFactory("*")
89 
90 oper.numberA = 8;
91 
92 oper.numberB = 2;
93 
94 var result = oper.getResult()
95 
96 console.log(result)
97 
98 console.pause(true);

相關文章