扩展(不改变)新行为的搜索类? [英] Extend (not change) search class for new behaviour?

查看:88
本文介绍了扩展(不改变)新行为的搜索类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了更改,供应数据的API(这是一个更新到我的原来的问题)。某些搜索大约需要作者的数据,并采取 IAuthor 对象。 API有一个 IAuthor 接口和一个具体的类,它实现 IAuthor 名为作者



我需要改变 Search.GetBooksByAuthor 方法的行为给予不同的语义当作者被标记为一个小说家。我听说过的开/常闭原则并又好像改变了 IAuthor 和/或作者和/或搜索类违反本(在图书类是绝对不变,虽然)。那么,如何使这个简单的变化?



例如,我本来想这样的,但我的想法可能是靠不住的,因为它涉及到改变的搜索类:

  //之前
类搜索
{
公共图书[] GetBooks(IAuthor作者){
//调用数据访问GetBooks ...
}
}


//后
类搜索
{
公共图书[] GetBooks(IAuthor作者){
//为了保持预先存在的行为
//调用数据访问GetBooks方法,SQL参数@AsNovelist =假...
//(或不通过任何东西,因为SQL参数默认为false)
}

公共图书[] GetBooksAsNovelist(IAuthor作者){
//要获得新的行为
//调用数据访问GetBooks方法,SQL参数@AsNovelist = TRUE
//让自己的非小说类书籍从结果$ b $省略b}
}


解决方案

这似乎是显而易见的的东西必须改变,以配合您知道作者是否是一个小说家,你可以做两种方式这一个。你不必改变什么理论,你不过需要一个新的类

 公共类小说家:作家,IAuthor {} 

然后就可以通过一个小说家到你的方法,然后deterimne你的作者的类型。

 类搜索
{
公共图书[] GetBooks(IAuthor作者){
如果(作者是小说家)
//做一些东西,或设置一个标志/布尔值
}
}

或如前面提到的,实现一个布尔成员到您的作者界面和检查。上面会阻止你改变你的类结构但是。



这意味着你的小说家其实还是一位作家,它只是有它自己的类型。你的方法签名保持不变,你的阶级结构保持不变,你只是有一个不同类型的作家,这在理论上是罚款类型。 。请致电如下测试



  GetBooks(新小说家()); 


I'm making a change to an API that serves data (this is an update to my original question). Some of the searches require data about an author and take a IAuthor object. The API has an IAuthor interface and a single concrete class that implements IAuthor called Author.

I need to change the behaviour of the Search.GetBooksByAuthor method to give different semantic when the author is flagged as a novelist. I've heard about the open/closed principle and it would seem that changing the IAuthor and/or Author and/or Search classes would violate this (the Book class is definitely remaining unchanged, though). How then to make this simple change?

For example, I was originally thinking something like this but my thinking is probably wonky because it involves changing the Search class:

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


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

   public Books[] GetBooksAsNovelist(IAuthor author){
       // To get new behaviour
       // call data access GetBooks method with SQL param @AsNovelist = true
       // so that their non-fiction books are omitted from results
    }
}

解决方案

It may seem obvious that something has to change to cater for knowing whether or not your author is a Novelist, you could do this one of two ways. You don't have to change anything in theory, you do however need a new class.

public class Novelist : Author, IAuthor { }

Then you can pass a novelist into your method and then deterimne your type of author.

class Search 
{
   public Books[] GetBooks(IAuthor author){
       if(author is Novelist)
         //Do some stuff or set a flag/bool value
    }
}

OR as previously mentioned, implement a boolean member to your Author interface and check that. The above would prevent you changing your class structures however.

This means that your novelist is in fact still an author, it just has it's own type. Your method signatures remain the same, your class structures remain the same you just have a type for a "different type of author", which should in theory be fine. Call as below to test.

GetBooks(new Novelist());

这篇关于扩展(不改变)新行为的搜索类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