시냅스

커맨드 패턴 Java로 구현 본문

디자인 패턴

커맨드 패턴 Java로 구현

ted k 2022. 9. 2. 20:28

Command Pattern

  • 요청을 객체로 만들어 전달한다.
  • 요청을 기록으로 남겨 취소도 가능하도록 한다.
  • 클라이언트 서버간의 프로토콜로 사용할 수 있다.
  • 명령 자체를 객체화 하여 여러 다른 객체에 명령이 전달되거나 명령이 조합될 수도 있다.
  • 새로운 프로토콜이 추가되기 쉽다.
  • 부가적인 정보가 많은 경우 비효율적일 수 있다.

 

  • Command
    • 각 명령이 수행할 메서드 선언
  • ConcreteCommand
    • 실제 명령이 호출되도록 execute 구현
  • Client
    • ConcreteCommand 객체를 생성하고 처리 객체로 정의
  • Invoker
    • Command 처리를 수행할 것을 요청
  • Receiver
    • Command를 처리함

 

구현

package command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

interface Command { // Command
    public void execute();
}

class Light { // receiver
    String location = "";
    public Light(String location) {
        this.location = location;
    }
    public void on() {
        System.out.println(location + " light is on");
    }
    public void off() {
        System.out.println(location + " light is off");
    }
}

class CeilingFan { // receiver
    String location = "";

    public CeilingFan(String location) {
        this.location = location;
    }

    public void on() {
        System.out.println(location + " Ceiling fan is on");
    }

    public void off() {
        System.out.println(location + " Ceiling fan is off");
    }
}

class Stereo { // receiver
    String location = "";

    public Stereo(String location) {
        this.location = location;
    }

    public void on() {
        System.out.println(location + " stereo is on");
    }

    public void off() {
        System.out.println(location + " stereo is off");
    }
}

class GarageDoor { // receiver
    String location;
    public GarageDoor(String location) {
        this.location = location;
    }
    public void up() {
        System.out.println(location + " garage Door is Up");
    }
    public void down() {
        System.out.println(location + " garage Door is Down");
    }
    public void stop() {
        System.out.println(location + " garage Door is Stopped");
    }
    public void lightOn() {
        System.out.println(location + " garage light is on");
    }
    public void lightOff() {
        System.out.println(location + " garage light is off");
    }
}

class LightOnCommand implements Command { // ConcreteCommand
    Light light;
    public LightOnCommand(Light light) {
        this.light = light;
    }
    public void execute() {
        light.on();
    }
}

class LightOffCommand implements Command { // ConcreteCommand
    Light light;
    public LightOffCommand(Light light) {
        this.light = light;
    }
    public void execute() {
        light.off();
    }
}

class CeilingFanOnCommand implements Command { // ConcreteCommand

    CeilingFan ceilingFan;

    public CeilingFanOnCommand(CeilingFan ceilingFan) {
        this.ceilingFan = ceilingFan;
    }

    @Override
    public void execute() {
        ceilingFan.on();
    }
}

class CeilingFanOffCommand implements Command { // ConcreteCommand

    CeilingFan ceilingFan;

    public CeilingFanOffCommand(CeilingFan ceilingFan) {
        this.ceilingFan = ceilingFan;
    }

    @Override
    public void execute() {
        ceilingFan.off();
    }
}

class GarageDoorUpCommand implements Command { // ConcreteCommand
    GarageDoor garageDoor;

    public GarageDoorUpCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.up();
    }
}

class GarageDoorDownCommand implements Command { // ConcreteCommand
    GarageDoor garageDoor;

    public GarageDoorDownCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.down();
    }
}

class StereoOnWithCDCommand implements Command { // ConcreteCommand

    Stereo stereo;

    public StereoOnWithCDCommand(Stereo stereo) {
        this.stereo = stereo;
    }

    @Override
    public void execute() {
        stereo.on();
    }
}

class StereoOffCommand implements Command { // ConcreteCommand

    Stereo stereo;

    public StereoOffCommand(Stereo stereo) {
        this.stereo = stereo;
    }

    @Override
    public void execute() {
        stereo.off();
    }
}

class RemoteControl { // invoker
    ArrayList<Command> onStore = new ArrayList<>();
    ArrayList<Command> offStore = new ArrayList<>();

    public void setCommand(int idx, Command commandOn, Command commandOff) {
        onStore.add(idx, commandOn);
        offStore.add(idx, commandOff);
    }

    public void onButtonWasPushed(int idx) {
        if (onStore.size() <= idx)
            return;
        onStore.get(idx).execute();
    }

    public void offButtonWasPushed(int idx) {
        if (offStore.size() <= idx)
            return;
        offStore.get(idx).execute();
    }
}

public class CommandImpl {
    public static void main(String[] args) {
        RemoteControl remoteControl = new RemoteControl();
        Light livingRoomLight = new Light("Living Room");
        CeilingFan ceilingFan= new CeilingFan("Living Room");
        GarageDoor garageDoor = new GarageDoor("Garage");
        Stereo stereo = new Stereo("Living Room");
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
        CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
        CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
        GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand(garageDoor);
        GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor);
        StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
        StereoOffCommand stereoOff = new StereoOffCommand(stereo);

        remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
        remoteControl.setCommand(1, ceilingFanOn, ceilingFanOff);
        remoteControl.setCommand(2, stereoOnWithCD, stereoOff);
        remoteControl.setCommand(3, garageDoorUp, garageDoorDown);
        System.out.println(remoteControl);
        remoteControl.onButtonWasPushed(0);
        remoteControl.offButtonWasPushed(0);
        remoteControl.onButtonWasPushed(1);
        remoteControl.offButtonWasPushed(1);
        remoteControl.onButtonWasPushed(2);
        remoteControl.offButtonWasPushed(2);
        remoteControl.onButtonWasPushed(3);
        remoteControl.offButtonWasPushed(3);
        remoteControl.onButtonWasPushed(4);
        remoteControl.offButtonWasPushed(4);
    }
}
Comments