[Cuis-dev] instance creation best practices

Mark Volkmann r.mark.volkmann at gmail.com
Thu Jun 13 05:58:02 PDT 2024


Suppose I want to define a Dog class that has the instance variables name
and breed and I want the instances to be immutable. Is this the recommended
way to define it?

Object subclass: #Dog
    instanceVariableNames: 'breed name'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyCategory'

With this class method:

name: aName breed: aBreed
    ^self new initializeName: aName breed: aBreed

And with these instance methods:

initializeName: aName breed: aBreed
    name := aName.
    breed := aBreed

breed
    ^breed

name
    ^name

If I want the following to be the only way to create an instance:

Dog name: 'Comet' breed: 'Whippet'

would I do something to prevent using this?

Dog new

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


More information about the Cuis-dev mailing list