--- a/Plug.st Sat Apr 27 14:42:52 1996 +0200
+++ b/Plug.st Sat Apr 27 16:19:26 1996 +0200
@@ -35,18 +35,30 @@
documentation
"
- A Plug is an objcet which simulates a protocol and evaluates
+ A Plug is an object which simulates a protocol and evaluates
a corresponding block when receiving messages.
+ A plugs interface can be changed dynamically.
+
Its main use is for the demo doIts, to play the role of a model,
- when no actual model class is available for the demo.
+ when no actual modelClass is available for the demonstration.
+ However, it can be used wherever some object is needed which responds to
+ some protocol AND you do not want to add a class for it
+ (lightWeight objects).
+
+ There is a slight performance penalty - compared to `normal' objects,
+ getting `normal' messages, though.
[author:]
Claus Gittinger
+
+ [see also:]
+ Model
"
!
examples
"
+ [exBegin]
|plug|
plug := Plug new.
@@ -55,12 +67,13 @@
plug foo.
plug foo:'some argument'
+ [exEnd]
simulating ``instance variables'':
(actually, this is somewhat expensive - the contexts locals are used for them ...)
be careful with unintended variable sharing (if plugs are created in a loop ..)
-
+ [exBegin]
|plug1 plug2 local1 local2|
plug1 := Plug new.
@@ -79,51 +92,110 @@
Transcript show:'plug1''s value: '; showCr:plug1 get.
Transcript show:'plug2''s value: '; showCr:plug2 get.
+ [exEnd]
"
! !
!Plug class methodsFor:'instance creation'!
new
- ^ super basicNew privateInitialize
+ ^ super basicNew __privateInitialize__
+
+ "Modified: 27.4.1996 / 16:16:59 / cg"
! !
!Plug methodsFor:'initialization'!
-privateInitialize
- "this method is NOT called initialize to allow plugging that
+__privateInitialize__
+ "this method is NOT called `#initialize' to allow plugging that
selector ..."
simulatedProtocol := IdentityDictionary new.
+
+ "Modified: 27.4.1996 / 16:15:45 / cg"
+ "Created: 27.4.1996 / 16:17:07 / cg"
! !
!Plug methodsFor:'message sending'!
doesNotUnderstand:aMessage
+ "catch unhandled messages by looking in my simulated protocol
+ definition; if there is some block for it, return its value.
+ Otherwise, fall into the real doesNotUnderstand error."
+
|block|
block := simulatedProtocol at:aMessage selector ifAbsent:[].
block isNil ifTrue:[
- ^ super doesNotUnderstand:aMessage
+ ^ super doesNotUnderstand:aMessage
].
^ block valueWithArguments:(aMessage arguments)
+
+ "Modified: 27.4.1996 / 16:15:34 / cg"
! !
!Plug methodsFor:'protocol definition'!
+forgetAbout:aSelector
+ "tell the receiver to forget about how to respond to the given by selector"
+
+ simulatedProtocol removeKey:aSelector ifAbsent:nil
+
+ "
+ |p|
+
+ p := Plug new.
+ p respondTo:#foo with:[Transcript showCr:'foo'].
+ p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCr:arg].
+
+ p foo.
+ p foo:'hello'.
+
+ p forgetAbout:#foo.
+
+ p foo.
+ "
+
+ "Modified: 27.4.1996 / 16:14:19 / cg"
+ "Created: 27.4.1996 / 16:19:08 / cg"
+!
+
respondTo:aSelector with:aBlock
+ "tell the receiver to respond to a message given by selector,
+ with evaluating aBlock. The number of arguments as defined by the
+ selector must match the number of blockArsg expected by the block.
+ The value returned from aBlock will be the value returned from the
+ message."
+
simulatedProtocol at:aSelector put:aBlock
+
+ "
+ |p|
+
+ p := Plug new.
+ p respondTo:#foo with:[Transcript showCr:'foo'].
+ p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCr:arg].
+
+ p foo.
+ p foo:'hello'
+ "
+
+ "Modified: 27.4.1996 / 16:14:19 / cg"
! !
!Plug methodsFor:'queries'!
respondsTo:aSelector
+ "return true, if the receiver responds to a message"
+
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true].
^ super respondsTo:aSelector
+
+ "Modified: 27.4.1996 / 16:14:41 / cg"
! !
!Plug class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.10 1996-04-26 07:12:31 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.11 1996-04-27 14:19:26 cg Exp $'
! !