In this article we will explain two JAVA design patterns:
Command and State.
In the main method we:
- first create the light object (aka receiver);
- then we check if the light is on or off;
- after that we create a light command passing to it the light we’ve just created;
- then we create a Remote Control and set the command into it;
- in the next step we press the button and we check the light state;
- finally, we repeat the process and check the light state again.
So lets start with the code:
Here we have the client class (the one that have the main method)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package pt.joaobrito.client; import pt.joaobrito.command.Command; import pt.joaobrito.command.LightCommand; import pt.joaobrito.command.invoker.RemoteControl; import pt.joaobrito.state.receiver.Light; /** * The client object */ public class Client { public static void main(String[] args) { // receiver Light light = new Light(); // prints to the console the current state of the light System.out.println("1st state - the light is " + (light.getCurrentState().isOn() ? "ON" : "OFF")); Command lightsCmd = new LightCommand(light); //invoker RemoteControl remoteControl = new RemoteControl(); remoteControl.setCommand(lightsCmd); //switch on remoteControl.pressButton(); System.out.println("2nd state - the light is " + (light.getCurrentState().isOn() ? "ON" : "OFF")); //switch off remoteControl.pressButton(); System.out.println("3rd state - the light is " + (light.getCurrentState().isOn() ? "ON" : "OFF")); } } |
The Command Design Pattern implementation
Here we define our Command interface
1 2 3 4 5 |
package pt.joaobrito.command; public interface Command { public void execute(); } |
This is the remote control class.
Note that in the pressButton method we actually execute the Command passed previously in the set method. Notice that this class has no knowledge at all about the receiver. Instead it just knows that it has a Command and how to interact with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package pt.joaobrito.command.invoker; import pt.joaobrito.command.Command; /** * Invoker */ public class RemoteControl { private Command command; public void setCommand(Command command) { this.command = command; } public void pressButton() { command.execute(); } } |
Here we have the concrete light command (the implementation of the Command interface). In the execute method we specify what the command really does: in this case with the help of the State Design Pattern we are simply saying to the light to go to the next state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package pt.joaobrito.command; import pt.joaobrito.state.receiver.Light; /** * Concrete Command: light command */ public class LightCommand implements Command { //reference to the light private Light light; public LightCommand(Light light) { this.light = light; } public void execute() { light.switchToNextState(); } } |
The next section is the State Design Pattern part
This is the Light class. From the Command point of view this is the receiver and from the point of view of the state design pattern this is the object that keeps the current state of the light (also known as a wrapper that will be passed to the concrete states).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package pt.joaobrito.state.receiver; /** * This class is responsible to keep the state of the light */ public class Light { private State currentState; public Light() { // 1st state of light currentState = new LightOff(); } public State getCurrentState() { return currentState; } public void setState(State state) { currentState = state; } /** * light switch */ public void switchToNextState() { currentState.next(this); } } |
This is the state interface. Notice that here we have two methods that have to be implemented by the classes that implement the state interface.
1 2 3 4 5 6 |
package pt.joaobrito.state.receiver; public interface State { void next(Light wrapper); boolean isOn(); } |
The next class represents one of the possible state of the light (the concrete state that represents the ON state of the light)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package pt.joaobrito.state.receiver; /** * Receiver */ public class LightOn implements State{ @Override public void next(Light wrapper) { wrapper.setState(new LightOff()); } @Override public boolean isOn() { return true; } } |
This is the other possible state (the concrete state that represents the OFF state of the light)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package pt.joaobrito.state.receiver; /** * Receiver */ public class LightOff implements State{ @Override public void next(Light wrapper) { wrapper.setState(new LightOn()); } @Override public boolean isOn() { return false; } } |
And the results:
1 2 3 4 5 |
run: 1st state - the light is OFF 2nd state - the light is ON 3rd state - the light is OFF BUILD SUCCESSFUL (total time: 0 seconds) |
And that’s it. Feel free to comment.
Thank you!