욱'S 노트

Command - 명령을 클래스로 표현하기 본문

Methdology/Design Pattern

Command - 명령을 클래스로 표현하기

devsun 2012. 4. 30. 10:36
When Using It

명령을 클래스로 표현하고 싶을때

Class Diagram


Sample Code



public interface Command {

public void execute();

}



public class ConcreteCommand implements Command {

private Receiver receiver = new Receiver();

@Override

public void execute() {

receiver.action();

}

}


public class Receiver {

public void action() {

System.out.println("Action Performed!!!");

}

}


public class Invoker {

private List<Command> commands = new ArrayList<Command>();

public void addCommand(Command command) {

commands.add(command);

}

public void excute() {

for ( Command command : commands) {

command.execute();

}

}

}



public class Client {

public static void main(String[] args) {

Invoker invoker = new Invoker();

invoker.addCommand(new ConcreteCommand());

invoker.excute();

}

}



Caution


특별한 주의사항은 없다.
Comments