【问题标题】:Modeling "optional" inheritance建模“可选”继承
【发布时间】:2009-11-19 23:54:35
【问题描述】:

我在决定如何建模这种关系时遇到了麻烦......

所有 Boss 都可以做某些事情并拥有某些东西(速度、健康等),因此这些是“主要”抽象 Boss 类的一部分。

class Boss // An abstract base class
{
    //Stuff that all Bosses can do/have and pure virtual functions
};

现在我想为可以射击的老板指定更多的纯虚拟功能和成员。我想知道我应该如何建模?我考虑过从 Boss 类派生一个 ShootingBoss 类,但特定的老板本身就是类(Boss 只是它们派生自的抽象基类。)因此,如果 ShootingBoss 派生自 Boss,而特定的老板派生自ShootingBoss,那个boss将无法访问Boss类中的受保护数据。

Boss(ABC) -> ShootingBoss(ABC) -> SomeSpecificBoss(can't access protected data from Boss?)

基本上,我想知道推荐的建模方法是什么。任何帮助表示赞赏。如果需要更多信息,我很乐意提供。

【问题讨论】:

  • SomeSpecificBoss 可以访问受保护的 Boss 成员。 SomeSpecificBoss 不是直接派生的没关系,只要它是以某种方式派生的。
  • 谢谢。出于某种原因,我认为 protected 只下降了一级。
  • @drhirsch - 实际上,如果 ShootingBoss 是从 Boss 私下继承的,那么 SomeSpecificBoss 将无法访问 Boss 中的任何受保护成员。
  • 我应该指定它是公共继承。

标签: c++ inheritance


【解决方案1】:

我认为您需要查看 Mixin 类。

例如,您可以创建以下类:

class Boss {
    // Here you will include all (pure virtual) methods which are common
    // to all bosses, and all bosses MUST implement.
};

class Shooter {
    // This is a mixin class which defines shooting capabilities
    // Here you will include all (pure virtual) methods which are common
    // to all shooters, and all shooters MUST implement.
};

class ShootingBoss : public Boss, public Shooter
{
    // A boss who shoots!
    // This is where you implement the correct behaviour.
};

Mixins 需要使用multiple inheritance,这样做有很多陷阱和复杂性。我建议您查看this one 等问题的答案,以确保避免这些陷阱。

【讨论】:

  • 谢谢,我不确定这是否适用于我正在尝试做的事情,但是有用的建议。
【解决方案2】:

为什么不开始使用接口呢?因此,您可以将自己的东西分散到功能中,而不是简单的 uber 基类。

struct IBoss : public IObject
{
}
struct ICanShoot : public IObject
{
}

通常要实现这一点,您从另一个允许您查询接口的接口派生接口。

struct IObject
{
 int getId(); // returns a unique ID for this interface.
 int addRef();
 int release();
 bool queryInterface(int id, void** pp);
}

这样,您可以更轻松地实现您的 Boss:

class Boss : public IBoss, public ICanShoot
{
};

这对某些人来说可能有点矫枉过正,但如果你的班级体系都搞砸了,这是摆脱困境的最佳方式。

看看 M$ 的 IUnknown 接口。

【讨论】:

  • IObject 的子类应将父类声明为虚拟。否则无法进行多重继承。如果需要在 IObject 中使用非默认构造函数,这将变得非常复杂
【解决方案3】:

有两种不同的方法:
1) Mixin 类(已经解释)
2) 角色扮演课程。
角色扮演有它的优点和缺点。该对象可以扮演的角色(老板、射手等)是使用遏制来实现的。它们必须从公共基接口类派生,必须动态地向下转换(啊……)。调用者将向您的类的对象询问角色指针(这是向下转换的来源),如果对象可以扮演角色(返回的非空指针),客户端将调用角色的适当函数。 角色扮演方法的主要优点(除了避免多重继承) - 它是动态的。对象可以在运行时接受新角色,而不是必须在编译时定义的 mixin。
它也是可扩展的。在多重继承(mixin)方法中,如果您决定使用“Protector”扩展层次结构并说老板可以是简单的Boss、ShootingBoss、ProtectingBoss、ShootingProtectingBoss,然后用 @ 进一步扩展它987654326@ (Boss, ShootingBoss, ProtectingBoss, ShootingProtectingBoss, CowardBoss, CowardShootingBoss, CowardProtectingBoss, CowardShootingProtectingBoss) - 你看到你的层次结构爆炸了。这是您需要切换到角色扮演模型的时候,其中对象只需接受新角色Coward。但是直到你确定你需要它——坚持使用 mixin 类。 下面是角色扮演 lcases 的层次结构示意图:

class IRole
{
 // some very basic common interface here
public:
 virtual ~IRole() {}
};

class IBoss : public IRole
{
};

class IShooter : public IRole
{
};

class IProtector : public IRole
{
};

class MultifunctionalPerson
{
public:
 bool AcceptRole(IRole* pRole); // pass some concrete role here
 IRole* GetRole(int roleID);
};


// your clinet will be using it like that
MultifunctionalPerson& clPerson = ... (coming from somewhere);

// dynamic_cast below may be replaced with static_cast if you are sure
// that role at PROTECTOR_ROLE location is guaranteed to be of IProtector type or NULL
IProtector* pProtector = dynamic_cast<IProtector*>(clPerson.GetRole(PROTECTOR_ROLE));
if( 0 != pProtector )
{
 pProtector->DoSomething();
}

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2011-07-14
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多