Re: Clean Object Class Design -- Circle/Ellipse

From: Dave Harris <brangdon_at_cix.co.uk>
Date: Mon, 8 Oct 2001 21:28 +0100 (BST)
Message-ID: <memo.20011008212857.61375A_at_brangdon.madasafish.com>


bbadour_at_golden.net (Bob Badour) wrote (abridged):
> How does Smalltalk:
> * allow me to declare circle as a subtype of ellipse?
> * allow me to send the setFoci message to ellipse variables (that
> incidentally contain circle values) with two different focal points?
> * prevent me from sending the setFoci message to circle variables?

If we need Circle to be a subclass of Ellipse, we probably need to make them immutable. So instead of setFoci we would have withFoci. Like:

    Circle>>withFocii: aPoint and: anotherPoint

        ^aPoint = anotherPoint
           ifTrue: [Circle
                newCenter: aPoint
                radius: self radius]
           ifFalse: [Ellipse
              newFocii: aPoint
              and: anotherPoint
              radius: self radius]

This returns a new object of the appropriate class, fully initialised. Once created, there is no public protocol to change its focii so a circle stays a circle.

Instances of Circle are what you call variables, although that often doesn't matter. One potential issue is that, by default, circles can have the same centre and radius but different identities. In practice, identity is less important for objects which cannot be changed. If it matters, we can fix it by using a variant of the Singleton Pattern.

  Dave Harris, Nottingham, UK | "Weave a circle round him thrice,

      brangdon_at_cix.co.uk      |   And close your eyes with holy dread,
                              |  For he on honey dew hath fed
 http://www.bhresearch.co.uk/ | And drunk the milk of Paradise." Received on Mon Oct 08 2001 - 22:28:06 CEST

Original text of this message