forked from bergsteiger/mindstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factories.txt
258 lines (247 loc) · 5.69 KB
/
factories.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<div dir="ltr" style="text-align: left;" trbidi="on">
 íåêîòîðîì ñìûñëå â ïðîäîëæåíèå òåìû ïîäíÿòîé òóò - <a href="http://programmingmindstream.blogspot.ru/2014/08/blog-post_85.html">Êîðîòêî. Î âîçáóæäåíèè èñêëþ÷åíèé</a><br />
<br />
Îò÷àñòè â ïðîäîëæåíèå âîò ýòîé òåìû - <a href="http://18delphi.blogspot.ru/2013/04/blog-post_7483.html">Ôàáðè÷íûé ìåòîä</a><br />
<br />
Êàê ìîæíî ñîçäàâàòü îáúåêò?<br />
<br />
Íó êîíå÷íî æå òàê:<br />
<br />
<pre class="brush:delphi">type
TmyObject = class
public
constructor Create(aSomeData: TSomeData);
end;//TmyObject
...
var
myObject : TmyObject;
...
myObject := TmyObject.Create(aSomeData);
</pre>
<br />
À ìîæíî òàê:
<br />
<br />
<pre class="brush:delphi">type
TmyObject = class
protected
constructor Create(aSomeData: TSomeData);
public
class function Make(aSomeData: TSomeData): TmyObject;
end;//TmyObject
...
class function TmyObject.Make(aSomeData: TSomeData): TmyObject;
begin
if IsValidData(aSomeData) then
Result := Self.Create(aSomeData)
else
Result := nil;
end;
...
var
myObject : TmyObject;
...
myObject := TmyObject.Make(theConcreteData);
</pre>
<br />
-- â ÷¸ì ðàçíèöà?
<br />
<br />
À â òîì, ÷òî â "ôàáðè÷íîì ìåòîäå" ìîæíî âñòàâèòü íåêîòîðóþ áèçíåñ-ëîãèêó.<br />
<br />
 íàøåì ñëó÷àå ýòî - IsValidData(aSomeData).<br />
