[Cuis] #when:send:to:

Dan Norton dnorton at mindspring.com
Mon Nov 16 19:50:30 CST 2015


On 16 Nov 2015 at 21:11, Juan Vuletich wrote:

> Hi Dan,
> 
> On 13/11/2015 02:58 p.m., Dan Norton wrote:
> > Hello Everyone,
> >
> > The following repo contains before and after examples of replacing
> the Dependency
> > Mechanism with the Observer Pattern:
> >
> > 	https://github.com/dhnorton/Cuis-Smalltalk-light
> >
> > Please take a look at this and tell me what needs to change. I'm
> trying to supply an example
> > of the Observer Pattern so that others might use #when:send:to:
> with less difficulty than I
> > experienced. There may be better examples but I picked one from
> the Blue Book.
> >
> > It seems that #when:send:to: is necessary but not sufficient as a
> replacement for the
> > Dependency Mechanism. In this example, it replaced #addDependent:,
> not #changed or
> > #update and I am still not comfortable with how to choose the
> parameters. This may be an
> > example of the difficulty in explaining the obvious, I'm not sure
> :-)
> >
> > The #changed method was replaced with #triggerEvent: with a symbol
> argument which is
> > referred to in one of the arguments of #when:send:to: and that's
> the only constraint AFAICT.
> >
> > The #update: method was retained, as a convenience; it also needs
> to agree with one of the
> > arguments of #when:send:to: and could have been different.
> >
> > Please tell me if any of this is wrong. Thanks.
> >
> >   - Dan
> 
> I think the idea of #when:send:to: is to use event names and actions
> that make sense to your model (and not #changed and #update:). For
> instance, in Example2>>#lights:, I would change the line:
> 
>          eachLight when: #changed send: #update: to: dependentLight
> with: self
> 
> to read:
> 
>          eachLight when: #turnedOn send: #turnOff to:
> dependentLight
> 
> And, in #turnOn, make it do:
> 
>          self triggerEvent: #turnedOn
> 
> 
> Doesn't this read better?
> 

+2
...because yes, it reads better, but there's more:

	#when:send:to: replaces #when:send:to:with:

	in Light2, #update: goes away, with a little factoring

The #triggerEvent: arg is changed to #turnedOn. This is elegant.

 - Dan




More information about the Cuis mailing list