ActionScript 3.0 Design Patterns

(Chris Devlin) #1
What is the Strategy Pattern? | 399

Key Features


When looking at the key features of the Strategy pattern, you’ll benefit from compar-


ing them with the key features of the State pattern in the previous chapter


(Chapter 10). The comparison will not only help you understand the difference


between the State and Strategy patterns, it’ll also help you better understand each


pattern in its own right.


The following key features are used in concert to create the Strategy design pattern,


but you will find the same elements when looking at good practices for just about


any object-oriented programming:



  • Define and encapsulate a family of algorithms.

  • Make encapsulated algorithms interchangeable.

  • Allow algorithms to change independently from the clients that use it.


Before going back to Chapter 10 and looking at the list of features in the State pat-


tern, take a look at the next section, “The Strategy Model.” Then make the compari-


son. Right off the bat you’ll see that the key features of the State and Strategy differ,


but by comparing the models as well, you’ll see the differences more clearly.


Because the features of the Strategy pattern are so fundamental to good OOP, we’re


going to save a more detailed discussion of them for the section, “Key OOP Con-


cepts used with the Strategy Pattern.” As you’ll see, all the features are discussed as


applicable to the more general principles of programming with OO languages such


as ActionScript 3.0.


The Strategy Model


When you look at the class diagram for the Strategy design pattern, you may be


struck by a sense of déjà vu. In Figure 10-3 in Chapter 10, the labels look different,


but the State design looks very similar. Fear not. They’re very different in intent and


implementation. So in looking at Figure 11-1, keep the Strategy design pattern key


features in mind.


Looking at the Context class in the Strategy class diagram, you can see that the Strat-


egy interface is anaggregateof the Context class. Because of this aggregation, we can


Figure 11-1. Strategy design pattern class diagram


Context
ContextInterface()

Strategy
AlgorithmInterface()

AlgorithmInterface()

ConcreteStrategyA
AlgorithmInterface() AlgorithmInterface()

ConcreteStrategyB ConcreteStrategyB
Free download pdf