10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

say that the Context class (aggregator) owns the Strategy interface (aggregate), just as<br />

in the State design pattern. (See Figure 10-3 in Chapter 10.) As you will see in the<br />

examples, the Context class aggregates the Strategy operations. Because aggregation<br />

is so fundamental to many of the design patterns and an integral part of OOP, look<br />

for its actual use in the examples. The Strategy design pattern is one of the best<br />

sources of clear examples showing aggregation at work.<br />

Key OOP Concepts Used with the Strategy Pattern<br />

David Geary (JavaWorld.<strong>com</strong>, 04/26/02) points out that the Strategy pattern embodies<br />

several of the OOP principles put forth by Gamma, Helm, Johnson and Vlissides.<br />

One of those principles is:<br />

Encapsulate the concept that varies<br />

Eric and Elisabeth Freeman (Head First <strong>Design</strong> <strong>Patterns</strong>, O’Reilly 2004) make the<br />

same point in discussing the Strategy pattern, and we’re not ones to break tradition<br />

and miss an important connection between a design pattern and an OOP concept.<br />

Encapsulating Variation<br />

In Chapter 10, you saw that with the State design pattern, the states varied in the<br />

video player, and so the State object encapsulated the state-dependent behavior, such<br />

as playing and stopping the video. With the Strategy pattern, the Strategy object<br />

encapsulates algorithms. What both these design patterns have in <strong>com</strong>mon is that<br />

the elements that change are encapsulated. (As a hint of what is encapsulated, look<br />

at the name of the class. In both the State and Strategy classes, the encapsulating<br />

object is the name of the class—check the class diagrams.)<br />

More important is why GoF suggests encapsulating those concepts that vary. Essentially,<br />

if you encapsulate those parts that vary, then, when you change the application,<br />

you’ll have fewer surprises. Encapsulated concepts allow the objects in objectoriented<br />

programming to act like objects. Rather than being sticky-glued to and<br />

dependent on other elements in a single program, encapsulated objects can act in<br />

concert with any new element added to the application, because they can vary independent<br />

of other objects.<br />

To better see what it means to encapsulate what varies, consider a script from an<br />

example in this chapter with and without encapsulation. This involves clowns and<br />

what they do in clown venues.<br />

Unencapsulated version (pseudocode)<br />

class Clown {<br />

function juggle( ) {<br />

//juggle code<br />

}<br />

400 | Chapter 11: Strategy Pattern

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!