ActionScript 3.0 Design Patterns

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

Instead of using simple string comparisons, you could use regular expressions. Far


more sophisticated substring searches and comparisons are possible usingRegExp


data types and algorithms.


Sort strategies


The algorithms used for the sorting strategies (Examples 11-28, 11-29, and 11-30)


are much simpler than the simple string algorithms used in the twoStringWork


implantations. All three of the algorithms use a singleArraymethod—sort( ). How-


ever, by changing the parameters, you actually have three different kinds of sorts.


Example 11-27. PasswordVerify.as


package
{
//Concrete Strategy
class PasswordVerify implements StringWork
{
public function stringer(s:String):String
{
var pwv:String=s;
pwv=pwv.toLocaleLowerCase( );
if (pwv == "sandlight")
{
return "Welcome to Sandlight";
} else
{
return "Your password is incorrect.
Please enter again.";
}
}
}
}


Example 11-28. SimpleSort.as


package
{
//Concrete Strategy
class SimpleSort implements SortWork
{
public function sorter(a:Array):Array
{
a.sort( );
return a;


}

}

}
Free download pdf