這是我的飛行模擬應用程序。我現在正離開純粹的原型設計階段,現在開始充實軟件設計。至少我試試..使用多重繼承來區分使用角色?
模擬中的每架飛機都有與之相關的飛行計劃,其確切性質對此問題毫無興趣。足以說運營商在模擬運行時編輯飛行計劃。大多數時候飛機模型只需要讀取飛行計劃對象,這個對象起初被認爲只是傳遞一個const引用。但有些情況下,飛機需要撥打AdvanceActiveWayPoint()
來表明已到達航點。這會影響函數ActiveWayPoint()
返回的迭代器。這意味着飛機模型確實需要一個非常量參考,這反過來也會將諸如AppendWayPoint()
之類的函數暴露給飛機模型。我想避免這種情況,因爲我想在編譯時強制執行上述使用規則。
請注意,class WayPointIter
等同於STL常量迭代器,也就是說迭代器不能改變點的方式。
class FlightPlan
{
public:
void AppendWayPoint(const WayPointIter& at, WayPoint new_wp);
void ReplaceWayPoint(const WayPointIter& ar, WayPoint new_wp);
void RemoveWayPoint(WayPointIter at);
(...)
WayPointIter First() const;
WayPointIter Last() const;
WayPointIter Active() const;
void AdvanceActiveWayPoint() const;
(...)
};
我的想法來克服這個問題是這樣的:定義一個抽象接口類爲每個使用的作用,從兩個繼承航班計劃。然後,每個用戶只會獲得相應用戶角色的引用。
class IFlightPlanActiveWayPoint
{
public:
WayPointIter Active() const =0;
void AdvanceActiveWayPoint() const =0;
};
class IFlightPlanEditable
{
public:
void AppendWayPoint(const WayPointIter& at, WayPoint new_wp);
void ReplaceWayPoint(const WayPointIter& ar, WayPoint new_wp);
void RemoveWayPoint(WayPointIter at);
(...)
};
這樣的FlightPlan
聲明只需要更改爲:
class FlightPlan : public IFlightPlanActiveWayPoint, IFlightPlanEditable
{
(...)
};
你覺得呢?有沒有我可能錯過的窟窿?爲了清楚起見,這個設計是否清晰?還是我應該想出不同的想法?
或者我也可以定義一個特殊的ActiveWayPoint
類,它將包含函數AdvanceActiveWayPoint()
,但覺得這可能是不必要的。
提前致謝!
謝謝!包括日誌記錄和額外檢查的可能性是我認爲非常有價值的補充。 – Arne 2010-06-01 09:45:45