5.2 Template Method


The problem arises when you have a range of different child classes with a common base class. You want each child class to execute some fixed sequence of methods in the same order, but you want to be able to vary what the individual calls of the sequence do.

The solution , as shown in Figure 5.3, is to place the sequence of method calls into a non-virtual templateMethod function that is not overridden by the child classes. The Template Method has calls to various virtual hookMethod calls that can be overridden.

Figure 5.3. The Template Method pattern

graphics/05fig03.gif

In the Pop Framework, the cGame::step method is a template that holds a sequence of calls to cGame methods that update and show the critters in a certain order, with certain calls that you might override located at a certain positions within the sequence. In this case the methods you might override are cGame::adjustGameParameters and cGame::gameOverMessage .

The cSprite::draw method is another example of the Template Method pattern; it has some special 'don't touch me' code nested around the virtual method cSprite::imagedraw that the sprite child classes override.



Software Engineering and Computer Games
Software Engineering and Computer Games
ISBN: B00406LVDU
EAN: N/A
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net