한줄코딩 디자인패턴 스터디 발표 211016

bluesky·2021년 10월 15일
0

[1] 개념 및 사용하는 경우

{1} 개념

요구 사항을 객체로 캡슐화할 수 있으며, 매개변수를 써서 여러 가지 다른 요구 사항을 집어넣을 수도 있습니다. 또한 요청 내역을 큐에 저장하거나 로그로 기록할 수도 있으며, 작업취소 기능도 지원 가능합니다.

{2} 사용하는 경우

요청을 하는 객체와 요청을 수행하는 객체를 분리시켜서,
요청이 더쉽게 확장 가능하게끔 할때 사용할 수 있다.

[2] 이론

{1} 구조

Invoker

  • 기능의 실행을 요청하는 호출자 클래스

Command

  • 실행할 기능에 대한 인터페이스

ConcreteCommand

  • 실행되는 기능을 구현함. 리시버의 특정 메서드를 호출.

Receiver

  • 행동을 하는 객체

Client

  • 커맨드 객체를 생성, 호출자(Invoker)를 통해 수신자(Receiver)에게 할 행동을 전달함

{2} 특징.

  • 커맨드 패턴을 활용하게 요청부와 동작부를 분리시켜주기 때문에 시스템의 결합도를 낮출 수 있으며, 각 객체들이 수정되어도 다른 객체가 영향을 받지 않습니다.

  • 클라이언트와 INVOKER 클래스 간의 의존성이 제거 된다.

  • 행위(Behavioral) 패턴 의 일종.

[3] 예시

{1} Simple remote

  • Command.java
package exercise.simpleremote;

public interface Command {
	public void execute();
}
  • Light.java
package exercise.simpleremote;

public class Light {

	public Light() {
	}

	public void on() {
		System.out.println("Light is on");
	}

	public void off() {
		System.out.println("Light is off");
	}
}
  • LightOnCommand.java
package exercise.simpleremote;

public class LightOnCommand implements Command {
	Light light;
  
	public LightOnCommand(Light light) {
		this.light = light;
	}
 
	public void execute() {
		light.on();
	}
}
  • SimpleRemoteControl.java
package exercise.simpleremote;

//
// This is the invoker
//
public class SimpleRemoteControl {
	Command slot;
 
	public SimpleRemoteControl() {}
 
	public void setCommand(Command command) {
		slot = command;
	}
 
	public void buttonWasPressed() {
		slot.execute();
	}
}
  • RemoteControlTest.java
package exercise.simpleremote;

public class RemoteControlTest {
	public static void main(String[] args) {
		SimpleRemoteControl remote = new SimpleRemoteControl();
		Light light = new Light();
		GarageDoor garageDoor = new GarageDoor();
		LightOnCommand lightOn = new LightOnCommand(light);
		GarageDoorOpenCommand garageOpen = 
		    new GarageDoorOpenCommand(garageDoor);
 
		remote.setCommand(lightOn);
		remote.buttonWasPressed();
		remote.setCommand(garageOpen);
		remote.buttonWasPressed();

    }
	
}

{2} remote

  • RemoteControl.java
package example.remote;

//
// This is the invoker
//
public class RemoteControl {
	Command[] onCommands;
	Command[] offCommands;
 
	public RemoteControl() {
		onCommands = new Command[7];
		offCommands = new Command[7];
 
		Command noCommand = new NoCommand();
		for (int i = 0; i < 7; i++) {
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
	}
  
	public void setCommand(int slot, Command onCommand, Command offCommand) {
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}
 
	public void onButtonWasPushed(int slot) {
		onCommands[slot].execute();
	}
 
	public void offButtonWasPushed(int slot) {
		offCommands[slot].execute();
	}
  
	public String toString() {
		StringBuffer stringBuff = new StringBuffer();
		stringBuff.append("\n------ Remote Control -------\n");
		for (int i = 0; i < onCommands.length; i++) {
			stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
				+ "    " + offCommands[i].getClass().getName() + "\n");
		}
		return stringBuff.toString();
	}
}

{3} macro command

  • MacroCommand.java
package example.party;

public class MacroCommand implements Command {
	Command[] commands;
 
	public MacroCommand(Command[] commands) {
		this.commands = commands;
	}
 
	public void execute() {
		for (int i = 0; i < commands.length; i++) {
			commands[i].execute();
		}
	}
 
    /**
     * NOTE:  these commands have to be done backwards to ensure 
     * proper undo functionality
     */
	public void undo() {
		for (int i = commands.length -1; i >= 0; i--) {
			commands[i].undo();
		}
	}
}


{4} java example

public class Main {
  public static void main(String[] args) {
    Thread t = new Thread(new Command());
    t.start();  /* run을 실행하지만 Command가 무엇을 하는지는 모른다 */
  }
}

class Command implements Runnable {
  @Override
  public void run() {
    System.out.println("RUN!");
  }
}

{5} open source example

https://www.findbestopensource.com/product/rylo-bubblesort-command-pattern

[4] 실습

https://github.com/0xd00d00/DPStudy/tree/master/6%EC%A3%BC%EC%B0%A8/command_pattern/src/exercise/simpleremote

[5] 참고자료

https://gmlwjd9405.github.io/2018/07/07/command-pattern.html

https://k0102575.github.io/articles/2020-02/command-pattern

https://johngrib.github.io/wiki/pattern/command/

https://johngrib.github.io/wiki/pattern/null-object/

profile
SMART https://github.com/dongseoki?tab=repositories

0개의 댓글