How do you share state between instances of a class?
static
Object subclass: #Color
instanceVariableNames: 'rgb cachedDepth...'
classVariableNames: 'ColorRegistry ComponentMask...'
package: 'Graphics-Primitives'
A class can have instance variables like any object
RPackageSet class
instanceVariableNames: 'cachePackages'
Each instance has a different value for cachePackages
WebServer class
instanceVariableNames: 'uniqueInstance'
WebServer class >> new
self error: 'Can''t create a new instance'
WebServer class >> uniqueInstance
^ uniqueInstance
ifNil: [ uniqueInstance := super new ]
Consequence:
uniqueInstance
WebServer
has its own singletonObject subclass: #WebServer
instanceVariableNames: ''
classVariableNames: 'UniqueInstance'
package: 'Web'
WebServer class >> new
self error: 'Can''t create a new instance'
WebServer class >> uniqueInstance
^ UniqueInstance
ifNil: [ UniqueInstance := super new ]
Consequence:
How and when are classes initialized?
A class is initialized
Color initialize
Color class >> initialize
"Externally, the red, green, and blue components of color are floats in the range [0.0..1.0]. Internally, they are represented as integers in the range [0..ComponentMask] packing into a small integer to save space and to allow fast hashing and equality testing."
ComponentMask := 1023.
HalfComponentMask := 512. "used to round up in integer calculations"
ComponentMax := 1023.0. "used to normalize components"
RedShift := 20.
GreenShift := 10.
BlueShift := 0.
self initializeIndexedColors.
self initializeColorRegistry.
self initializeGrayToIndexMap.
super initialize
in a class initialize
methodinitialize
/