gpt4 book ai didi

c++ - 使用多重继承来区分使用角色?

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:11 25 4
gpt4 key购买 nike

这又是我的飞行模拟应用程序。我现在要离开单纯的原型(prototype)制作阶段,现在开始充实软件设计。至少我尝试过..

模拟中的每架飞机都有一个与之关联的飞行计划,其确切性质与本题无关。足以说明运算符(operator)可以在模拟运行时编辑飞行计划。飞机模型大部分时间只需要读取访问飞行计划对象,最初认为这需要简单地传递一个常量引用。但有时飞机需要调用 AdvanceActiveWayPoint() 以指示已到达航路点。这将影响函数 ActiveWayPoint() 返回的迭代器。这意味着飞机模型确实需要非常量引用,这反过来也会向飞机模型公开 AppendWayPoint() 等函数。我想避免这种情况,因为我想在编译时强制执行上述使用规则。

请注意,class WayPointIter 相当于一个STL const 迭代器,即迭代器不能改变路点。

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;

(...)
};

我克服这个问题的想法是:为每个使用角色定义一个抽象接口(interface)类,并从两者继承 FlightPlan。然后每个用户只会获得适当使用角色的引用。

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() 但我觉得这可能是不必要的。

提前致谢!

最佳答案

从严格的设计角度来看,你的想法确实很好。它相当于拥有一个对象和对该对象的多个不同“ View ”。

但是这里存在缩放问题(与实现相关)。如果您有另一个对象 Foo 需要访问飞行计划,您会添加 IFlightPlanFoo 接口(interface)怎么办?

您可能很快就会面临继承问题的风险。

传统的做法是创建另一个对象,一个Proxy,并使用这个对象来适配/限制/控制使用。这是一个设计模式:Proxy

在这里您将创建:

class FlightPlanActiveWayPoint
{
public:
FlightPlanActiveWayPoint(FlightPlan& fp);

// forwarding
void foo() { fp.foo(); }

private:
FlightPlan& mFp;
};

为其提供您为 IFlightPlanActiveWayPoint 计划的接口(interface),使用对实际 FlightPlan 对象的引用构建它,然后转发调用。

这种方法有几个优点:

  • 依赖性:无需每次有新需求时都编辑flightPlan.h,因此无需重新构建整个应用
  • 速度更快,因为不再有虚拟调用,并且函数可以内联(因此几乎没有)。尽管我建议不要一开始就内联它们(这样您就可以修改它们而无需重新编译所有内容)。
  • 无需修改基类即可轻松添加检查/日志记录等(以防您在特定场景中遇到问题)

我的 2 美分。

关于c++ - 使用多重继承来区分使用角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2948438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com