author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jan 2000 11:12:57 +0100 | |
changeset 1317 | 18aabf275ec2 |
parent 444 | 4ad5cc26ad6a |
child 1554 | d01ffa42ca7a |
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 |
" |
|
12 |
||
55 | 13 |
Model subclass:#Plug |
162 | 14 |
instanceVariableNames:'simulatedProtocol' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Kernel-Objects' |
|
54 | 18 |
! |
19 |
||
20 |
!Plug class methodsFor:'documentation'! |
|
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
54 | 36 |
documentation |
37 |
" |
|
228 | 38 |
A Plug is an object which simulates a protocol and evaluates |
54 | 39 |
a corresponding block when receiving messages. |
228 | 40 |
A plugs interface can be changed dynamically. |
41 |
||
226 | 42 |
Its main use is for the demo doIts, to play the role of a model, |
228 | 43 |
when no actual modelClass is available for the demonstration. |
44 |
However, it can be used wherever some object is needed which responds to |
|
45 |
some protocol AND you do not want to add a class for it |
|
46 |
(lightWeight objects). |
|
47 |
||
48 |
There is a slight performance penalty - compared to `normal' objects, |
|
49 |
getting `normal' messages, though. |
|
224 | 50 |
|
51 |
[author:] |
|
52 |
Claus Gittinger |
|
228 | 53 |
|
54 |
[see also:] |
|
55 |
Model |
|
54 | 56 |
" |
57 |
! |
|
58 |
||
59 |
examples |
|
60 |
" |
|
228 | 61 |
[exBegin] |
54 | 62 |
|plug| |
63 |
||
64 |
plug := Plug new. |
|
268 | 65 |
plug respondTo:#foo with:[Transcript showCR:'received foo']. |
66 |
plug respondTo:#foo: with:[:arg | Transcript showCR:'received foo: ', arg printString]. |
|
54 | 67 |
|
68 |
plug foo. |
|
69 |
plug foo:'some argument' |
|
228 | 70 |
[exEnd] |
162 | 71 |
|
360 | 72 |
using a plug as a generator (simulates a readStream): |
73 |
[exBegin] |
|
74 |
|generator num| |
|
75 |
||
76 |
num := 0. |
|
77 |
generator := Plug new. |
|
78 |
generator respondTo:#next |
|
79 |
with:[num := num + 1. num]. |
|
80 |
generator respondTo:#atEnd |
|
81 |
with:[false]. |
|
82 |
||
83 |
10 timesRepeat:[ |
|
84 |
Transcript showCR:(generator next) |
|
85 |
] |
|
86 |
[exEnd] |
|
162 | 87 |
|
88 |
simulating ``instance variables'': |
|
89 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
90 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 91 |
[exBegin] |
162 | 92 |
|plug1 plug2 local1 local2| |
93 |
||
94 |
plug1 := Plug new. |
|
95 |
plug1 respondTo:#get with:[local1]. |
|
96 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
97 |
||
98 |
plug2 := Plug new. |
|
99 |
plug2 respondTo:#get with:[local2]. |
|
100 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
101 |
||
268 | 102 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
103 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
162 | 104 |
|
105 |
plug1 set:5. |
|
106 |
plug2 set:17. |
|
107 |
||
268 | 108 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
109 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
228 | 110 |
[exEnd] |
54 | 111 |
" |
112 |
! ! |
|
113 |
||
114 |
!Plug class methodsFor:'instance creation'! |
|
115 |
||
116 |
new |
|
232 | 117 |
^ super basicNew privateInitialize |
228 | 118 |
|
119 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 120 |
! ! |
121 |
||
137 | 122 |
!Plug methodsFor:'initialization'! |
123 |
||
232 | 124 |
privateInitialize |
228 | 125 |
"this method is NOT called `#initialize' to allow plugging that |
137 | 126 |
selector ..." |
127 |
||
128 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 129 |
|
130 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
131 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 132 |
! ! |
133 |
||
54 | 134 |
!Plug methodsFor:'message sending'! |
135 |
||
136 |
doesNotUnderstand:aMessage |
|
228 | 137 |
"catch unhandled messages by looking in my simulated protocol |
138 |
definition; if there is some block for it, return its value. |
|
139 |
Otherwise, fall into the real doesNotUnderstand error." |
|
140 |
||
54 | 141 |
|block| |
142 |
||
143 |
block := simulatedProtocol at:aMessage selector ifAbsent:[]. |
|
144 |
block isNil ifTrue:[ |
|
228 | 145 |
^ super doesNotUnderstand:aMessage |
54 | 146 |
]. |
147 |
^ block valueWithArguments:(aMessage arguments) |
|
228 | 148 |
|
149 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 150 |
! |
151 |
||
1317 | 152 |
update:something with:aParameter from:changedObject |
153 |
"catch unhandled messages by looking in my simulated protocol |
|
154 |
definition; if there is some block for it, return its value. |
|
155 |
Otherwise, fall into the real update." |
|
156 |
||
157 |
|block| |
|
158 |
||
159 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
160 |
block isNil ifTrue:[ |
|
161 |
^ super update:something with:aParameter from:changedObject |
|
162 |
]. |
|
163 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
164 |
||
165 |
! |
|
166 |
||
377 | 167 |
value |
168 |
"catch this one - its so common" |
|
169 |
||
170 |
^ self doesNotUnderstand:(Message selector:#value arguments:#()) |
|
171 |
||
172 |
"Created: 21.1.1997 / 15:21:24 / cg" |
|
173 |
! |
|
174 |
||
175 |
value:arg |
|
176 |
"catch this one - its so common" |
|
177 |
||
178 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
179 |
||
180 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 181 |
! ! |
182 |
||
137 | 183 |
!Plug methodsFor:'protocol definition'! |
184 |
||
228 | 185 |
forgetAbout:aSelector |
186 |
"tell the receiver to forget about how to respond to the given by selector" |
|
187 |
||
188 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
189 |
||
190 |
" |
|
191 |
|p| |
|
192 |
||
193 |
p := Plug new. |
|
268 | 194 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
195 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 196 |
|
197 |
p foo. |
|
198 |
p foo:'hello'. |
|
199 |
||
200 |
p forgetAbout:#foo. |
|
201 |
||
202 |
p foo. |
|
203 |
" |
|
204 |
||
205 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
206 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
207 |
! |
|
208 |
||
137 | 209 |
respondTo:aSelector with:aBlock |
228 | 210 |
"tell the receiver to respond to a message given by selector, |
211 |
with evaluating aBlock. The number of arguments as defined by the |
|
212 |
selector must match the number of blockArsg expected by the block. |
|
213 |
The value returned from aBlock will be the value returned from the |
|
214 |
message." |
|
215 |
||
377 | 216 |
(self class implements:aSelector) ifFalse:[ |
444 | 217 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 218 |
|
219 |
"/ sorry - this implementation is too quick of a hack. |
|
220 |
"/ (must be rewritten to inherit from nil, in order |
|
221 |
"/ to be able to catch more ...) |
|
222 |
||
223 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
224 |
] |
|
225 |
]. |
|
137 | 226 |
simulatedProtocol at:aSelector put:aBlock |
228 | 227 |
|
228 |
" |
|
229 |
|p| |
|
230 |
||
231 |
p := Plug new. |
|
268 | 232 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
233 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 234 |
|
235 |
p foo. |
|
236 |
p foo:'hello' |
|
237 |
" |
|
238 |
||
444 | 239 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 240 |
! ! |
241 |
||
82 | 242 |
!Plug methodsFor:'queries'! |
243 |
||
244 |
respondsTo:aSelector |
|
228 | 245 |
"return true, if the receiver responds to a message" |
246 |
||
82 | 247 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
248 |
^ super respondsTo:aSelector |
|
228 | 249 |
|
250 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 251 |
! ! |
252 |
||
137 | 253 |
!Plug class methodsFor:'documentation'! |
54 | 254 |
|
137 | 255 |
version |
1317 | 256 |
^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.17 2000-01-25 10:12:57 cg Exp $' |
54 | 257 |
! ! |