author | mawalch |
Tue, 16 May 2017 10:56:48 +0200 | |
changeset 3951 | e5d3a0e5404f |
parent 3940 | d5fb7f7790eb |
child 4241 | 8c18cc3470d8 |
permissions | -rw-r--r-- |
88 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
3 |
All Rights Reserved |
88 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
3940 | 12 |
"{ Package: 'stx:libview2' }" |
88 | 13 |
|
3940 | 14 |
"{ NameSpace: Smalltalk }" |
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
15 |
|
55 | 16 |
Model subclass:#Plug |
2100 | 17 |
instanceVariableNames:'simulatedProtocol inheritedClasses' |
162 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Objects' |
|
54 | 21 |
! |
22 |
||
23 |
!Plug class methodsFor:'documentation'! |
|
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
54 | 39 |
documentation |
40 |
" |
|
228 | 41 |
A Plug is an object which simulates a protocol and evaluates |
54 | 42 |
a corresponding block when receiving messages. |
2100 | 43 |
A plug's interface can be changed dynamically. |
44 |
A plug can also be told to simulate inheriting messages from other classes, |
|
45 |
even multiple inheritance is possible. |
|
228 | 46 |
|
226 | 47 |
Its main use is for the demo doIts, to play the role of a model, |
228 | 48 |
when no actual modelClass is available for the demonstration. |
49 |
However, it can be used wherever some object is needed which responds to |
|
50 |
some protocol AND you do not want to add a class for it |
|
51 |
(lightWeight objects). |
|
52 |
||
53 |
There is a slight performance penalty - compared to `normal' objects, |
|
54 |
getting `normal' messages, though. |
|
224 | 55 |
|
3940 | 56 |
[caveat:] |
57 |
The name 'plug' was chosen because instances can be 'plugged' in anywhere. |
|
58 |
That name was invented way before the name 'mock' became popular; |
|
59 |
nowadays, it would probably be called 'PluggableMock'... |
|
60 |
||
224 | 61 |
[author:] |
62 |
Claus Gittinger |
|
228 | 63 |
|
64 |
[see also:] |
|
65 |
Model |
|
54 | 66 |
" |
67 |
! |
|
68 |
||
69 |
examples |
|
70 |
" |
|
2100 | 71 |
a simple plug: |
228 | 72 |
[exBegin] |
2100 | 73 |
|plug| |
54 | 74 |
|
2100 | 75 |
plug := Plug new. |
76 |
plug respondTo:#foo with:[Transcript showCR:'Plug received foo']. |
|
77 |
plug respondTo:#foo: with:[:arg | Transcript showCR:'Plug received foo: ', arg printString]. |
|
54 | 78 |
|
2100 | 79 |
plug foo. |
80 |
plug foo:'some argument' |
|
228 | 81 |
[exEnd] |
162 | 82 |
|
2101 | 83 |
|
360 | 84 |
using a plug as a generator (simulates a readStream): |
85 |
[exBegin] |
|
2100 | 86 |
|generator num| |
360 | 87 |
|
2100 | 88 |
num := 0. |
89 |
generator := Plug new. |
|
90 |
generator respondTo:#next with:[num := num + 1. num]. |
|
91 |
generator respondTo:#atEnd with:[false]. |
|
360 | 92 |
|
2100 | 93 |
10 timesRepeat:[ |
94 |
Transcript showCR:(generator next) |
|
95 |
] |
|
360 | 96 |
[exEnd] |
162 | 97 |
|
2101 | 98 |
|
99 |
simulating a big collection: |
|
100 |
[exBegin] |
|
101 |
|virtualList| |
|
102 |
||
103 |
virtualList := Plug new. |
|
104 |
virtualList inheritFrom:SequenceableCollection. |
|
105 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
106 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
107 |
virtualList inspect. |
|
108 |
[exEnd] |
|
109 |
||
110 |
||
162 | 111 |
simulating ``instance variables'': |
112 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
113 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 114 |
[exBegin] |
2100 | 115 |
|plug1 plug2 local1 local2| |
116 |
||
117 |
plug1 := Plug new. |
|
118 |
plug1 respondTo:#get with:[local1]. |
|
119 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
162 | 120 |
|
2100 | 121 |
plug2 := Plug new. |
122 |
plug2 respondTo:#get with:[local2]. |
|
123 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
124 |
||
125 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
|
126 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
127 |
||
128 |
plug1 set:5. |
|
129 |
plug2 set:17. |
|
162 | 130 |
|
2100 | 131 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
132 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
133 |
[exEnd] |
|
134 |
||
2101 | 135 |
|
2100 | 136 |
simulating a big list in a ListView |
137 |
(real applications would read the lines from a database or file): |
|
138 |
[exBegin] |
|
139 |
|virtualList top lv| |
|
162 | 140 |
|
2100 | 141 |
virtualList := Plug new. |
142 |
virtualList inheritFrom:SequenceableCollection. |
|
143 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
144 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
162 | 145 |
|
2100 | 146 |
top := StandardSystemView extent:200@200. |
162 | 147 |
|
2100 | 148 |
lv := ScrollableView for:ListView in:top. |
149 |
lv origin:0.0 @ 0.0 corner:1.0 @ 1.0. |
|
150 |
lv list:virtualList expandTabs:false scanForNonStrings:false includesNonStrings:false. |
|
151 |
||
152 |
top open. |
|
228 | 153 |
[exEnd] |
54 | 154 |
" |
155 |
! ! |
|
156 |
||
157 |
!Plug class methodsFor:'instance creation'! |
|
158 |
||
159 |
new |
|
232 | 160 |
^ super basicNew privateInitialize |
228 | 161 |
|
162 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 163 |
! ! |
164 |
||
137 | 165 |
!Plug methodsFor:'initialization'! |
166 |
||
232 | 167 |
privateInitialize |
228 | 168 |
"this method is NOT called `#initialize' to allow plugging that |
137 | 169 |
selector ..." |
170 |
||
171 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 172 |
|
173 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
174 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 175 |
! ! |
176 |
||
54 | 177 |
!Plug methodsFor:'message sending'! |
178 |
||
1760 | 179 |
at:key |
180 |
"catch this one - its so common" |
|
181 |
||
182 |
^ self doesNotUnderstand:(Message selector:#at: arguments:(Array with:key)) |
|
183 |
! |
|
184 |
||
185 |
at:key put:value |
|
186 |
^ self doesNotUnderstand:(Message selector:#at:put: arguments:(Array with:key with:value)) |
|
187 |
! |
|
188 |
||
54 | 189 |
doesNotUnderstand:aMessage |
228 | 190 |
"catch unhandled messages by looking in my simulated protocol |
191 |
definition; if there is some block for it, return its value. |
|
192 |
Otherwise, fall into the real doesNotUnderstand error." |
|
193 |
||
54 | 194 |
|block| |
195 |
||
2041 | 196 |
block := simulatedProtocol at:(aMessage selector) ifAbsent:[]. |
2100 | 197 |
block notNil ifTrue:[ |
198 |
^ block valueWithArguments:(aMessage arguments) |
|
54 | 199 |
]. |
2100 | 200 |
inheritedClasses notNil ifTrue:[ |
201 |
inheritedClasses do:[:eachClass | |
|
202 |
|method| |
|
203 |
||
204 |
method := eachClass lookupMethodFor:(aMessage selector). |
|
205 |
method notNil ifTrue:[ |
|
206 |
^ method valueWithReceiver:self arguments:(aMessage arguments). |
|
207 |
]. |
|
208 |
]. |
|
209 |
]. |
|
210 |
^ super doesNotUnderstand:aMessage |
|
228 | 211 |
|
212 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 213 |
! |
214 |
||
2101 | 215 |
isSequenceable |
216 |
"catch this one - its so common" |
|
217 |
||
218 |
^ self doesNotUnderstand:(Message selector:#isSequenceable arguments:#()) |
|
219 |
! |
|
220 |
||
2100 | 221 |
size |
222 |
"catch this one - its so common" |
|
223 |
||
224 |
^ self doesNotUnderstand:(Message selector:#size arguments:#()) |
|
225 |
! |
|
226 |
||
1317 | 227 |
update:something with:aParameter from:changedObject |
228 |
"catch unhandled messages by looking in my simulated protocol |
|
229 |
definition; if there is some block for it, return its value. |
|
230 |
Otherwise, fall into the real update." |
|
231 |
||
232 |
|block| |
|
233 |
||
234 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
235 |
block isNil ifTrue:[ |
|
236 |
^ super update:something with:aParameter from:changedObject |
|
237 |
]. |
|
238 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
239 |
||
240 |
! |
|
241 |
||
377 | 242 |
value |
243 |
"catch this one - its so common" |
|
244 |
||
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
245 |
|block| |
377 | 246 |
|
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
247 |
block := simulatedProtocol at:#value ifAbsent:[]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
248 |
block isNil ifTrue:[ |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
249 |
^ self |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
250 |
]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
251 |
^ block valueWithArguments:#() |
377 | 252 |
! |
253 |
||
254 |
value:arg |
|
255 |
"catch this one - its so common" |
|
256 |
||
257 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
258 |
||
259 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 260 |
! ! |
261 |
||
137 | 262 |
!Plug methodsFor:'protocol definition'! |
263 |
||
228 | 264 |
forgetAbout:aSelector |
265 |
"tell the receiver to forget about how to respond to the given by selector" |
|
266 |
||
267 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
268 |
||
269 |
" |
|
270 |
|p| |
|
271 |
||
272 |
p := Plug new. |
|
268 | 273 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
274 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 275 |
|
276 |
p foo. |
|
277 |
p foo:'hello'. |
|
278 |
||
279 |
p forgetAbout:#foo. |
|
280 |
||
281 |
p foo. |
|
282 |
" |
|
283 |
||
284 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
285 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
286 |
! |
|
287 |
||
2100 | 288 |
inheritFrom:aClass |
289 |
"very tricky - change the inheritance. |
|
290 |
This cannot be done by changing my class directly, because the instance layout |
|
291 |
must still be correct for Plugs instance variables. |
|
292 |
Therefore, the inheritance is remembered, and done dynamically in the doesNotUnderstand |
|
293 |
implementation." |
|
294 |
||
295 |
self assert:(aClass instSize == 0). |
|
296 |
inheritedClasses isNil ifTrue:[ |
|
297 |
inheritedClasses := Array with:aClass |
|
298 |
] ifFalse:[ |
|
299 |
inheritedClasses := inheritedClasses copyWith:aClass |
|
300 |
]. |
|
301 |
! |
|
302 |
||
137 | 303 |
respondTo:aSelector with:aBlock |
228 | 304 |
"tell the receiver to respond to a message given by selector, |
305 |
with evaluating aBlock. The number of arguments as defined by the |
|
306 |
selector must match the number of blockArsg expected by the block. |
|
307 |
The value returned from aBlock will be the value returned from the |
|
308 |
message." |
|
309 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
310 |
(self class includesSelector:aSelector) ifFalse:[ |
444 | 311 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 312 |
|
313 |
"/ sorry - this implementation is too quick of a hack. |
|
314 |
"/ (must be rewritten to inherit from nil, in order |
|
315 |
"/ to be able to catch more ...) |
|
316 |
||
317 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
318 |
] |
|
319 |
]. |
|
137 | 320 |
simulatedProtocol at:aSelector put:aBlock |
228 | 321 |
|
322 |
" |
|
323 |
|p| |
|
324 |
||
325 |
p := Plug new. |
|
268 | 326 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
327 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 328 |
|
329 |
p foo. |
|
330 |
p foo:'hello' |
|
331 |
" |
|
332 |
||
444 | 333 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 334 |
! ! |
335 |
||
82 | 336 |
!Plug methodsFor:'queries'! |
337 |
||
338 |
respondsTo:aSelector |
|
228 | 339 |
"return true, if the receiver responds to a message" |
340 |
||
82 | 341 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
342 |
^ super respondsTo:aSelector |
|
228 | 343 |
|
344 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 345 |
! ! |
346 |
||
137 | 347 |
!Plug class methodsFor:'documentation'! |
54 | 348 |
|
137 | 349 |
version |
3940 | 350 |
^ '$Header$' |
54 | 351 |
! ! |
3940 | 352 |