[ Team LiB ] |
5.1 StrategyThe Strategy pattern is an example of delegation, the general object-oriented idiom that was mentioned in the last chapter. Before starting, we should mention that in discussing implementations of patterns such as the Strategy pattern it will be helpful to use obvious, illustrative class names like Context Figure 5.2. The Strategy patternThe problem addressed by the Strategy pattern is when we have a range of objects, all members of the same class called, let's say, Context, and we want to be able to change the behavior of the behave method of a Context The solution is to create a Strategy The reason you need to pass the 'this' to the behavealgorithm is so that the method can use the Context If you find it useful, you can use the same name for the Context::behave() and Strategy::behavealgorithm methods. The Strategy pattern plays a role similar to the role of function pointers in old-style C programming. One of the motivations for using the Strategy pattern is to avoid having a combinatorial explosion of classes. Rather than having to derive off new subclasses for new kinds of behavior, we use the Strategy pattern to let classes 'plug-in' whatever behavior they need. In the Pop Framework, the cCritter::feellistener() The cListener::listen(cCritter *pcritter) The fact that a Strategy |
[ Team LiB ] |
No comments:
Post a Comment