Mediator

Definition: The mediator pattern centralizes complex communications and control between related objects. Instead of coupling these objects with eachother or with other objects, they are all couple to the mediator object.

Usage: Use the mediator pattern as an interface to a whole set of complex objects rather than accessing each separately.

Example

class GuiAccess {
	GuiAccess() {
		creditsPanel = new CreditsPanel();
		dialoguePanel = new DialoguePanel();
		gamePanel = new GamePanel();
		titlePanel = new TitlePanel();

		displayTitlePanel();
	}

	displayCreditsPanel() {
		currentPanel = creditsPanel;
	}

	displayDialoguePanel(String dialogueText) {
		dialoguePanel.setText(dialogueText);
		currentPanel = dialoguePanel;
	}

	displayGamePanel() {
		currentPanel = gamePanel;
	}

	displayTitlePanel() {
		currentPanel = titlePanel;
	}

	drawCurrentPanel(Graphics2D g2d) {
		currentPanel.draw(g2d);
	}

	updateCurrentPanel() {
		currentPanel.update();
	}

	JPanel currentPanel;

	CreditsPanel creditsPanel;
	DialoguePanel dialoguePanel;
	GamePanel gamePanel;
	TitlePanel titlePanel;
}

The GuiAccess class is an interface to all the various panels so that they don't need to know about eachother, and no other classes need to know about them either. Everything is decoupled except from the GuiAccess class.

Index

Copyright (C) 2008 Steven Fletcher. All rights reserved.