Design Architecture/Design Pattern 예제

Commnad패턴 예제

lipnus 2022. 4. 26. 10:45
반응형

 

 

public interface Command {
    public void excute();
}

public class LightOnCommand implements Command {
    Light light; // Light: 리시버

    public LightOnCommand(Light light) {
        this.light = light;
    }

    public void excute() {
        light.on();
    }
}

public class SimpleRemoteControl {
    Command slot;
    public SimpleRemoteControl() {}

    public void setCommand(Command command) {
        this.slot = command;
    }

    public void buttonWasPressed() {
        slot.excute();
    }
}

public class RemoteControlTest {
    public static void main(String[] args) {
        SimpleRemoteControl remote = new SimpleRemoteControl(); // 인보커(종업원)
        Light light = new Light(); // 리시버(주방장)
        LightOnCommand command = new LightOnCommand(light);

        remote.set(command);
        remote.buttonWasPressed();
    }
}
반응형

'Design Architecture > Design Pattern 예제' 카테고리의 다른 글

Visitor 패턴 예제  (0) 2022.03.25
Composite 패턴 예제  (0) 2021.09.28
Decorator 예제  (0) 2021.09.28
Template Method 예제  (0) 2021.09.28
빌더(Builder) 패턴 예제  (0) 2021.09.26