418 | Chapter 11: Strategy Pattern
Even with the added parameter and the return datatype, the interfaces are very sim-
ple. They get more interesting in their implementation.
Checking strategies
The first two concrete strategies (Examples 11-26 and 11-27) create algorithms using
Stringmethods. Each adds an algorithm requiring work with strings. Neither is
especially sophisticated, but both are handy for certain applications that require
checking strings.
Example 11-24. StringWork.as
package
{
//Strategy
interface StringWork
{
function stringer(s:String):String;
}
}
Example 11-25. SortWork.as
package
{
//Strategy
interface SortWork
{
function sorter(a:Array):Array;
}
}
Example 11-26. EmailCheck.as
package
{
//Concrete Strategy
class EmailCheck implements StringWork
{
public function stringer(s:String):String
{
var atPlace:int=s.indexOf("@");
if (atPlace != -1 && atPlace !=0 && atPlace != (s.length-1))
{
return "Email address verified";
} else
{
return "Email does not verify.\nMissing or
misplaced @ sign";
}
}
}
}