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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 11-5. Output from TestStringStrategy<br />

Summary<br />

After examining some applications using the Strategy design pattern, you can see the<br />

scope of possibilities for it as a design pattern. It’s certainly a showcase for good<br />

OOP practices. Perhaps most obvious is how it uses <strong>com</strong>position instead of inheritance<br />

to develop and use the algorithms. However, it still uses the basic OOP feature<br />

of inheritance in its concrete context classes.<br />

Furthermore, you can clearly see polymorphism in the variety of ways the different<br />

strategies are implemented. Each of the interfaces demonstrates abstraction along<br />

with the main context class. All the strategies encapsulate an algorithm, and each<br />

serves as a delegate to another class. So there you have it, the four basic elements of<br />

good OOP:<br />

• Inheritance<br />

• Polymorphism<br />

• Abstraction<br />

• Encapsulation<br />

As we saw in this chapter in the section on the key OOP practices, the Strategy<br />

design pattern demonstrates several other good OOP practices in addition to the<br />

basics.<br />

So, while we believe that the Strategy design pattern certainly demonstrates fundamental<br />

OOP practices, it’s still a distinct design pattern. Ironically, this fact is most<br />

obvious when you <strong>com</strong>pare it to the State design pattern—the pattern it’s often<br />

accused of duplicating. The State pattern encapsulates states and state contexts, and<br />

the Strategy pattern encapsulates algorithms. Both have different intents but these<br />

intentions are masked by the almost identical class diagrams. Only when you understand<br />

and use each one do the differences manifest themselves. As these differences<br />

be<strong>com</strong>e clear, you can see the actual use of the State and Strategy patterns as solutions<br />

to different kinds of problems. Since the definition of design patterns is that of<br />

design solution to recurring problems, we can see that the Strategy design pattern<br />

clearly meets this fundamental criterion.<br />

Summary | 423

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

Saved successfully!

Ooh no, something went wrong!