http://ko.wikipedia.org/wiki/%EC%BB%A4%EB%A7%A8%EB%93%9C_%ED%8C%A8%ED%84%B4
커맨드 패턴을 이용하면 요구사항을 객체로 캡슐화 할 수 있다. 매개변수를 써서 여러가지 요구사항을 만들 수도 있다. 또한 요청내역을 큐에 저장하여 로그를 기록하는 것도 가능하고 작업 취소 구현도 가능하다.
/*the Invoker class*/ public class Switch { private Command flipUpCommand; private Command flipDownCommand; public Switch(Command flipUpCmd,Command flipDownCmd){ this.flipUpCommand=flipUpCmd; this.flipDownCommand=flipDownCmd; } public void flipUp(){ flipUpCommand.execute(); } public void flipDown(){ flipDownCommand.execute(); } } /*Receiver class*/ public class Light{ public Light(){ } public void turnOn(){ System.out.println("The light is on"); } public void turnOff(){ System.out.println("The light is off"); } } /*the Command interface*/ public interface Command{ void execute(); } /*the Command for turning on the light*/ public class TurnOnLightCommand implements Command{ private Light theLight; public TurnOnLightCommand(Light light){ this.theLight=light; } public void execute(){ theLight.turnOn(); } } /*the Command for turning off the light*/ public class TurnOffLightCommand implements Command{ private Light theLight; public TurnOffLightCommand(Light light){ this.theLight=light; } public void execute(){ theLight.turnOff(); } } /*The test class*/ public class TestCommand{ public static void main(String[] args){ Light light=new Light(); Command switchUp=new TurnOnLightCommand(light); Command switchDown=new TurnOffLightCommand(light); Switch s=new Switch(switchUp,switchDown); s.flipUp(); s.flipDown(); } }