これはコマンドラインアプリケーションなので、私は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);
}
}
/* La clase de procesador de comandos, básicamente toma sus parámetros de entrada para crear el comando que necesita ejecutar y ejecuta el comando. */
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);
}
}
さまざまな機能がフラグに基づいてあるでしょう言及しています。したがって、内部ロジックはコンテキストに基づいて変更されますが、正しいですか?フラグに基づいて変更される機能の例を挙げてください。 –
@Sam私はあなたがdownvotesを受け取るかもしれないのではないかと心配しています。これは興味深いですが、Software Designにとっては、この会場よりもはるかに興味深いものです。あなたが必要とするものではなく、人々が好きなものや想像するものを手に入れます。 – ipavlu