author | Claus Gittinger <cg@exept.de> |
Sat, 27 Apr 1996 16:19:26 +0200 | |
changeset 228 | 8f73cdf66b60 |
parent 226 | c5a265ceddd1 |
child 232 | b898f5762952 |
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. |
|
65 |
plug respondTo:#foo with:[Transcript showCr:'received foo']. |
|
66 |
plug respondTo:#foo: with:[:arg | Transcript showCr:'received foo: ', arg printString]. |
|
67 |
||
68 |
plug foo. |
|
69 |
plug foo:'some argument' |
|
228 | 70 |
[exEnd] |
162 | 71 |
|
72 |
||
73 |
simulating ``instance variables'': |
|
74 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
75 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 76 |
[exBegin] |
162 | 77 |
|plug1 plug2 local1 local2| |
78 |
||
79 |
plug1 := Plug new. |
|
80 |
plug1 respondTo:#get with:[local1]. |
|
81 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
82 |
||
83 |
plug2 := Plug new. |
|
84 |
plug2 respondTo:#get with:[local2]. |
|
85 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
86 |
||
87 |
Transcript show:'plug1''s value: '; showCr:plug1 get. |
|
88 |
Transcript show:'plug2''s value: '; showCr:plug2 get. |
|
89 |
||
90 |
plug1 set:5. |
|
91 |
plug2 set:17. |
|
92 |
||
93 |
Transcript show:'plug1''s value: '; showCr:plug1 get. |
|
94 |
Transcript show:'plug2''s value: '; showCr:plug2 get. |
|
228 | 95 |
[exEnd] |
54 | 96 |
" |
97 |
! ! |
|
98 |
||
99 |
!Plug class methodsFor:'instance creation'! |
|
100 |
||
101 |
new |
|
228 | 102 |
^ super basicNew __privateInitialize__ |
103 |
||
104 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 105 |
! ! |
106 |
||
137 | 107 |
!Plug methodsFor:'initialization'! |
108 |
||
228 | 109 |
__privateInitialize__ |
110 |
"this method is NOT called `#initialize' to allow plugging that |
|
137 | 111 |
selector ..." |
112 |
||
113 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 114 |
|
115 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
116 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 117 |
! ! |
118 |
||
54 | 119 |
!Plug methodsFor:'message sending'! |
120 |
||
121 |
doesNotUnderstand:aMessage |
|
228 | 122 |
"catch unhandled messages by looking in my simulated protocol |
123 |
definition; if there is some block for it, return its value. |
|
124 |
Otherwise, fall into the real doesNotUnderstand error." |
|
125 |
||
54 | 126 |
|block| |
127 |
||
128 |
block := simulatedProtocol at:aMessage selector ifAbsent:[]. |
|
129 |
block isNil ifTrue:[ |
|
228 | 130 |
^ super doesNotUnderstand:aMessage |
54 | 131 |
]. |
132 |
^ block valueWithArguments:(aMessage arguments) |
|
228 | 133 |
|
134 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
54 | 135 |
! ! |
136 |
||
137 | 137 |
!Plug methodsFor:'protocol definition'! |
138 |
||
228 | 139 |
forgetAbout:aSelector |
140 |
"tell the receiver to forget about how to respond to the given by selector" |
|
141 |
||
142 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
143 |
||
144 |
" |
|
145 |
|p| |
|
146 |
||
147 |
p := Plug new. |
|
148 |
p respondTo:#foo with:[Transcript showCr:'foo']. |
|
149 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCr:arg]. |
|
150 |
||
151 |
p foo. |
|
152 |
p foo:'hello'. |
|
153 |
||
154 |
p forgetAbout:#foo. |
|
155 |
||
156 |
p foo. |
|
157 |
" |
|
158 |
||
159 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
160 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
161 |
! |
|
162 |
||
137 | 163 |
respondTo:aSelector with:aBlock |
228 | 164 |
"tell the receiver to respond to a message given by selector, |
165 |
with evaluating aBlock. The number of arguments as defined by the |
|
166 |
selector must match the number of blockArsg expected by the block. |
|
167 |
The value returned from aBlock will be the value returned from the |
|
168 |
message." |
|
169 |
||
137 | 170 |
simulatedProtocol at:aSelector put:aBlock |
228 | 171 |
|
172 |
" |
|
173 |
|p| |
|
174 |
||
175 |
p := Plug new. |
|
176 |
p respondTo:#foo with:[Transcript showCr:'foo']. |
|
177 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCr:arg]. |
|
178 |
||
179 |
p foo. |
|
180 |
p foo:'hello' |
|
181 |
" |
|
182 |
||
183 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
137 | 184 |
! ! |
185 |
||
82 | 186 |
!Plug methodsFor:'queries'! |
187 |
||
188 |
respondsTo:aSelector |
|
228 | 189 |
"return true, if the receiver responds to a message" |
190 |
||
82 | 191 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
192 |
^ super respondsTo:aSelector |
|
228 | 193 |
|
194 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 195 |
! ! |
196 |
||
137 | 197 |
!Plug class methodsFor:'documentation'! |
54 | 198 |
|
137 | 199 |
version |
228 | 200 |
^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.11 1996-04-27 14:19:26 cg Exp $' |
54 | 201 |
! ! |