ActionScript 3.0 Design Patterns

(Chris Devlin) #1
The Player Interface | 477

With a single template method, and three methods that make up the template


method within an abstract class, theRefereeclass has both flexibility and utility. In


the example application in this chapter, the implementation of theRefereeclass sub-


classesRPS. TheRPSclass provides the specific details for theRPSgame. The mea-


sure of flexibility is whether a different game could be subclassed from theReferee


class to create a whole different game.


Of the three functions that make up the template method, flexibility is most impor-


tant in the function that determines who won or if the play results in a tie. The fol-


lowing shows that function in theReferee class:


function doWinner(p1Move:String,p2Move:String):String
{
return winner;
}

Because the method resides in an abstract class, we can expect it to be overridden for


different games. It takes the move of players 1 and 2, having no idea whether the


move is by the proxy or not, and returns a string with the winner or a tie result.


Because it is abstract in a literal sense, it simply waits until one of the two moves


results in a win or tie condition. With theRPSgame, this is relatively easy because


each round of moves is a complete game.


However, what about games likeTic Tac Toe? It’s a game of several different rounds


and a wider range of outcomes. Because thedoWinnermethod includes parameters


for both players, all moves can be calculated. Each move can be described in terms of


the 3-by-3 matri xas C1R1 to C3R3 (Column#/Row#). Once either player has met


the win conditions or the play has reached a point where neither can win (a draw),


the entire template method can launch.


Alternatively, each move can be delegated to aRefereeclass, including aRefereesub-


class. This alternative is the original intention of Heliotis and Schreiner, and the flexi-


bility of the Symmetric Proxy design allows either alternative. In games with more


than two players, teams, or some other combination of individual players or teams,


only a few changes in thedoWinnermethod parameters could set it up for more com-


plex alternatives.


The key to game flexibility in the Symmetric Proxy class lies in the ability to override


the methods in the original template method in theRefereeclass. Creating multi-


player games beyond two players is quite easy as long as each has a “home” referee,


immediately sends all moves to all players and/or referees, but does not have to make


adjustments to the basic design pattern.


The Player Interface


The player interface is the starting point for creating a Symmetric Proxy design pat-


tern. The player interface contains si xmethods. As with all interfaces, it contains the


abstract methods and their parameters. Example 13-1 shows theISymPlayer interface.

Free download pdf