I’m looking to see if there is a particular name for this style of programming a certain kind of behavior into a program.
Said program runs in real time, in an update loop, and the program uses the State design pattern to do some work, but it’s the specific way it does the work that I want to know about.
Here’s how it’s used.
- Object Foo constructed, with concrete StateA object in it
- First loop runs
--- Foo.Run function calls StateA.Bar
--- in StateA.Bar replace Foo's state to StateB
- Second loop runs
--- Foo.Run calls StateB.Bar
- Third loop runs
--- Foo.Run calls StateB.Bar
- Fourth loop
--- etc.
So in short, Foo
doesn’t have an explicit Initialize
function. It will just have Run
, but Run
will do something unique in the first frame to initialize something for Foo
and then replace it with a different action that will repeat in all the frames following it- thus not needing to check if Foo
‘s already initialized. It’s just a “press start and go” action.
What would you call implementing this type of behavior?
9
I would call it lazy initialization implemented using the state pattern:
…lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.
This is typically accomplished by maintaining a flag indicating whether the process has taken place. Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot.
See lazy evaluation for a general treatment of this idea. In heavily imperative languages this pattern carries hidden dangers, as does any programming habit that relies on shared state…
1