static void Main() { Console.WriteLine("! Parking Lot Service !"); var lotService = new ParkingLotService(); var commandFactory = new CommandExecutorFactory(lotService); while (true) { try { var stringInput = Console.ReadLine(); var command = new Command(stringInput); var executor = commandFactory.GetCommandExecutor(command); var isValidCommand = executor.Validate(command); if (!isValidCommand) { throw new Exception("Invalid Command"); } executor.Execute(command); if (command.GetCommandName().Equals("exit")) { break; } } catch (Exception e) { Console.WriteLine(e.Message); break; } } }
public void GetCommandExecutor_UnidentifiedCommand_Expect_Null(string command) { // Act var executor = CommandExecutorFactory.GetCommandExecutor(command, _serviceProvider); // Assert executor.Should().BeNull(); }
public void GetCommandExecutor_RoverCommands_Expect_RoverCommandExecutor(string command) { // Act var executor = CommandExecutorFactory.GetCommandExecutor(command, _serviceProvider); // Assert executor.Should().BeOfType <RoverCommandExecutor>(); }
/// <summary> /// 执行命令 /// </summary> /// <typeparam name="T">命令类型</typeparam> /// <param name="command">命令对象</param> public static void Execute <T>(T command) where T : class, ICommand { //获取相应命令执行者实例 ICommandExecutor <T> commandExecutor = CommandExecutorFactory.GetCommandExecutorFor(command); //执行 commandExecutor.Execute(command); }