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.

Contexts for String Strategies<br />

You can think of the context classes as the clients for the strategies. They use the different<br />

strategies. Often, you will hear that a client has-a behavior. Whenever a client<br />

class delegates to a delegate class, such as the strategy classes in Figure 11-4, it has-a<br />

behavior generated by the strategy class. In this application, the context classes represent<br />

the client classes for the strategy classes.<br />

Like all context classes, the StringChecker class in Example 11-21 sets a reference to<br />

the two strategy interfaces, StringWork and SortWork. In addition, it has functions,<br />

workStrings( ) and workSorts( ) that constitute the methods used to delegate work to<br />

the strategies.<br />

Example 11-21. StringChecker.as<br />

package<br />

{<br />

//Context class<br />

class StringChecker<br />

{<br />

protected var stringWork:StringWork;<br />

protected var sortWork:SortWork;<br />

public function StringChecker( ):void<br />

{<br />

}<br />

}<br />

}<br />

public function workStrings(s:String):String<br />

{<br />

return stringWork.stringer(s);<br />

}<br />

public function workSorts(a:Array):Array<br />

{<br />

return sortWork.sorter(a);<br />

}<br />

public function setString(sw:StringWork):void<br />

{<br />

stringWork=sw;<br />

}<br />

public function setSort(sow:SortWork):void<br />

{<br />

sortWork=sow;<br />

}<br />

In addition to having reference properties and methods, the context class includes<br />

setter operations, setString( ) and setSort( ), to dynamically set strategies to<br />

instances of the context classes.<br />

Next, in Examples 11-22 and 11-23, two concrete context classes extend the primary<br />

context class. Note that they inherit the references to the strategies,<br />

416 | Chapter 11: Strategy Pattern

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

Saved successfully!

Ooh no, something went wrong!