我正在对提供数据的 API 进行更改(这是对我原来的问题的更新)。一些搜索需要有关作者的数据并获取IAuthor
对象。API 有一个IAuthor
接口和一个具体的实现类,IAuthor
称为Author
.
Search.GetBooksByAuthor
当作者被标记为小说家时,我需要更改该方法的行为以提供不同的语义。我听说过开放/封闭原则,似乎更改IAuthor
和/或Author
和/或Search
类会违反这一点(Book
不过,类肯定保持不变)。那么如何进行这个简单的改变呢?
例如,我最初在想这样的事情,但我的想法可能很不稳定,因为它涉及更改Search
类:
//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
}
}