Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
I don't understand in StateA.Bar replace Foo's state to StateB. Do you mean that the object StateA gets replaced by the object StateB? –  Robert Harvey Aug 29 '12 at 17:44
    
@Robert Harvey, correct. StateA and StateB both implement the same interface and Foo stores a handle for an object using this interface. –  Chris C Aug 29 '12 at 17:46
    
Well, it sounds like a modified version of a Factory pattern, but a concrete implementation replacing itself with another concrete implementation seems a bit strange. –  Robert Harvey Aug 29 '12 at 17:47
    
Chris is right that it's a specific use of the State pattern, but Robert is right that it's an unusual one. It seems slightly overengineered to me. Surely if you're going to do that then Foo should set the State to StateB every time, or something. Either way, it's odd enough that I doubt it has a name. –  pdr Aug 29 '12 at 17:50
    
@pdr It was mostly planned to replace writing something like if Foo.initialized, do action A, else do action B and needing to check for that every time the loop runs. The way Foo is set up I cannot do action A anywhere but inside the loop. –  Chris C Aug 29 '12 at 18:08
show 4 more comments

1 Answer

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...

share|improve this answer
1  
@gnat, Outstanding improvement, thank you. –  Wayne Conrad Aug 31 '12 at 12:23
    
I recall someone posted an answer saying this could be used in a video game but he deleted it. He would be right in his assumption- my actual use case is for a game, but I considered my question more of a general programming which is why it's here and not Gamedev.SE. –  Chris C Aug 31 '12 at 15:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.