Plug.st
author Claus Gittinger <cg@exept.de>
Tue, 23 Apr 1996 13:06:56 +0200
changeset 210 5405de794686
parent 162 cbbd2f3422a3
child 224 fe3164373601
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Model subclass:#Plug
	instanceVariableNames:'simulatedProtocol'
	classVariableNames:''
	poolDictionaries:''
	category:'Kernel-Objects'
!

!Plug class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    A Plug is an objcet which simulates a protocol and evaluates
    a corresponding block when receiving messages.
    Its main use is for the demo doIts, to play the role of a model.
"
!

examples
"
    |plug|

    plug := Plug new.
    plug respondTo:#foo  with:[Transcript showCr:'received foo'].
    plug respondTo:#foo: with:[:arg | Transcript showCr:'received foo: ', arg printString].

    plug foo.
    plug foo:'some argument'


  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 ..)

    |plug1 plug2 local1 local2|

    plug1 := Plug new.
    plug1 respondTo:#get  with:[local1].
    plug1 respondTo:#set: with:[:arg | local1 := arg].

    plug2 := Plug new.
    plug2 respondTo:#get  with:[local2].
    plug2 respondTo:#set: with:[:arg | local2 := arg].

    Transcript show:'plug1''s value: '; showCr:plug1 get.
    Transcript show:'plug2''s value: '; showCr:plug2 get.

    plug1 set:5.
    plug2 set:17.

    Transcript show:'plug1''s value: '; showCr:plug1 get.
    Transcript show:'plug2''s value: '; showCr:plug2 get.
"
! !

!Plug class methodsFor:'instance creation'!

new
    ^ super basicNew privateInitialize
! !

!Plug methodsFor:'initialization'!

privateInitialize
    "this method is NOT called initialize to allow plugging that
     selector ..."

    simulatedProtocol := IdentityDictionary new.
! !

!Plug methodsFor:'message sending'!

doesNotUnderstand:aMessage
    |block|

    block := simulatedProtocol at:aMessage selector ifAbsent:[].
    block isNil ifTrue:[
	^ super doesNotUnderstand:aMessage
    ].
    ^ block valueWithArguments:(aMessage arguments)
! !

!Plug methodsFor:'protocol definition'!

respondTo:aSelector with:aBlock
    simulatedProtocol at:aSelector put:aBlock
! !

!Plug methodsFor:'queries'!

respondsTo:aSelector
    (simulatedProtocol includesKey:aSelector) ifTrue:[^ true].
    ^ super respondsTo:aSelector
! !

!Plug class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.8 1996-02-10 13:11:10 cg Exp $'
! !