0

I used Command Design Pattern to create menu in my simple app.

interface ICommand
{
    string Description { get; }
    void Execute(Library books, List<Book> bookList);
    bool ValueChecker(string checkValue);
}

I'm using ValueChecker to check if fields in my answers are int only and that are somewhat unique. (I add products in AddBookCommand class). Would it be a bad practice to add more methods like bool IsUnique or bool IsYearLessThanCurrent to interface or should I think of other ways to do some simple error handling in my menu?

3
  • 3
    I think that you should not put too much logic into your commands. Commands should be stupid. Instead, you should create command handlers that handle your command. Some of them could be validators. Then you set up a chain of handlers and pass your command to the chain. If it passes the validators then you continue with exacution. If it does not, you do something else. Commented Oct 3, 2016 at 12:39
  • @JakubRusilko could you please enlighten me with some short sample of such handleer? I'm total newbie in coding. Commented Oct 3, 2016 at 13:25
  • It is hard to give you a comprehensive example that would fit your particular case, @Easy. However, there are a lot of articles on the net about commands, etc. I can recommend this site: sourcemaking.com/design_patterns . Take a look at the Command pattern and the Chain of Responsibility pattern described on this page. Commented Oct 3, 2016 at 16:28

2 Answers 2

2

Commands exist for 1 reason = execute some logic. Sometimes it is allowed to add logic regarding Undo functionality or logic, which determine, is is possible to use this command right now or not. There are already defined this interface in .NET here

///<summary>
///     An interface that allows an application author to define a method to be invoked.
///</summary>
public interface ICommand
{
    /// <summary>
    ///     Raised when the ability of the command to execute has changed.
    /// </summary>
    event EventHandler CanExecuteChanged;

    /// <summary>
    ///     Returns whether the command can be executed.
    /// </summary>
    /// <param name="parameter">A parameter that may be used in executing the command. This parameter may be ignored by some implementations.</param>
    /// <returns>true if the command can be executed with the given parameter and current state. false otherwise.</returns>
    bool CanExecute(object parameter);

    /// <summary>
    ///     Defines the method that should be executed when the command is executed.
    /// </summary>
    /// <param name="parameter">A parameter that may be used in executing the command. This parameter may be ignored by some implementations.</param>
    void Execute(object parameter);
}

Adding additional logic for your objects here is not good. It breaks SRP principle and makes code more complicated.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can but It would be more effective if you keep Icommand generic as possible. You can also define a IcommandBase which will keep all execute and all these general methods. And then define another Icommand say IcommandBook for specialized for your objects like Books and Student.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.