설명 출처 : https://ko.wikipedia.org/wiki/%EC%BB%A4%EB%A7%A8%EB%93%9C_%ED%8C%A8%ED%84%B4
code : https://github.com/EomTaeWook/Algorithm/tree/master/Algorithm/DesignPattern/Command
커맨드 패턴(Command pattern)이란 요청을 객체의 형태로 캡슐화하여 사용자가 보낸 요청을 나중에 이용할 수 있도록 매서드 이름, 매개변수 등 요청에 필요한 정보를 저장 또는 로깅, 취소할 수 있게 하는 패턴이다.
커맨드 패턴에는 명령(command), 수신자(receiver), 발동자(invoker), 클라이언트(client)의 네개의 용어가 항상 따른다. 커맨드 객체는 수신자 객체를 가지고 있으며, 수신자의 메서드를 호출하고, 이에 수신자는 자신에게 정의된 메서드를 수행한다. 커맨드 객체는 별도로 발동자 객체에 전달되어 명령을 발동하게 한다. 발동자 객체는 필요에 따라 명령 발동에 대한 기록을 남길 수 있다. 한 발동자 객체에 다수의 커맨드 객체가 전달될 수 있다. 클라이언트 객체는 발동자 객체와 하나 이상의 커맨드 객체를 보유한다. 클라이언트 객체는 어느 시점에서 어떤 명령을 수행할지를 결정한다. 명령을 수행하려면, 클라이언트 객체는 발동자 객체로 커맨드 객체를 전달한다.
IReceiver.cs
ICommand.cs
namespace Algorithm.DesignPattern.Command
{
public interface ICommand
{
void Execute();
void Undo();
}
//ExampleClass
class ButtonCommand : DesignPattern.Command.ICommand
{
Button _receiver;
public ButtonCommand(DesignPattern.Command.IReceiver receiver)
{
_receiver = receiver as Button;
}
public void Execute()
{
Console.WriteLine("Execute ButtonCommand");
_receiver.Btn_Click();
}
public void Undo()
{
Console.WriteLine("Undo PowerCommand");
}
}
class PowerOffCommand : DesignPattern.Command.ICommand
{
Power _receiver;
public PowerOffCommand(DesignPattern.Command.IReceiver receiver)
{
_receiver = receiver as Power;
}
public void Execute()
{
Console.WriteLine("Execute PowerOffCommand");
_receiver.Off();
}
public void Undo()
{
Console.WriteLine("Undo PowerOffCommand");
_receiver.On();
}
}
class PowerOnCommand : DesignPattern.Command.ICommand
{
private Power _receiver;
public PowerOnCommand(Power receiver)
{
_receiver = receiver;
}
public void Execute()
{
Console.WriteLine("Execute PowerOnCommand");
_receiver.On();
}
public void Undo()
{
Console.WriteLine("Undo PowerOnCommand");
_receiver.Off();
}
}
}
namespace Algorithm
{
class Remote
{
List<ICommand> _list;
public Remote()
{
_list = new List<ICommand>();
}
public Remote Appand(ICommand command)
{
_list.Add(command);
return this;
}
public void PowerOn()
{
foreach(var command in _list)
{
if(command is PowerOnCommand)
{
command.Execute(); break;
}
}
}
public void PowerOff()
{
foreach (var command in _list)
{
if (command is PowerOffCommand)
{
command.Execute(); break;
}
}
}
public void Click()
{
foreach (var command in _list)
{
if (command is ButtonCommand)
{
command.Execute(); break;
}
}
}
}
class Program
{
static void Main(string[] args)
{
//IReceiver
Power power = new Power();
Button button = new Button();
//ICommand
ButtonCommand buttonCommand = new ButtonCommand(button);
PowerOffCommand powerOffCommand = new PowerOffCommand(power);
PowerOnCommand powerOnCommand = new PowerOnCommand(power);
//Invoke
var remote = new Remote();
remote.Appand(buttonCommand).Appand(powerOffCommand).Appand(powerOnCommand);
remote.PowerOff();
remote.PowerOn();
remote.Click();
}
}
}
'디자인패턴' 카테고리의 다른 글
팩토리 메소드 패턴(Factory Method Pattern) (0) | 2018.03.27 |
---|---|
옵저버 패턴(Observer Pattern) (0) | 2018.03.15 |
싱글톤 패턴(Singleton Pattern) (0) | 2017.08.11 |