代码之家  ›  专栏  ›  技术社区  ›  Sam

具有多种功能的命令行软件设计模式

  •  9
  • Sam  · 技术社区  · 8 年前

    我正在开发一个程序,该程序将从命令行调用。该软件将是一种“工具包”,根据调用程序时定义的不同标志具有不同的功能。其中一些功能与其他功能大不相同,唯一的相似之处是它所操作的文件类型。

    我想知道实现这种功能的最佳设计模式是什么?我读过facade设计模式,它将每个模块划分为不同的子系统。在不同的facade下构建每个功能将保持模块化,并允许我构建单个类来处理要调用的facade。

    这是最好的方式,还是你会推荐一些不同的方式?

    谢谢 山姆

    2 回复  |  直到 8 年前
        1
  •  11
  •   Jairo Alfaro    5 年前

    因为这是一个命令行应用程序,我认为 Command pattern

    这是一种可能的实现(在c语言中)来支持多个命令:

    // The main class, the entry point to the program
    internal class Program
    {
        private static void Main(string[] args)
        {
            var p = new Processor();
            p.Process(args);
        }
    }
    
    /* The command processor class, its responsibility is to take its input parameters to create the command and execute it. */
    public class Processor
    {
        private CommandFactory _commandFactory;
    
        public Processor()
        {
            _commandFactory = new CommandFactory();
        }
    
        public void Process(string[] args)
        {
            var arguments = ParseArguments(args);
            var command = _commandFactory.CreateCommand(arguments);
    
            command.Execute();
        }
    
        private CommandArguments ParseArguments(string[] args)
        {
            return new CommandArguments
            {
                CommandName = args[0]
            };        
        }
    }
    
    /* Creates a command based on command name */
    public class CommandFactory
    {
        private readonly IEnumerable<ICommand> _availableCommands = new ICommand[]
                        {
                            new Command1(), new Command2(), .....
                        };
    
        public ICommand CreateCommand(CommandArguments commandArguments)
        {
            var result = _availableCommands.FirstOrDefault(cmd => cmd.CommandName == commandArguments.CommandName);
            var command = result ?? new NotFoundCommand { CommandName = commandArguments.CommandName };
            command.Arguments = commandArguments;
    
            return command;
        }
    }
    
    public interface ICommand
    {
        string CommandName { get; }
        void Execute();
    }
    
    /* One of the commands that you want to execute, you can create n implementations of ICommand */
    public class Command1 : ICommand
    {
        public CommandArguments Arguments { get; set; }
    
        public string CommandName
        {
            get { return "c1"; }
        }
    
        public void Execute()
        {
            // do whatever you want to do ...
            // you can use the Arguments
        }
    }
    
    
    /* Null object pattern for invalid parametters */
    public class NotFoundCommand : ICommand
    {
        public string CommandName { get; set; }
    
        public void Execute()
        {
            Console.WriteLine("Couldn't find command: " + CommandName);
        }
    }
    
        2
  •  4
  •   Bit33 Riyad Kalla    8 年前

    你可能想看看 strategy pattern :

    策略模式(也称为策略模式)是一种行为软件设计模式,可以在运行时选择算法

    推荐文章