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.

Example 11-24. StringWork.as<br />

package<br />

{<br />

//Strategy<br />

interface StringWork<br />

{<br />

function stringer(s:String):String;<br />

}<br />

}<br />

Example 11-25. SortWork.as<br />

package<br />

{<br />

//Strategy<br />

interface SortWork<br />

{<br />

function sorter(a:Array):Array;<br />

}<br />

}<br />

Even with the added parameter and the return datatype, the interfaces are very simple.<br />

They get more interesting in their implementation.<br />

Checking strategies<br />

The first two concrete strategies (Examples 11-26 and 11-27) create algorithms using<br />

String methods. Each adds an algorithm requiring work with strings. Neither is<br />

especially sophisticated, but both are handy for certain applications that require<br />

checking strings.<br />

Example 11-26. EmailCheck.as<br />

package<br />

{<br />

//Concrete Strategy<br />

class EmailCheck implements StringWork<br />

{<br />

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

{<br />

var atPlace:int=s.indexOf("@");<br />

if (atPlace != -1 && atPlace !=0 && atPlace != (s.length-1))<br />

{<br />

return "Email address verified";<br />

} else<br />

{<br />

return "Email does not verify.\nMissing or<br />

misplaced @ sign";<br />

}<br />

}<br />

}<br />

}<br />

418 | Chapter 11: Strategy Pattern

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

Saved successfully!

Ooh no, something went wrong!