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.

With any application, you’re likely to encounter recurring algorithms. For this application,<br />

we decided to include some little algorithms that we use with different applications<br />

that have to be rebuilt every time we create an application that uses them.<br />

However, we don’t want to make them into classes where the main objective is to<br />

inherit the functionality. Rather, we want to use them in <strong>com</strong>position so that we<br />

have more flexibility, and use their functionality only where we need them.<br />

For our example, we’ve taken two simple types of string and sorting issues that seem<br />

to <strong>com</strong>e up in a lot of different applications. First, we built some simple strategies for<br />

checking strings. The first strategy is designed to see if the @ symbol is placed in an<br />

email address, and if it’s not in the first or last position in a string. The second strategy<br />

is a password-checking algorithm that knocks all entries to lowercase, and then<br />

sees if they match the correct password.<br />

Our second strategy interface is for demonstrating different sorting possibilities using<br />

the Array class and sort( ) method. By changing the sort parameters, you will be able<br />

to see which constants to use with different types of sorts and see the out<strong>com</strong>es.<br />

Figure 11-4 shows all of the connections between the classes.<br />

StringChecker<br />

stringWork EmailCheck()<br />

sortWork SimpleSort<br />

StringChecker<br />

stringWork StringWork<br />

sortWork SortWork<br />

workStrings()<br />

workSorts()<br />

setString()<br />

sortSet()<br />

Passwork<br />

stringWork PasswordVerify()<br />

sortWork SortAll()<br />

Figure 11-4. Applying the Strategy pattern<br />

EmailCheck<br />

stringer() {<br />

//implements<br />

checking for<br />

@ sign in email<br />

}<br />

‹‹interface››<br />

StringWork<br />

stringer()<br />

PasswordVerify<br />

stringer() {<br />

//implements<br />

check password<br />

match<br />

}<br />

SimpleSort<br />

sorter() {<br />

//implements<br />

basic array<br />

sort<br />

}<br />

‹‹interface››<br />

SortWork<br />

sorter()<br />

SortAll<br />

sorter() {<br />

//implements<br />

sort ignoring<br />

case<br />

}<br />

SortBack<br />

sorter() {<br />

//implements<br />

descending<br />

sort ignoring<br />

case<br />

}<br />

The code in Examples 11-21 through 11-33 makes up the <strong>ActionScript</strong> files that constitute<br />

this next application. The application also requires a single Flash document<br />

file with the Document class name TestStringStrategy.<br />

Working with String Strategies | 415

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

Saved successfully!

Ooh no, something went wrong!