<br />
Íî ìîæíî ïîéòè äàëüøå:
<br />
<br />
<pre class="brush:delphi">interface
...
type
TmyObject = class
protected
constructor Create(aSomeData: TSomeData);
procedure SomeMethodToOverride; virtual;
public
class function Make(aSomeData: TSomeData): TmyObject;
end;//TmyObject
...
implementation
...
TmySpecialObject = class(TmyObject)
protected
procedure SomeMethodToOverride; override;
end;//TmySpecialObject
...
class function TmyObject.Make(aSomeData: TSomeData): TmyObject;
begin
if IsMySpecialData(aSomeData) then
Result := TmySpecialObject.Create(aSomeData)
else
if IsValidData(aSomeData) then
Result := Self.Create(aSomeData)
else
Result := nil;
end;
...
var
myObject : TmyObject;
...
myObject := TmyObject.Make(theConcreteData);
</pre>
<br />
À ìîæíî ïîéòè åù¸ äàëüøå:
<br />
<br />
<pre class="brush:delphi">interface
...
type
TmyObject = class
protected
constructor Create(aSomeData: TSomeData);
procedure SomeMethodToOverride; virtual;
public
class function Make(aSomeData: TSomeData): TmyObject;
end;//TmyObject
...
implementation
...
TmyNULLObject = class(TmyObject)
protected
procedure SomeMethodToOverride; override;
end;//TmyNULLObject
TmySpecialObject = class(TmyObject)
protected
procedure SomeMethodToOverride; override;
end;//TmySpecialObject
...
class function TmyObject.Make(aSomeData: TSomeData): TmyObject;
begin
if IsMySpecialData(aSomeData) then
Result := TmySpecialObject.Create(aSomeData)
else
if IsValidData(aSomeData) then
Result := Self.Create(aSomeData)
else
Result := TmyNULLObject.Create(aSomeData);
end;
...
var
myObject : TmyObject;
...
myObject := TmyObject.Make(theConcreteData);
</pre>
<br />
Íó è ìîæíî ïîéòè è åù¸ äàëüøå:
<br />
<br />
<pre class="brush:delphi">interface
...
type
ImyInterface = interface
procedure SomeMethodToOverride;
end;//ImyInterface
TmyObject = class(TIntefacedObject, ImyInterface)
protected
constructor Create(aSomeData: TSomeData);
procedure SomeMethodToOverride; virtual;
public
class function Make(aSomeData: TSomeData): ImyInterface;
end;//TmyObject
...
implementation
...
TmyNULLObject = class(TIntefacedObject, ImyInterface)
protected
procedure SomeMethodToOverride;
// - Òóò ïîíÿòíîå äåëî override óæå íå íóæåí
constructor Create(aSomeData: TSomeData);
public
class function Make(aSomeData: TSomeData): ImyInterface;
// - à òóò âîîáùå ãîâîðÿ ìîæíî "çàáàáàõàòü ñèíãëåòîí"
end;//TmyNULLObject
TmySpecialObject = class(TmyObject)
protected
procedure SomeMethodToOverride; override;
end;//TmySpecialObject
...
class function TmyObject.Make(aSomeData: TSomeData): ImyInterface;
begin
if IsMySpecialData(aSomeData) then
Result := TmySpecialObject.Create(aSomeData)
else
if IsValidData(aSomeData) then
Result := Self.Create(aSomeData)
else
Result := TmyNULLObject.Make(aSomeData);
end;
...
var
myObject : ImyInterface;
...
myObject := TmyObject.Make(theConcreteData);
</pre>
<br />
Íó âîò ñîáñòâåííî è âñ¸.<br />
<br />
Íàäåþñü, ÷òî ýòî êîìó-íèáóäü ïîíðàâèòñÿ.
<br />
<br />
Îïÿòü æå îãîâîðþñü, ÷òî ýòî âñåãî ëèøü "ìàêåò".<br />
<br />
Äà!<br />
<br />
È åù¸ îäíà êðàòêàÿ ðåìàðêà.<br />
<br />
Êàê ñäåëàòü òàê, ÷òîáû "íå ïðîøëè ìèìî ôàáðèêè"?<br />
<br />
Ò.å. ÷òîáû íå âûçâàëè "ïàðàçèòíûé óìîë÷àòåëüíûé êîíñòðóêòîð". Êîòîðûé îò TObject.<br />
<br />
À âîò òàê:<br />
<br />
<pre class="brush:delphi">type
TmyObject = class
protected
constructor Create(aSomeData: TSomeData); overload;
public
class function Make(aSomeData: TSomeData): TmyObject;
constructor Create; overload;
end;//TmyObject
...
constructor TmyObject.Create;
begin
Assert(false, 'Íàäî âûçûâàòü ôàáðè÷íûé ìåòîä, à íå óíàñëåäîâàííûé êîíñòðóêòîð');
end;
</pre>
<br />
À ìîæíî ñäåëàòü åù¸ "âåñåëåå", âîò òàê:
<br />
<br />
<pre class="brush:delphi">type
TmyObject = class
protected
constructor InternalCreate(aSomeData: TSomeData);
public
class function Make(aSomeData: TSomeData): TmyObject;
procedure Create;
end;//TmyObject
...
procedure TmyObject.Create;
begin
Assert(false, 'Íàäî âûçûâàòü ôàáðè÷íûé ìåòîä, à íå óíàñëåäîâàííûé êîíñòðóêòîð');
end;
</pre>
<br />
-- òîãäà îøèáî÷íûé êîä âîîáùå êîìïèëèðîâàòüñÿ íå áóäåò.<br />
<br />
À ìîæíî íàâåðíîå âîîáùå òàê:
<br />
<br />
<pre class="brush:delphi">type
TmyObject = class
protected
constructor InternalCreate(aSomeData: TSomeData);
public
class function Create(aSomeData: TSomeData): TmyObject;
end;//TmyObject
</pre>
</div>