我正在對提供數據的API進行更改(這是對我的original question的更新)。某些搜索需要關於作者的數據並採用IAuthor
對象。該API具有一個IAuthor
接口和一個實現IAuthor
的單個具體類,稱爲Author
。擴展(不更改)搜索類的新行爲?
我需要改變Search.GetBooksByAuthor
方法的行爲,以便在作者被標記爲小說家時賦予不同的語義。我聽說open/closed principle,看起來改變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
}
}
你能簡單地定義一個布爾成員'IsNovelist'在界面中? – LukeHennerley