lukebevan.com Blog A Computer Science Student from the University of Wolverhampton, UK

21Jul/100

Design Patterns with Java (Encapsulating Invocation)

Over the last few months I have been preparing for my final year for my BSc Computer Science (Software Engineering) degree, during this time I have read Head First Object Orientated Programming with Java,  Java Pocket Guide and Head First Java. During the past year I have been on an Industrial Placement as an I.T. Technician at King Edward VI College in Stourbridge, I will resume my studies on the 13th September.

Below is some  Java code of a design pattern called the "Command Design Pattern", this design pattern enables to change the command's behaviour during runtime as the code below is "loosely coupled". The reason why it is loosely coupled is because the command the button uses is not hard-coded and as explained, the command can be changed at runtime.  The code block below demostrates that a Command object can be used by a button object and the Command object can be simply changed by invoking the  setCommand() method on the button object.


public class CommandTest {

	public static void main(String[] args) {

		Command openDoorCommand = new OpenDoorCommand(new Door());
		Button button = new Button(openDoorCommand);

		button.doCommand();
		button.undo();
		button.setCommand(new OpenWindowCommand(new Window()));
		button.doCommand();
		button.undo();

	}

}

The code below is for the button, the button uses a command object. Here the command's execute() method can be invoked with in the doCommand method (which was called in the main method of the CommandTest class), the button can use any object unless it implements the Command Interface. The command can be changed by using the setCommand method if required.


public class Button {

	Command command;

	public Button(Command command){
		this.command = command;
	}

	public void doCommand(){
		command.execute();
	}

	public void undo(){
		command.undo();
	}

	public void setCommand(Command newCommand){
		command = newCommand;

	}

}

This code contains what the command does, the execute() method can open a "door" object and the undo() method can close the door, this class implements the Command interface. The code below also enables the encapsulation of invocation as a button's command can be changed during runtime and a command object can interact with another object, in this instance a door.


public class OpenDoorCommand implements Command {

	Door door;

	public OpenDoorCommand(Door door){
		this.door = door;
	}

	public void execute() {
		door.open();

	}

	public void undo() {
		door.close();

	}

}

The code below is the same as OpenDoorCommand, just replace "Door" with "Window", remember OpenWindowCommand must use a Window object in its parameterised constructor.


public class OpenWindowCommand implements Command {

	Window window;

	public OpenWindowCommand(Window window){
		this.window = window;
	}

	public void execute() {
		window.open();

	}

	public void undo() {
		window.close();

	}

}

The code below is the Door class, an instance of a Door object can open and close, these methods are called by the OpenDoorCommand.


public class Door {

	public void open(){
		System.out.println("The door has opened");
	}

	public void close(){
		System.out.println("The door has closed");
	}

}

The code below is the same as the Door class, I have only replaced the word "Door" with "Window" as a window has  similar function when compared to a door in the real world.


public class Window {

	public void open(){
		System.out.println("The window has opened");
	}

	public void close(){
		System.out.println("The window has closed");
	}

}

This is the code for the Command interface, all commands implement the Command interface as well as the execute() and undo() methods, this is to ensure all commands have these methods so they can be called by the button's doCommand() and undo() methods. This also enables the button to use any command object.


public interface Command {

 public void execute();
 public void undo();

}

Credit to Head First Design Patterns by Eric Freeman, Elizabeth Freeman with Kathy Sierra and Bert Bates published by O'Reilly. I am only a third through the book and it did take me a while to get my head round the command pattern, but after a while it actually makes sense.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.