public void ExecuteAction(string command)
{
if (command == "Jump")
{
Jump();
}
else if (command == "Run")
{
Run();
}
else if (command == "Attack")
{
Attack();
}
else if (command == "Defend")
{
Defend();
}
else
{
Console.WriteLine("Unknown command.");
}
}
이렇게 있으면
public enum PlayerCommand
{
Jump,
Run,
Attack,
Defend
}
private readonly Dictionary<PlayerCommand, Action> dispatchTable;
public PlayerController()
{
dispatchTable = new Dictionary<PlayerCommand, Action>
{
{ PlayerCommand.Jump, Jump },
{ PlayerCommand.Run, Run },
{ PlayerCommand.Attack, Attack },
{ PlayerCommand.Defend, Defend }
};
}
public void ExecuteCommand(PlayerCommand command)
{
if (dispatchTable.TryGetValue(command, out Action action))
{
action();
}
}
이런식으로
이런식으로
이게 훨씬 보기 편함
아 Dictionary에 델리게이트 넣는다는거였네 ㅇㅅㅇ 패턴매칭은 어떰 ㅇㅅㅇ?
패턴 매칭도 하는데, 이게 훨씬 깔끔하고 유지 보수성이 많음. 왜냐하면 해시테이블 기반으로 동작하기때문에 O(1) 에 따라 성능면에서 유리함
상위 구조와의 연관성이 없다면 개삽질임.
옵션별 단일 이벤트 실행이라서 괜찮아 보임. 나는 저런거 쓸일이 없겠지만