Delegation! Since it's clear that extracting services into delegation

classes is only going to become more common, I've gone and created a
comprehensive facility for creating and using delegates in the place
controller and manager as well as the game controller and manager. With
the pattern nicely set, it is also easy to extend to controller/managers
further up the hierarchy that might need to delegate special methods of
their own. Whee!


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@994 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-02-13 03:21:28 +00:00
parent 8399c9a9cf
commit a85a6f1394
13 changed files with 531 additions and 106 deletions
@@ -1,5 +1,5 @@
//
// $Id: GameController.java,v 1.8 2001/10/12 00:03:03 mdb Exp $
// $Id: GameController.java,v 1.9 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
@@ -8,7 +8,9 @@ import java.awt.event.ActionEvent;
import com.samskivert.swing.Controller;
import com.threerings.presents.dobj.*;
import com.threerings.crowd.client.PlaceController;
import com.threerings.crowd.client.PlaceControllerDelegate;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;
@@ -26,8 +28,7 @@ import com.threerings.parlor.util.ParlorContext;
* subscription to the game object and dispatch of commands and
* distributed object events.
*/
public abstract class GameController
extends PlaceController
public abstract class GameController extends PlaceController
implements AttributeChangeListener, GameCodes
{
/**
@@ -127,6 +128,12 @@ public abstract class GameController
*/
protected void gameDidStart ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceControllerDelegate delegate) {
((GameControllerDelegate)delegate).gameDidStart();
}
});
}
/**
@@ -136,6 +143,12 @@ public abstract class GameController
*/
protected void gameDidEnd ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceControllerDelegate delegate) {
((GameControllerDelegate)delegate).gameDidEnd();
}
});
}
/**
@@ -143,6 +156,12 @@ public abstract class GameController
*/
protected void gameWasCancelled ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceControllerDelegate delegate) {
((GameControllerDelegate)delegate).gameWasCancelled();
}
});
}
/** A reference to the active parlor context. */
@@ -0,0 +1,47 @@
//
// $Id: GameControllerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.crowd.client.PlaceControllerDelegate;
/**
* Extends the {@link PlaceControllerDelegate} mechanism with game
* controller specific methods.
*/
public class GameControllerDelegate extends PlaceControllerDelegate
{
/**
* Provides the delegate with a reference to the game controller for
* which it is delegating.
*/
public GameControllerDelegate (GameController ctrl)
{
super(ctrl);
}
/**
* Called when the game transitions to the <code>IN_PLAY</code>
* state. This happens when all of the players have arrived and the
* server starts the game.
*/
public void gameDidStart ()
{
}
/**
* Called when the game transitions to the <code>GAME_OVER</code>
* state. This happens when the game reaches some end condition by
* normal means (is not cancelled or aborted).
*/
public void gameDidEnd ()
{
}
/**
* Called when the game was cancelled for some reason.
*/
public void gameWasCancelled ()
{
}
}
@@ -1,5 +1,5 @@
//
// $Id: GameManager.java,v 1.18 2002/02/12 06:57:29 mdb Exp $
// $Id: GameManager.java,v 1.19 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
@@ -9,9 +9,11 @@ import com.threerings.presents.dobj.MessageEvent;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.server.PlaceManager;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.PlaceManagerDelegate;
import com.threerings.parlor.Log;
import com.threerings.parlor.client.ParlorCodes;
@@ -25,8 +27,7 @@ import com.threerings.parlor.client.ParlorCodes;
* implicitly played in a location, the players of the game implicitly
* bodies in that location.
*/
public class GameManager
extends PlaceManager
public class GameManager extends PlaceManager
implements ParlorCodes, GameCodes, AttributeChangeListener
{
// documentation inherited
@@ -158,6 +159,12 @@ public class GameManager
*/
protected void gameWillStart ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceManagerDelegate delegate) {
((GameManagerDelegate)delegate).gameWillStart();
}
});
}
/**
@@ -168,6 +175,12 @@ public class GameManager
*/
protected void gameDidStart ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceManagerDelegate delegate) {
((GameManagerDelegate)delegate).gameDidStart();
}
});
}
/**
@@ -195,6 +208,13 @@ public class GameManager
*/
protected void gameDidEnd ()
{
// let our delegates do their business
applyToDelegates(new DelegateOp() {
public void apply (PlaceManagerDelegate delegate) {
((GameManagerDelegate)delegate).gameDidEnd();
}
});
// calculate ratings and all that...
}
@@ -0,0 +1,43 @@
//
// $Id: GameManagerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
import com.threerings.crowd.server.PlaceManagerDelegate;
/**
* Extends the {@link PlaceManagerDelegate} mechanism with game manager
* specific methods.
*/
public class GameManagerDelegate extends PlaceManagerDelegate
{
/**
* Provides the delegate with a reference to the game manager for
* which it is delegating.
*/
public GameManagerDelegate (GameManager gmgr)
{
super(gmgr);
}
/**
* Called by the game manager when the game is about to start.
*/
public void gameWillStart ()
{
}
/**
* Called by the game manager after the game was started.
*/
public void gameDidStart ()
{
}
/**
* Called by the game manager after the game ended.
*/
public void gameDidEnd ()
{
}
}
@@ -1,5 +1,5 @@
//
// $Id: GameObject.dobj,v 1.7 2002/02/08 23:55:25 mdb Exp $
// $Id: GameObject.dobj,v 1.8 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
@@ -23,13 +23,13 @@ public class GameObject extends PlaceObject
public static final int AWAITING_PLAYERS = 0;
/** A game state constant indicating that the game is in play. */
public static final int IN_PLAY = 1;
public static final int IN_PLAY = AWAITING_PLAYERS+1;
/** A game state constant indicating that the game ended normally. */
public static final int GAME_OVER = 2;
public static final int GAME_OVER = IN_PLAY+2;
/** A game state constant indicating that the game was cancelled. */
public static final int CANCELLED = 3;
public static final int CANCELLED = GAME_OVER+3;
/** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY},
* {@link #GAME_OVER}, or {@link #CANCELLED}. */
@@ -1,5 +1,5 @@
//
// $Id: GameObject.java,v 1.1 2002/02/08 23:55:25 mdb Exp $
// $Id: GameObject.java,v 1.2 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.game;
@@ -32,13 +32,13 @@ public class GameObject extends PlaceObject
public static final int AWAITING_PLAYERS = 0;
/** A game state constant indicating that the game is in play. */
public static final int IN_PLAY = 1;
public static final int IN_PLAY = AWAITING_PLAYERS+1;
/** A game state constant indicating that the game ended normally. */
public static final int GAME_OVER = 2;
public static final int GAME_OVER = IN_PLAY+2;
/** A game state constant indicating that the game was cancelled. */
public static final int CANCELLED = 3;
public static final int CANCELLED = GAME_OVER+3;
/** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY},
* {@link #GAME_OVER}, or {@link #CANCELLED}. */