本文介紹了鉆石繼承(C++)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我知道擁有鉆石繼承被認為是不好的做法.但是,我有兩個案例,我覺得鉆石繼承非常適合.我想問一下,在這些情況下你會推薦我使用菱形繼承,還是有其他更好的設計.
I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there another design that could be better.
案例 1: 我想在我的系統中創建代表不同類型操作"的類.動作由幾個參數分類:
Case 1: I want to create classes that represent different kinds of "Actions" in my system. The actions are classified by several parameters:
- 操作可以是讀取"或寫入".
- 動作可以有延遲或沒有延遲(它不僅僅是 1 個參數.它會顯著改變行為).
- 動作的流類型"可以是 FlowA 或 FlowB.
我打算有以下設計:
// abstract classes
class Action
{
// methods relevant for all actions
};
class ActionRead : public virtual Action
{
// methods related to reading
};
class ActionWrite : public virtual Action
{
// methods related to writing
};
class ActionWithDelay : public virtual Action
{
// methods related to delay definition and handling
};
class ActionNoDelay : public virtual Action {/*...*
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!