[Cuis-dev] Gatekeeper example from "Discovering Smalltalk"

Mark Volkmann r.mark.volkmann at gmail.com
Sun Jan 26 11:56:13 PST 2025


I recently read the book "Discovering Smalltalk". I know it's quite dated
and it's based on Smalltalk/V instead of Smalltalk-80, but I still learned
a lot from it.

I implement the Gatekeeper example described on pages 428-430 in Cuis. It
demonstrates using the becomes: method to track all messages sent to a
given object. I got it working, but I had to make a small modification to
the ProtoObject instance method becomes:. I'm wondering if my change would
break other uses of this method.

I replaced the line

selfMethod = otherObjectMethod ifFalse: [

with

selfMethod ~= nil and:
[otherObjectMethod ~= nil] and:
[selfMethod ~= otherObjectMethod] :: ifTrue: [

Here's my working Gatekeeper code where I renamed that class to
HistoryProxy.

Object subclass: #HistoryProxy
    instanceVariableNames: 'history object'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Demo'

"class method"
on: anObject
    ^ self new on: anObject

"instance method"
on: anObject
    history := OrderedCollection new.
    object := anObject shallowCopy.
    anObject become: self

"instance method"
doesNotUnderstand: aMessage
    "Record the message and reroute it to the object."
    history add: aMessage.
    ^ aMessage sendTo: object.

"instance method"
restoreAndProvideHistory
    | result |
    "Extract information before mutating because it won't be available
afterwards."
    result := history.
    self become: object.
    ^ result.

Here's an example of using this:

date := Date today.
HistoryProxy on: date.
date monthName print.
date dayOfWeekName print.
date year print.
history := date restoreAndProvideHistory.
history print "an OrderedCollection(monthName dayOfWeekName year)"

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20250126/fec58876/attachment.htm>


More information about the Cuis-dev mailing list