문제

How can we initialize a class variable in Visualworks Smalltalk and destroy it after its use? I want to know about ClassVariables. NOT ClassInstanceVariables.

I am implementing Singleton pattern and here is my code

MyClass class>> aClasMethod
         aClassVariable isNil ifTrue:[
               aClassVariable := 'I am a variable'.
         ]
         ^aClassVariable.

Once the variable is created, I am not able to destroy it. i.e Next time I run my code, I see that the class variable is retaining its previous value. How can I avoid this?

I tried this: MyClass allInstances do: [:inst | inst become: nil ]. But of no use.

도움이 되었습니까?

해결책

The best way is simply to add a class method to set the class variable to nil and then call it whenever it's an appropriate time to clear it. I do this all the time with the Singleton pattern.

다른 팁

i think we usually use singleton pattern so that we only have one object. I don't know why do you want to store a string value in the class variable. Try to store a object so you will have only one copy of that specific object and store values in the instance variables. so that specific object has only some specific values. Then in future if u need to update the values stored in instance variable then make one more method updateValues and do everything in that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top