Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // the command interface
- public interface Command {
- public void execute();
- public void undo();
- }
- import java.util.Stack;
- // invoker
- public class DeviceButton {
- Stack<Command> stack = new Stack<Command>();
- public void execute(Command cmd){
- System.out.println("Device button is pressed!");
- stack.add(cmd);
- cmd.execute();
- }
- public void undo(Command cmd) {
- System.out.println("Undoing last command!");
- stack.add(cmd);
- cmd.undo();
- }
- }
- public interface ElectronicDevice {
- public void on();
- public void off();
- public void volumeUp();
- public void volumeDown();
- }
- // the client
- public class PlayWithRemote{
- public static void main(String[] args){
- DeviceButton button = new DeviceButton();
- //TV commands
- // create command receiver
- ElectronicDevice newTV = RemoteControl.GetTelevision();
- // create a concrete command
- TurnOn onTVCommand = new TurnOn(newTV);
- TurnOff offTVCommand = new TurnOff(newTV);
- VolumeUp tvVolumeUp = new VolumeUp(newTV);
- VolumeDown tvVolumeDown = new VolumeDown(newTV);
- // create invoker
- // invoke command
- // create another concrete command
- // invoke another concrete command
- button.execute(onTVCommand);
- button.undo(onTVCommand);
- button.execute(offTVCommand);
- button.undo(offTVCommand);
- //Stereo Commands
- ElectronicDevice newStereo = RemoteControl.GetStereo();
- //concrete commands for stereo
- TurnOn onStereoCommand = new TurnOn(newStereo);
- TurnOff offStereoCommand = new TurnOff(newStereo);
- VolumeUp stereoVolumeUp = new VolumeUp(newStereo);
- VolumeDown stereoVolumeDown = new VolumeDown(newStereo);
- // create invoker
- // invoke commands
- button.execute(onStereoCommand);
- button.undo(onStereoCommand);
- button.execute(stereoVolumeUp);
- button.undo(stereoVolumeUp);
- button.execute(stereoVolumeDown);
- button.undo(stereoVolumeDown);
- button.execute(offStereoCommand);
- button.undo(offStereoCommand);
- }
- }
- public class RemoteControl {
- // return a command receiver
- public static ElectronicDevice GetTelevision(){
- return new Television();
- }
- public static ElectronicDevice GetStereo() {
- return new Stereo();
- }
- }
- // a comand receiver
- public class Stereo implements ElectronicDevice {
- private int volume = 0;
- public void on() {
- System.out.println("Stereo is on");
- }
- public void off() {
- System.out.println("Stereo is off");
- }
- public void volumeUp() {
- volume++;
- System.out.println("Stereo Volume is at: " + volume);
- }
- public void volumeDown() {
- volume--;
- System.out.println("Stereo is at: " + volume);
- }
- }
- // a comand receiver
- public class Television implements ElectronicDevice {
- private int volume = 0;
- public void on() {
- System.out.println("TV is on");
- }
- public void off() {
- System.out.println("TV is off");
- }
- public void volumeUp() {
- volume++;
- System.out.println("TV Volume is at: " + volume);
- }
- public void volumeDown() {
- volume--;
- System.out.println("TV Volume is at: " + volume);
- }
- }
- // a concrete command
- public class TurnOff implements Command {
- ElectronicDevice myDevice;
- public TurnOff(ElectronicDevice device){
- myDevice = device;
- }
- public void execute() {
- myDevice.off();
- }
- @Override
- public void undo() {
- myDevice.on();
- }
- }
- // a concrete command
- public class TurnOn implements Command {
- ElectronicDevice myDevice;
- public TurnOn(ElectronicDevice device){
- // register receiver
- myDevice = device;
- }
- public void execute() {
- myDevice.on();
- }
- @Override
- public void undo() {
- myDevice.off();
- }
- }
- public class VolumeDown implements Command {
- ElectronicDevice myDevice;
- public VolumeDown(ElectronicDevice device){
- myDevice = device;
- }
- public void execute() {
- myDevice.volumeDown();
- } @Override
- public void undo() {
- myDevice.volumeUp();
- }
- }
- public class VolumeUp implements Command {
- ElectronicDevice myDevice;
- public VolumeUp(ElectronicDevice device){
- myDevice = device;
- }
- public void execute() {
- myDevice.volumeUp();
- }
- @Override
- public void undo() {
- myDevice.volumeDown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement