0

データを提供する API に変更を加えています。一部の検索では、作成者に関するデータが必要で、IAuthorオブジェクトが取得されます。API には、インターフェースと、と呼ばれるIAuthorを実装する単一の具象クラスがあります。IAuthorAuthor

IsNovelistすべてではなく一部の検索のセマンティクスを変更するブール型プロパティを追加する必要があります。

オープン/クローズの原則について聞いたことがありますが、 IAuthorand/orクラスを変更するとAuthorこれに違反するようです。では、この単純な変更を行うにはどうすればよいでしょうか。


アップデート:

間違ったクラスに集中していたのかもしれません。クラスに動作を追加したくありませんAuthor(これは、パラメーターを API に渡す方法にすぎません)。したがって、Decorated作成者はブール値フラグを必要としません。これはisNovelist == true、暗示によるものだからです。

GetBooks小説家としてフラグが立てられている著者を指定して、メソッドの動作を変更する必要があります。Booksこのようなものですが、クラスを変更している (拡張していない) ため、私の考えはおそらく不安定です。

//Before
class Books 
{
   public Books[] GetBooks(IAuthor author){
       // Call data access GetBooks...
    }
}


//After
class Books 
{
   public Books[] GetBooks(IAuthor author){
       // To maintain pre-existing behaviour
       // call data access GetBooks with SQL param @isNovelist = false...
       // (or don't pass anything because the SQL param defaults to false)
    }

   public Books[] GetBooksForNovelist(IAuthor author){
       // To get new behaviour
       // call data access GetBooks with SQL param @isNovelist = true
    }
}
4

2 に答える 2

1

デコレータパターンの使用方法は次のとおりです

interface IAuthor
{
    void someMethod();
}

class Author:IAuthor{

    public void someMethod(){
        //Implementation here
    }
}

//Class that will add the extra behavior.
class DecoratedAuthor : IAuthor
{
    IAuthor author;
    public bool isNovelist{get;set;}//Add the extra Behavior

    public DecoratedAuthor(IAuthor auth)
    {
        this.author = auth;
    }

    public void someMethod(){
        //Call the Author's version here
        author.someMethod();

        //check if he is a novelist
        isNovelist = true;
    }

}


public class program{
    public static void Main(string[] args)
    {
        IAuthor auth = new Author();
        DecoratedAuthor novAuth = new DecoratedAuthor(auth);

        DecoratedAuthor.someMethod();
        //see if he is a novelist
        Console.WriteLine(novAuth.isNovelist);

    }

}
于 2013-01-17T07:21:50.010 に答える
1

解決策2:

class Program
{
    private static void Main(string[] args)
    {

        IAuthor author = new Novelist();
        author.Name = "Raj";

        // i guess u have check if author is a novelist
        // the simple way is by safe typecasting

        Novelist novelist = author as Novelist;

        if (novelist != null)
        {
            Console.WriteLine("Wohoo, i am a novelist");
        }
        else
        {
            Console.WriteLine("Damn,i cant write novel");
        }


    }

解決策1:

public enum AuthourType
{
    Novelist,
    Other
}

public interface IAuthor
{
    string Name { get; set; }
    AuthourType Type { get; set; }

}

public class  Novelist : IAuthor
{
    public string Name { get; set; }
    public AuthourType Type { get; set; }
    // incase u dont want it to be set to other value
    /*        
    public AuthourType Type
    {
        get { return type; }
        set
        {
            if (value != AuthourType.Novelist)
            {
                throw new NotSupportedException("Type");
            }
            type = value;
        }
    }
    */
}
于 2013-01-17T06:55:06.817 に答える