smalltalk singleton pattern: how do I initialize the instance variables?

后端 未结 1 1083
感情败类
感情败类 2021-01-11 17:17

I\'m having trouble in getting the singleton pattern to initialize a instance variable in smalltalk. (here is a link to another implementation for clarification)

thi

相关标签:
1条回答
  • 2021-01-11 18:06

    Try simpler:

    YourClass class>>singleton
    
           UniqueInstance ifNil: [UniqueInstance := self basicNew initialize].
           ^UniqueInstance
    

    then on instance side of your class implement an appropriate #initialize method, for example:

    YourClass>>initialize
    
              someInstvar := someInitalValue.
             ^self
    

    Update:: Name of the class method accessing the singleton varies, it can be #default, #current, or #singleton. I mostly use later.

    0 讨论(0)
提交回复
热议问题