https://refactoring.guru/design-patterns/command
Command
/ Design Patterns / Behavioral Patterns Command Also known as: Action, Transaction Intent Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you p
refactoring.guru
Dependency Inversion 의 핵심과 같은 패턴.
모듈 내 특정 상황에 맞는 행동을 설정된 외부 객체에 의해 control 하도록 한다.
예시
- level에 따라 다른 스킬이 발동되는 tower/캐릭터 모듈.
- 버튼 생성시 부여되는 버튼 클릭 액션 정의 및 설정.
예제 코드
더보기
public interface ICommand
{
void Execute();
}
public class XMLLoader : ICommand
{
public void Execute()
{
Console.WriteLine("XML has been loaded.");
}
}
public class JsonLoader : ICommand
{
public void Execute()
{
Console.WriteLine("Json has been loaded.");
}
}
public class FlexibleLoader
{
ICommand _cmdExecute;
public void SetLoader(ICommand cmd) { _cmdExecute = cmd; }
public void Execute()
{
if(_cmdExecute is ICommand)
_cmdExecute.Execute();
else
Console.WriteLine("Need to init.");
}
}
internal class CommandDemo
{
public void Run()
{
FlexibleLoader loader = new FlexibleLoader();
loader.SetLoader(new XMLLoader());
loader.Execute();
}
}
'Work & Programming > System Design' 카테고리의 다른 글
[Behavioral Pattern] - Observer (0) | 2024.05.16 |
---|---|
[Behavioral Pattern] - Memento (0) | 2024.05.16 |
[Behavioral Pattern] - Chain Of Responsibility (0) | 2024.05.16 |
[Structural Pattern] - Proxy (0) | 2024.05.15 |
[Structural Pattern] - Flyweight (0) | 2024.05.15 |