๐ŸŒป JAVA/๋””์ž์ธ ํŒจํ„ด

6. ์ปค๋งจ๋“œ(Command) ํŒจํ„ด - Head First Design Patterns

iseunghan 2020. 4. 6. 22:30
๋ฐ˜์‘ํ˜•

์ปค๋งจ๋“œ ํŒจํ„ด ์ •์˜ ) ์ปค๋งจ๋“œ ํŒจํ„ด์„ ์ด์šฉํ•˜๋ฉด ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ฐ์ฒด๋กœ ์บก์Šํ™” ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์จ์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋‹ค๋ฅธ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ง‘์–ด ๋„ฃ์„์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์š”์ฒญ ๋‚ด์—ญ์„ ํ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•˜์—ฌ, ์ž‘์—… ์ทจ์†Œ ๊ธฐ๋Šฅ๋„ ๋„ฃ์„์ˆ˜ ์žˆ๋‹ค.

 

์ปค๋งจํŠธ ํŒจํ„ด์€ ์ผ๋ จ์˜ ํ–‰๋™์„ ํŠน์ • ๋ฆฌ์‹œ๋ฒ„ ํ•˜๊ณ  ์—ฐ๊ฒฐ ์‹œํ‚ด์œผ๋กœ์จ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์บก์Šํ™” ์‹œํ‚จ๋‹ค.

์š”์ฒญ์„ ํ•˜๋Š” ๊ฐ์ฒด์™€ ๊ทธ ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ปค๋งจ๋“œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

์ปค๋งจ๋“œ ํŒจํ„ด : ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ


 

Remote Control ์˜ˆ์ œ

 

public class LightOnCommand implements Command {
	Light light;

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

	public void execute() {
		light.on();
	}
}
public class RemoteControl {
	Command[] onCommands;	// on ๋ฒ„ํŠผ๋“ค ๋ชจ์•„๋†“๋Š” ๋ฐฐ์—ด
	Command[] offCommands;	// off ๋ฒ„ํŠผ๋“ค ๋ชจ์•„๋†“๋Š” ๋ฐฐ์—ด
 
	public RemoteControl() {
		onCommands = new Command[7];
		offCommands = new Command[7];
 
		Command noCommand = new NoCommand();
		for (int i = 0; i < 7; i++) {
			onCommands[i] = noCommand;	// ์ผ๋‹จ ์ดˆ๊ธฐํ™”๋ฅผ ์ปค๋งจ๋“œ๊ฐ€ ์—†๋Š” noCommand ๊ฐ์ฒด ๋„ฃ์Œ.
			offCommands[i] = noCommand; // ์ผ๋‹จ ์ดˆ๊ธฐํ™”๋ฅผ ์ปค๋งจ๋“œ๊ฐ€ ์—†๋Š” noCommand ๊ฐ์ฒด ๋„ฃ์Œ.
		}
	}
  
	public void setCommand(int slot, Command onCommand, Command offCommand) {
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}
 
	public void onButtonWasPushed(int slot) { // slot์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด on ์‹คํ–‰(execute)
		onCommands[slot].execute();
	}
 
	public void offButtonWasPushed(int slot) { // slot์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด off ์‹คํ–‰(execute)
		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();
	}
}
public class RemoteLoader {
 
	public static void main(String[] args) {
		RemoteControl remoteControl = new RemoteControl(); //๋ฆฌ๋ชจ์ปจ ๊ฐ์ฒด ์ƒ์„ฑ
 //living room์— ์žˆ๋Š” Light ๊ฐ์ฒด ์ƒ์„ฑ
		Light livingRoomLight = new Light("Living Room");
 //on,off์ปค๋งจ๋“œ์— Light ์œ„์น˜๋ฅผ ๋„˜๊ฒจ์คŒ.
		LightOnCommand livingRoomLightOn = 
				new LightOnCommand(livingRoomLight);
		LightOffCommand livingRoomLightOff = 
				new LightOffCommand(livingRoomLight);
 //on,off command ๋“ฑ๋ก
		remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
  
		System.out.println(remoteControl);
 
		remoteControl.onButtonWasPushed(0);
		remoteControl.offButtonWasPushed(0);
	}
}

์ถœ๋ ฅ )  [slot 0] package.LightOnCommand         package.LightOffCommand

         ( noCommand ..์ƒ๋žต )

         Living Room light is on

         Living Room light is off

 

 

 

๊ฐ„๋‹จํ•œ ๋ฆฌ๋ชจ์ปจ : ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

๋ฆฌ๋ชจ์ปจ : ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

 


 

๋ฆฌ๋ชจ์ปจ์— Undo() ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ.

 

public interface Command{
	public void execute();
    	 public void undo();	// <<-- undo๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋จ! 
  }
//LightOnCommand

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

public void undo(){
	light.off(); // on -> off / off -> on
}
public class RemoteControlWithUndo {
	Command[] onCommands;
	Command[] offCommands;
	Command undoCommand;	// new!
 
	public RemoteControlWithUndo() {
		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;
		}
		undoCommand = noCommand;	// new!
	}
  
	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];		// new!
	}
 
	public void offButtonWasPushed(int slot) {
		offCommands[slot].execute();
		undoCommand = offCommands[slot];	// new!
	}
 
	public void undoButtonWasPushed() {		// new!
		undoCommand.undo();					
	}
   //toString() ์ƒ๋žต.
}

 

 

party mode

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();
       	}
   }
//Macro ๋“ฑ๋ก.

Command[] partyOn = {lightOn, stereoOn, tvOn, hottubOn};
Command[] partyOff = {lightOff, stereoOff, tvOff, hottubOff};

MacroCommand partyOnMacro = new MacroCommand(partyOn);
MacroCommand partyOffMacro = new MacroCommand(partyOff);

 

๋ฐ˜์‘ํ˜•