Tuesday, October 20, 2009

5.2 Template Method



[ Team LiB ]










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





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.






    [ Team LiB ]



    No comments:

    Post a Comment