일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Heap
- OS
- Java
- mongoDB
- 디자인 패턴
- 자바
- 백준
- MSA
- IT
- Proxy
- 운영체제
- c언어
- react
- JavaScript
- Galera Cluster
- JPA
- C
- 파이썬
- Data Structure
- Kafka
- design pattern
- 네트워크
- MySQL
- redis
- spring webflux
- Spring
- 컴퓨터구조
- 알고리즘
- Algorithm
- 자료구조
Archives
- Today
- Total
시냅스
커맨드 패턴 Java로 구현 본문
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);
}
}
'디자인 패턴' 카테고리의 다른 글
컴포지트 패턴 Java로 구현 (0) | 2022.09.02 |
---|---|
어댑터 패턴 Java로 구현 (0) | 2022.09.02 |
메디에이터 패턴 Java로 구현 (0) | 2022.08.24 |
브릿지 패턴 Java로 구현 (0) | 2022.08.24 |
앱스트랙트 팩토리 패턴 Java로 구현 (0) | 2022.08.24 |
Comments