ActionScript 3.0 Design Patterns

(Chris Devlin) #1
The Referee | 481

Boolean variables tofalse. (Thefalsestate means that the movehas notbeen made.)


Because the method uses an array, it doesn’t care how many moves have to be reset.


It’s far more flexible and reusable than using non-array variables.


Template Method


The final method is constructed from the three methods that currently exist in the


Refereeclass. As a template method, it is locked using thefinalstatement. As a


reminder, thefinalstatement disallows any overrides of the method; however, the


methodsthat make upthe template method can be overridden, and we generally


expect that at least some methods in the template method will be.


final function moveComplete(p1Move,p2Move,displayWindow,movecheck):void
{
outcome=doWinner(p1Move,p2Move);
displayResults(displayWindow,outcome);
resetGame(movecheck);
}

In this particular template method, the first method passes the game outcome, a


String variable. This variable is then used as a parameter in the second method to


display the outcome to a specified output object. Finally, the template method resets


the game to the start state. Simplified, the template does the following:



  1. Determines who won or if it’s a tie, and places that information in a variable.

  2. Displays the game outcome.

  3. Resets the game to play again.


By invoking theReferee, all the information is neatly packaged and ready to resolve


the game outcome, display the results, and reset the game.


RPS Subclass


TheRefereeclass is set up to be subclassed and its methods overridden so that devel-


opers can reuse the design pattern for more than a single type of game. Example 13-3


shows theRefereesubclass,RPS, designed to determine the outcome of aRock,


Paper,Scissors game.


Example 13-3. RPS.as


package
{
//Rock, Paper, Scissors
public class RPS extends Referee
{
private var winner:uint;
private var gameOver:Array;
private var winNow:String;
//

Free download pdf