ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Working with String Strategies | 415

With any application, you’re likely to encounter recurring algorithms. For this appli-


cation, we decided to include some little algorithms that we use with different appli-


cations that have to be rebuilt every time we create an application that uses them.


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


inherit the functionality. Rather, we want to use them in composition so that we


have more flexibility, and use their functionality only where we need them.


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


to come up in a lot of different applications. First, we built some simple strategies for


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


email address, and if it’s not in the first or last position in a string. The second strat-


egy is a password-checking algorithm that knocks all entries to lowercase, and then


sees if they match the correct password.


Our second strategy interface is for demonstrating different sorting possibilities using


theArrayclass andsort( )method. By changing the sort parameters, you will be able


to see which constants to use with different types of sorts and see the outcomes.


Figure 11-4 shows all of the connections between the classes.


The code in Examples 11-21 through 11-33 makes up the ActionScript files that con-


stitute this next application. The application also requires a single Flash document


file with the Document class nameTestStringStrategy.


Figure 11-4. Applying the Strategy pattern


StringChecker
stringWork StringWork
sortWork SortWork
workStrings()
workSorts()
setString()
sortSet()

StringChecker
stringWork EmailCheck()
sortWork SimpleSort

Passwork
stringWork PasswordVerify()
sortWork SortAll()

StringWork
stringer()

EmailCheck
stringer() {
//implements
checking for
@ sign in email
}

PasswordVerify
stringer() {
//implements
check password
match
}

‹‹interface››
SortWork
sorter()

SimpleSort
sorter() {
//implements
basic array
sort
}

SortAll
sorter() {
//implements
sort ignoring
case
}

‹‹interface››

SortBack
sorter() {
//implements
descending
sort ignoring
case
}
Free download pdf