Delphi实现简单工厂模式(转载)
计算器的功能实现
实现工厂设计模式
通过面向对象的思想,进行设计,所用的编程思想为对象的三大特性:封装,继承,多态
通过实现一个简单的 计算器的功能(输入两个数,进行加,减,乘,除)
思想思路:
1.设计虚拟父类;
2.子类继承父类
3. 通过虚拟方法,进行进行重载
4.工厂类,实例化对象,采用多态
1 unit uOperation; 2 3 interface 4 5 uses Classes, inifiles; 6 7 type 8 {基础类} 9 TOperation = class(Tobject) 10 private 11 FNumberA: Double; 12 FNumberB: Double; 13 public 14 function GetResult: double; virtual; abstract; 15 property NumberA: Double read FNumberA write FNumberA; 16 property NumberB: Double read FNumberB write FNumberB; 17 constructor create; 18 destructor Destroy; override; 19 end; 20 21 {加法} 22 TAdd = class(TOperation) 23 public 24 function GetResult: double; override; 25 end; 26 27 {减法} 28 TSub = class(Toperation) 29 public 30 function GetResult: double; override; 31 end; 32 33 {乘法} 34 TMultiple = class(TOperation) 35 public 36 function GetResult: double; override; 37 end; 38 39 {除法} 40 TDev = class(TOperation) 41 public 42 function GetResult: Double; override; 43 end; 44 45 {工厂类} 46 47 TOperationFactory = class 48 private 49 FOpMap: TStringHash; 50 public 51 52 {可以 GetOperation 定义为类方法,这样就不用创建TOperationFactory 对象,可以直接使用 53 54 TOperationFactory.GetOperation 但是在本例子中,由于要创建 FOpMap,所有不能定义 类方法,根据情况灵活应用,还有 55 56 简单工厂模式,返回值是父类型} 57 function GetOperation(Op: string): TOperation; 58 constructor create; 59 destructor Destroy; override; 60 61 end; 62 63 implementation 64 65 { TOperation } 66 67 constructor TOperation.create; 68 begin 69 70 end; 71 72 destructor TOperation.Destroy; 73 begin 74 75 inherited; 76 end; 77 78 { TAdd } 79 80 function TAdd.GetResult: double; 81 begin 82 result := FNumberA + FNumberB; 83 end; 84 85 { TSub } 86 87 function TSub.GetResult: double; 88 begin 89 result := FNumberA - FNumberB; 90 end; 91 92 { TMultiple } 93 94 function TMultiple.GetResult: double; 95 begin 96 Result := FNumberA * FNumberB; 97 end; 98 99 { TDev } 100 101 function TDev.GetResult: Double; 102 begin 103 Result := 0; 104 if FNumberB <> 0 then 105 Result := FNumberA / FNumberB; 106 107 end; 108 109 { TOperationFactory } 110 111 constructor TOperationFactory.create; 112 begin 113 FOpMap := TStringHash.Create; 114 FOpMap.Add('+', 1); 115 FOpMap.Add('-', 2); 116 FOpMap.Add('*', 3); 117 FOpMap.Add('/', 4); 118 end; 119 120 destructor TOperationFactory.Destroy; 121 begin 122 FOpMap.Free; 123 inherited; 124 end; 125 126 function TOperationFactory.GetOperation(Op: string): TOperation; 127 begin 128 result := nil; 129 case FOpMap.ValueOf(Op) of 130 1: result := TAdd.create; 131 2: result := TSub.create; 132 3: result := TMultiple.create; 133 4: result := TDev.create; 134 end; 135 end; 136 end. 137 138 {使用} 139 140 unit Unit1; 141 142 interface 143 144 uses 145 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 146 Dialogs, StdCtrls; 147 148 type 149 TForm1 = class(TForm) 150 Button1: TButton; 151 Edit1: TEdit; 152 Edit2: TEdit; 153 Edit3: TEdit; 154 Label1: TLabel; 155 Label2: TLabel; 156 Label3: TLabel; 157 Label4: TLabel; 158 Edit4: TEdit; 159 procedure Button1Click(Sender: TObject); 160 private 161 { Private declarations } 162 public 163 { Public declarations } 164 end; 165 166 var 167 Form1: TForm1; 168 169 implementation 170 171 uses uOperation; 172 173 {$R *.dfm} 174 175 procedure TForm1.Button1Click(Sender: TObject); 176 var 177 op: TOperation; 178 OpFact: TOperationFactory; 179 begin 180 OpFact := TOperationFactory.create; 181 182 op := OpFact.GetOperation(Edit4.Text); 183 try 184 if op = nil then exit; 185 op.NumberA := strtointdef(Edit1.Text, -1); 186 op.NumberB := strtointdef(Edit2.Text, -1); 187 edit3.Text := FloatToStr(op.GetResult); 188 finally 189 OPFact.Free; 190 OP.Free; 191 end; 192 end; 193 194 end.