2017-10-26 9 views
3

私はコマンドラインから呼び出される予定のプログラムを開発しています。このソフトウェアは、プログラムを呼び出す際に定義されたさまざまなフラグに基づいて異なる機能を持つ一種の「キット」になります。この機能のいくつかは、他のものとは大きく異なります。唯一の類似点は、それが動作するファイルのタイプです。複数の機能を持つコマンドラインソフトウェアのデザインパターン

私は、この種の機能を実装するのに最適なデザインパターンが何であるか疑問に思っていましたか?私は、各モジュールを異なるサブシステムに分割するファサードデザインパターンについて読んだ。別のファサードの下で機能の各部分を構築することはモジュラを維持し、どのファサードを呼び出すかを処理する単一のクラスを構築することができます。

これは最善の方法ですか、別のものをお勧めしますか?

おかげで、 サム

+0

さまざまな機能がフラグに基づいてあるでしょう言及しています。したがって、内部ロジックはコンテキストに基づいて変更されますが、正しいですか?フラグに基づいて変更される機能の例を挙げてください。 –

+0

@Sam私はあなたがdownvotesを受け取るかもしれないのではないかと心配しています。これは興味深いですが、Software Designにとっては、この会場よりもはるかに興味深いものです。あなたが必要とするものではなく、人々が好きなものや想像するものを手に入れます。 – ipavlu

答えて

1

これはコマンドラインアプリケーションなので、私は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); 
    } 
} 
+0

ありがとう、これは間違いなく私が探していたものです。このスレッドにアクセスする人は、https://sourcemaking.com/design_patterns/commandにこのデザインパートナーに関する詳細な説明があります。 – Sam

3

あなたはstrategy patternチェックアウトする場合があります(また、ポリシーパターンとして知られている)

戦略パターンがでアルゴリズムを選択可能な行動デザインパターンですランタイム

+0

これは栄光のある基本的なインターフェイスパターンです。そのような場合、すべてのコードがうまくいきます。共有されたインターフェイスで特定のカテゴリやファミリの下で同じ部品や動作を共有している同様のものです。私は良い答えと思います... upvoted – ipavlu

関連する問題