从两个接口实现创建实例
Creating instance from two interface implementations
我想要的是拥有一个对象,其中一些方法以特定方式实现,而其他实现方式不同。我决定使用两个接口。
我们称它为 InterfaceA
和 InterfaceB
。这两个接口,可以有不同的实现。让我显示代码:
class InterfaceA
{
public:
virtual int method_A_foo(void) = 0;
virtual int method_A_bar(void) = 0;
};
class InterfaceB
{
public:
virtual int method_B_foo(void) = 0;
virtual int method_B_bar(void) = 0;
};
现在我有几个 InterfaceA
的实现,假设我有 IA_instance1
、IA_instance2
。该实现是 InterfaceB
的基础,它使用 InterfaceA
中的方法,实现:
class IA_instance1 : public InterfaceA
{
public:
int method_A_foo(void);
int method_A_bar(void);
};
class IA_instance2 : public InterfaceA
{
public:
int method_A_foo(void);
int method_A_bar(void);
};
class IB_instance : public InterfaceB
{
public:
/* in implementation of this method i want to call method_A_foo() twice */
int method_B_foo(void);
/* in implementation of this method i want to call method_A_foo() 4 times and method_A_bar() one time*/
int method_B_bar(void);
};
我所做的是创建了一个包含两个接口的抽象 class。 InterfaceA
实现存在于这个抽象 class 中,并且 InterfaceB
实现被移动到派生的 class:
class InterfaceBase : public InterfaceA, InterfaceB
{
private:
InterfaceA * m_interfaceA;
public:
InterfaceBase(InterfaceA * _interfaceA)
{
this->m_interfaceA = _interfaceA;
}
int method_A_foo(void)
{
return this->m_interfaceA->method_A_foo();
}
int method_A_bar(void)
{
return this->m_interfaceA->method_A_bar();
}
virtual int method_B_foo(void) = 0;
virtual int method_B_bar(void) = 0;
};
class IB_instance1 : public InterfaceBase
{
public:
IB_instance1(InterfaceA * _interafceA)
: InterfaceBase(_interafceA)
{ }
int method_B_foo(void);
int method_B_bar(void);
};
现在我需要的是实现 InterfaceA
,并将其传递给 IB_instance1
的构造函数。我不创建实现这两个接口的 class,因此我不复制 InterfaceA
实现。我使用 InterfaceA
的一个实例并将其传递给 InterfaceBase
包装器。我可以执行以下操作:
InterfaceA * instanceA1 = new IA_instance1();
InterfaceBase * instanceB1 = new IB_instance1(instanceA1);
我有以下问题:
- 用这种方式解决这个问题是个好主意吗?我可以做得更好吗?
- 我需要在
InterfaceBase
中复制 virtual int
声明吗?如果我删除它们,我将无法在转换为 InterfaceBase *
后调用 InterfaceB
方法。我可以调用 InterfaceA
方法但是对于 InterfaceB
我必须在 InterfaceBase
中放置 virtual int
声明
但是,对我来说这似乎是个好方法,我认为编写声明和定义并使用单独的 .h 和 .cpp 文件很糟糕。
Need I to duplicate virtual int declarations in InterfaceBase
? If I remove them I won't be able to call InterfaceB
methods after cast to InterfaceBase *
. I am able to call InterfaceA
methods but for InterfaceB
I have to put virtual int declarations in InterfaceBase
.
这是因为您私下继承自InterfaceB
,而不是公开继承。将您的代码更改为
class InterfaceBase : public InterfaceA, public InterfaceB
{
/* Your implementation without repeating InterfaceB */
};
我想要的是拥有一个对象,其中一些方法以特定方式实现,而其他实现方式不同。我决定使用两个接口。
我们称它为 InterfaceA
和 InterfaceB
。这两个接口,可以有不同的实现。让我显示代码:
class InterfaceA
{
public:
virtual int method_A_foo(void) = 0;
virtual int method_A_bar(void) = 0;
};
class InterfaceB
{
public:
virtual int method_B_foo(void) = 0;
virtual int method_B_bar(void) = 0;
};
现在我有几个 InterfaceA
的实现,假设我有 IA_instance1
、IA_instance2
。该实现是 InterfaceB
的基础,它使用 InterfaceA
中的方法,实现:
class IA_instance1 : public InterfaceA
{
public:
int method_A_foo(void);
int method_A_bar(void);
};
class IA_instance2 : public InterfaceA
{
public:
int method_A_foo(void);
int method_A_bar(void);
};
class IB_instance : public InterfaceB
{
public:
/* in implementation of this method i want to call method_A_foo() twice */
int method_B_foo(void);
/* in implementation of this method i want to call method_A_foo() 4 times and method_A_bar() one time*/
int method_B_bar(void);
};
我所做的是创建了一个包含两个接口的抽象 class。 InterfaceA
实现存在于这个抽象 class 中,并且 InterfaceB
实现被移动到派生的 class:
class InterfaceBase : public InterfaceA, InterfaceB
{
private:
InterfaceA * m_interfaceA;
public:
InterfaceBase(InterfaceA * _interfaceA)
{
this->m_interfaceA = _interfaceA;
}
int method_A_foo(void)
{
return this->m_interfaceA->method_A_foo();
}
int method_A_bar(void)
{
return this->m_interfaceA->method_A_bar();
}
virtual int method_B_foo(void) = 0;
virtual int method_B_bar(void) = 0;
};
class IB_instance1 : public InterfaceBase
{
public:
IB_instance1(InterfaceA * _interafceA)
: InterfaceBase(_interafceA)
{ }
int method_B_foo(void);
int method_B_bar(void);
};
现在我需要的是实现 InterfaceA
,并将其传递给 IB_instance1
的构造函数。我不创建实现这两个接口的 class,因此我不复制 InterfaceA
实现。我使用 InterfaceA
的一个实例并将其传递给 InterfaceBase
包装器。我可以执行以下操作:
InterfaceA * instanceA1 = new IA_instance1();
InterfaceBase * instanceB1 = new IB_instance1(instanceA1);
我有以下问题:
- 用这种方式解决这个问题是个好主意吗?我可以做得更好吗?
- 我需要在
InterfaceBase
中复制virtual int
声明吗?如果我删除它们,我将无法在转换为InterfaceBase *
后调用InterfaceB
方法。我可以调用InterfaceA
方法但是对于InterfaceB
我必须在InterfaceBase
中放置
virtual int
声明
但是,对我来说这似乎是个好方法,我认为编写声明和定义并使用单独的 .h 和 .cpp 文件很糟糕。
Need I to duplicate virtual int declarations in
InterfaceBase
? If I remove them I won't be able to callInterfaceB
methods after cast toInterfaceBase *
. I am able to callInterfaceA
methods but forInterfaceB
I have to put virtual int declarations inInterfaceBase
.
这是因为您私下继承自InterfaceB
,而不是公开继承。将您的代码更改为
class InterfaceBase : public InterfaceA, public InterfaceB
{
/* Your implementation without repeating InterfaceB */
};