2016-10-03 4 views
0

커맨드 디자인 패턴을 사용하여 간단한 어플리케이션에서 메뉴를 생성했습니다. 내 대답 필드는 INT 있는지 확인 ValueChecker를 사용하고있어커맨드 패턴에서 단일 커맨드에 더 많은 메소드 추가하기 C#

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

은 다소 독특합니다. AddBookCommand 클래스에 제품을 추가합니다. bool IsUnique 또는 bool IsYearLessThanCurrent 같은 메소드를 인터페이스에 추가하는 것은 나쁜 습관입니까 아니면 내 메뉴에서 간단한 오류 처리를 수행하는 다른 방법을 생각해야합니까?

+3

나는 당신이 당신의 명령에 너무 많은 논리를 넣으면 안된다고 생각합니다. 명령은 어리 석다. 대신 명령을 처리하는 명령 핸들러를 작성해야합니다. 그 중 일부는 유효성 검사기가 될 수 있습니다. 그런 다음 일련의 핸들러를 설정하고 명령을 체인에 전달합니다. 검사기가 통과하면 대책을 계속합니다. 그렇지 않으면, 당신은 다른 것을합니다. –

+0

@JakubRusilko 저 같은 핸들러의 짧은 견본으로 나를 밝혀 주시겠습니까? 나는 코딩의 초보자 다. – Easy

+0

당신의 사례에 맞는 포괄적 인 예제를 제공하기는 어렵습니다. 그러나 명령에 관한 그물에 관한 많은 기사가 있습니다. https://sourcemaking.com/design_patterns와 같은이 사이트를 추천 할 수 있습니다. 이 페이지에서 설명하는 명령 패턴과 책임 패턴 패턴을 살펴보십시오. –

답변

0

가능하지만 가능한 한 Icommand 제네릭을 유지하면 더 효과적입니다. IcommandBase를 정의하여 모든 실행 및 모든 일반적인 방법을 유지할 수도 있습니다. 그런 다음 Books 및 Student와 같은 객체에 특화된 IcommandBook이라는 또 다른 Icommand를 정의하십시오.

2

명령은 1 가지 이유로 존재하지만 일부 논리를 실행합니다. 때로는이 명령을 지금 사용할지 여부를 결정하는 실행 취소 기능 또는 논리와 관련된 논리를 추가하는 것이 허용됩니다. 이미 여기 개체에 대한 추가 로직을 추가 .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); 
} 

에서이 인터페이스를이 정의하는 것은 좋지 않다. SRP 원칙을 깨고 코드를 더욱 복잡하게 만듭니다.

관련 문제