티스토리 뷰

Command 패턴 

요청을 객체로 캡슐화해주며, 파라미터를 통해 다양한 요청을 처리할 수 있도록 해준다. 또한 요청을 큐에 유지할 수 있으며, 요청에 대한 로그를 유지할 수 있고, 요청 취소 기능도 제공한다.


Project : 작업 취소 기능이 있는 선풍기 구현해보기


Command 인터페이스 생성 

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



Command 인터페이스에 맞춰 프로그래밍할 클래스 생성

CeilingFanHighCommand implements Command{..}

CeilingFanLowCommand implements Command{..}

CeilingFanMediumCommnad implements Command{..}

CeilingFanOffCommand implements Command{..}

NoCommand implements Command{..}  ( NoCommand는 초기화하기 위한 빈 클래스 )

public class CeilingFanOffCommand implements Command {

	CeilingFan ceilingFan;
	CeilingFanSpeed prevSpeed;

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

	@Override
	public void execute() {
		prevSpeed = ceilingFan.getSpeed();
		ceilingFan.SetSpeed(CeilingFanSpeed.OFF);
	}

	@Override
	public void undo() {
		ceilingFan.SetSpeed(prevSpeed);
	}
} 




속도의 기준을 묶어둔 enum 클래스

public enum CeilingFanSpeed {
	OFF, LOW, MEDIUM, HIGH;
} 



리모컨 기능을 하는 클래스

public class RemoteControlWithUndo {

	private final int size = 7;

	Command[] onCommands;
	Command[] offCommands;
	Command undoCommand;

	public RemoteControlWithUndo() {
		onCommands = new Command[size];
		offCommands = new Command[size];

		Command noCommand = new NoCommand();
		for (int i = 0; i < size; i++) {
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
		undoCommand = noCommand;
	}

	public void setCommand(int slot, Command onCommand, Command offCommand) {
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}

	public void onButtonWasPushed(int slot) {
		onCommands[slot].execute();
		undoCommand = onCommands[slot];
	}

	public void offButtonWasPushed(int slot) {
		offCommands[slot].execute();
		undoCommand = offCommands[slot];
	}

	public void undoButtonWasPushed() {
		undoCommand.undo();
	}

	@Override
	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();
	}
} 



선풍기 역할을 하는 클래스 
public class CeilingFan {

	String location;
	CeilingFanSpeed speed;

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

	public void SetSpeed(CeilingFanSpeed speed) {
		this.speed = speed;
		System.out.println("Living Room ceiling fan is on " + this.speed);
	}

	public CeilingFanSpeed getSpeed() {
		return speed;
	}
} 


실행 

public class Test {

	public static void main(String[] args) {

		RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();
		CeilingFan ceilingFan = new CeilingFan("Living Room");
		
		CeilingFanMediumCommnad ceilingFanMedium = new CeilingFanMediumCommnad(ceilingFan);
		CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
		CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
		
		
		remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff);
		remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff);
		
		
		
		remoteControl.onButtonWasPushed(0);
		remoteControl.offButtonWasPushed(0);
		System.out.println(remoteControl);
		remoteControl.undoButtonWasPushed();
		
		remoteControl.onButtonWasPushed(1);
		System.out.println(remoteControl);
		remoteControl.undoButtonWasPushed();
	}

} 


결과화면




UML ( 부분 )








참고자료 : Head Frist Design Patterns




'Paradigm > OOP' 카테고리의 다른 글

[OOP] AdapterPattern  (0) 2013.05.10
[OOP] 초기화를 위한 Null Object  (0) 2013.04.27
[OOP] Singleton, Doubleton, Mutilton  (0) 2013.04.19
[OOP] Singleton Pattern의 생성자문제  (0) 2013.04.19
[OOP] Observable의 단점  (0) 2013.04.19
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
Total
Today
Yesterday