假設你有這樣的代碼:「基類PARAMS並不總是使用」代碼味道
public Base
{
abstract void Register();
}
public Registrator1: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
public Registrator2: Base
{
override void Register()
{
//uses the current state of the object to populate the UI captions
}
}
但是當你收到這實際上寄存器基於某些參數的新業務規則要求你寫Registrator3和更改您的代碼基地到下一個:
public Base
{
abstract void Register(externalParam);
}
public Registrator1: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate theUI
}
}
public Registrator2: Base
{
override void Register(externalParam)
{
//uses the current state of the object to populate the UI
}
}
public Registrator3: Base
{
override void Register(externalParam)
{
//uses a DDD - service passed in the params to populate the UI
}
}
但是註冊器1和註冊器2不需要該參數和代碼變得臭。有什麼方法可以重寫這段代碼?
如果你提到oop你可以添加一些虛擬的方法也許。這看起來像代碼氣味「目前同名的新方法」。 –
@Valentin Kuzub,完成 –
我還是遇到了這個問題。最初的方法沒有參數,這意味着它只是操縱它所調用的對象的狀態。它不清楚該登記冊實際上在做什麼,因此很難想到缺陷或更好的方式。當我們添加一個externalParam類似於完全改變了整個圖片時,現在註冊者正在接受這些東西,並且可能會根據這些參數註冊參數或其他內容,這意味着它的狀態已不夠了嗎?無論如何,我認爲如果你想得到一些好的建議,你可以擴大這個問題,例如爲註冊函數顯示一些僞代碼。 –