ActionScript 3.0 Design Patterns

(Chris Devlin) #1

422 | Chapter 11: Strategy Pattern


Keep in mind that the call to the concrete class methods,workStringsandworkSorts,


are calls that are delegated to the strategy classes. After opening a new Flash docu-


ment file and placingTextStringStratein the Document class window, test the


application. Figure 11-5 shows what you can expect to see.


Also, note that after the dynamic changes were reset using setString( ) and


setSort( ),the output changes. Even though adding the setter is optional for a true


Strategy design pattern, setters add a whole other level of flexibility and should not


be overlooked.


{

doText( );


//Stringer -- Uses Checker concrete context
var stringer:StringChecker= new Checker( );
showText.text="Stringer\n\n"+stringer.workStrings
("[email protected]");
var friends:Array=new Array("John","Carl","aYo","Delia");
showText.appendText("\n"+stringer.workSorts(friends));
stringer.setString(new PasswordVerify( ));
showText.appendText("\n"+stringer.workStrings("Sandlight"));


//Passer -- Uses Passwork concrete context
var passer:StringChecker= new Passwork( );
showText2.text="Passer\n\n"+passer.workStrings("Rumple");
showText2.appendText("\n"+passer.workSorts(friends));
passer.setSort(new SortBack( ));
showText2.appendText("\n"+passer.workSorts(friends));
passer.setString(new EmailCheck);
showText2.appendText
("\n"+passer.workStrings("@passGuy.com"));
}
private function doText( ):void
{
showText=new ShowText( );
showText2=new ShowText( );
textShow=new TextShow( );
showText.defaultTextFormat = textShow;
showText2.defaultTextFormat = textShow;
addChild(showText),addChild(showText2);
showText.x=50, showText2.x=270;
showText.y=100, showText2.y=100;
showText.width=200,showText2.width=200;
showText.height=150,showText2.height=150;
}
}
}


Example 11-33. TestStringStrategy.as (continued)

Free download pdf