어떠한 명령 / 이벤트에 있어 if문을 열거하지않고 체크해주는 방법
public class Non_ChainOfResponsibility {
public static void Main(String[] args) {
whlie(true)
{
string command = Console.ReadLine();
string firstWord = command.Split(' ')[0];
if(firstWord.Equals("SomeWord1")) { // Do Your Stuff1 }
else if(firstWord.Equals("SomeWord2")) { // Do Your Stuff2 }
else if(firstWord.Equals("SomeWord3")) { // Do Your Stuff3 }
...
}
}
}
이 경우 명령어가 추가됨에 따라 if-else문이 늘어난다. 그러면서 명령어가 늘어나면 리스너클래스의 수정도 불가피하다.
이와 같은 if-else열거를 해결하고, 명령어가 늘어나는 변경에 대해 리스너 클래스를 닫는 방법으로 ChainOfResposibility를 사용해봤다.
public interface CommandHandler
{
bool IsSupport(string _command);
void Execute(params string[] _commands);
}
당연하지만 해당 클래스는 CommandHandler를 상속
class EchoCommand : CommandHandler
{
public void Execute(params string[] _commands)
{
Console.Write("Echo : ");
for (int i = 1; i < _commands.Length; i++)
Console.Write($"{_commands[i]} ");
Console.WriteLine();
}
public bool IsSupport(string _command)
{
return _command.Split(' ').Length >= 2 && _command.Split(' ')[0].Equals("Echo");
}
}
DI는 하지않음
public class ChainOfResponsibility {
List<CommandHandler> commandHandlers;
public ChainOfResponsibility()
{
commandHandlers = new List<CommandHandler>();
// 이곳에서 구현한 클래스들을 넣어줌
commandHandlers.Add(new EchoCommand());
}
public static void Main(String[] args) {
whlie(true)
{
string command = Console.ReadLine();
foreach(var commandHandler in commandHandlers)
{
if(commandHandler.IsSupport(command));
commandHandler.Execute(command.Split(' '));
}
}
}
}