|
10 | 10 |
|
11 | 11 | > Ex: 計算機 |
12 | 12 |
|
13 | | - |
| 13 | + |
| 14 | + |
| 15 | +### Example |
| 16 | +https://github.com/kimi0230/DesignPatternGolang/tree/master/FactoryMethod |
| 17 | + |
| 18 | +```go |
| 19 | +/* |
| 20 | +Factory Method 工廠方法模式: 由次類別決定要建立的具象類別為何者 |
| 21 | + 定義一個用於創建對象的接口,讓子類決定實例化哪一個類 |
| 22 | + 是一種管理物件創建的模式,隨著輸入的參數不同,簡單工廠會回傳不同的物件 |
| 23 | + 使用者取得物件的時候只要傳入正確的參數,不需要去理解這個物件 |
| 24 | + 是一個使一個類的實例化延遲到其子類 |
| 25 | +此範例為 簡單工廠 |
| 26 | +*/ |
| 27 | +package factorymethod |
| 28 | + |
| 29 | +type OperationFunc interface { |
| 30 | + SetNumA(float32) |
| 31 | + SetNumB(float32) |
| 32 | + GetNumA() float32 |
| 33 | + GetNumB() float32 |
| 34 | + GetResult() (float32, bool) |
| 35 | +} |
| 36 | + |
| 37 | +type Operation struct { |
| 38 | + numberA float32 |
| 39 | + numberB float32 |
| 40 | +} |
| 41 | + |
| 42 | +func (o *Operation) SetNumA(num float32) { |
| 43 | + if o == nil { |
| 44 | + return |
| 45 | + } |
| 46 | + o.numberA = num |
| 47 | +} |
| 48 | + |
| 49 | +func (o *Operation) SetNumB(num float32) { |
| 50 | + if o == nil { |
| 51 | + return |
| 52 | + } |
| 53 | + o.numberB = num |
| 54 | +} |
| 55 | +func (o *Operation) GetNumA() float32 { |
| 56 | + if o == nil { |
| 57 | + return 0 |
| 58 | + } |
| 59 | + return o.numberA |
| 60 | +} |
| 61 | +func (o *Operation) GetNumB() float32 { |
| 62 | + if o == nil { |
| 63 | + return 0 |
| 64 | + } |
| 65 | + return o.numberB |
| 66 | +} |
| 67 | + |
| 68 | +// 加法 |
| 69 | +type OperationAdd struct { |
| 70 | + Operation |
| 71 | +} |
| 72 | + |
| 73 | +func (o *OperationAdd) GetResult() (float32, bool) { |
| 74 | + if o == nil { |
| 75 | + return 0, false |
| 76 | + } |
| 77 | + return o.numberA + o.numberB, true |
| 78 | +} |
| 79 | + |
| 80 | +// 減 |
| 81 | +type OperationSub struct { |
| 82 | + Operation |
| 83 | +} |
| 84 | + |
| 85 | +func (o *OperationSub) GetResult() (float32, bool) { |
| 86 | + if o == nil { |
| 87 | + return 0, false |
| 88 | + } |
| 89 | + return o.numberA - o.numberB, true |
| 90 | +} |
| 91 | + |
| 92 | +// 乘 |
| 93 | +type OperationMul struct { |
| 94 | + Operation |
| 95 | +} |
| 96 | + |
| 97 | +func (o *OperationMul) GetResult() (float32, bool) { |
| 98 | + if o == nil { |
| 99 | + return 0, false |
| 100 | + } |
| 101 | + return o.numberA * o.numberB, true |
| 102 | +} |
| 103 | + |
| 104 | +// 除 |
| 105 | +type OperationDiv struct { |
| 106 | + Operation |
| 107 | +} |
| 108 | + |
| 109 | +func (o *OperationDiv) GetResult() (float32, bool) { |
| 110 | + if o == nil { |
| 111 | + return 0, false |
| 112 | + } |
| 113 | + if o.numberB == 0 { |
| 114 | + return 0, false |
| 115 | + } |
| 116 | + return o.numberA / o.numberB, true |
| 117 | +} |
| 118 | + |
| 119 | +// 建立工廠 |
| 120 | +type OperationFactory struct { |
| 121 | + oper string |
| 122 | +} |
| 123 | + |
| 124 | +func (of *OperationFactory) createoperation(op string) OperationFunc { |
| 125 | + switch op { |
| 126 | + case "+": |
| 127 | + return &OperationAdd{} |
| 128 | + case "-": |
| 129 | + return &OperationSub{} |
| 130 | + case "*": |
| 131 | + return &OperationMul{} |
| 132 | + case "/": |
| 133 | + return &OperationDiv{} |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +``` |
0 commit comments