在下面的示例中,派生類的作者將期望調用base.Add()。如果它發生在第一,基地可以做一種代碼。如果最後發生,基地可以做另一種邏輯(見樣本)。我似乎不可能有兩種方式。簡單的解決方法是停止調用基本方法,因爲基礎永遠不會知道它是被第一次,最後還是中間還是兩次調用!何時調用base.method()以及base.method()中應該放置什麼代碼?
什麼是面向對象的方式來處理這個問題?我應該簡單地停止將代碼放入基本方法中,因爲我永遠不會知道前置條件和後置條件嗎?
編輯:目標是有一個做CRUD操作的業務對象類。重複的代碼將被移動到基類。例如,檢查在添加記錄之前是否有業務對象的id爲0,並在保存後檢查業務對象的id是否大於0。
namespace StackOverFlowSample
{
class BusinessObjectBase
{
private bool _isNew;
private int _id;
public virtual void Add(string newAccount)
{
//Code that happens when subclasses run this method with the
//same signature
//makes sense if base is called 1st
if(_isNew && _id>0) throw new InvalidOperationException("Invalid precondition state");
//makes sense if bae is called 2nd
if (!_isNew && _id == 0) throw new InvalidOperationException("Invalid post condition state");
}
}
class BusinessObject : BusinessObjectBase {
public override void Add(string newAccount)
{
//doesn't make sense, because base will need to be called again.
base.Add(newAccount);//pre validation, logging
//Save newAccount to database
//doesn't make sense, because base has already been called
base.Add(newAccount); //post validation, logging
}
}
}
基本上和我一樣的答案,除了一分鐘之前......我可以建議你設置AddInternal保護嗎? – 2009-06-26 14:17:10
@bruno:絕對; AddInternal應該受到保護,這是我的疏忽。感謝您指出。 – 2009-06-26 14:21:31