如何在不丢失接口的情况下避免代码重复?

时间:2014-06-26 08:27:28

标签: c++ inheritance design-patterns

以下是当前的代码设计(示例)。如何避免重复“methodParent()”的代码(在两个子类的实现中)而不丢失接口类?

//Interfaces
//=======================================================

class InterfaceParent() //Interface class 
{
   public:
     virtual void methodParent() = 0; 
}; 

class InterfaceChild1() : public InterfaceParent //Interface class 
{
    public:
     virtual void methodParent() = 0; 
     virtual void methodChild1() = 0; 
}; 
 class InterfaceChild2() : public InterfaceParent //Interface class 
{
    public:
    virtual void methodParent() = 0; 
    virtual void methodChild2() = 0;
}; 

// Concrete Classes
//=========================================================

 class Child1() : public InterfaceChild1  // Concrete Class
 {
    public:
     void methodParent() { cout << "PARENT_METHOD";  } 
     void methodChild1() { cout << "CHILD_1_METHOD"; } 
}; 
class Child2() : public InterfaceChild2 // Concrete Class
{
    public:
      void methodParent() { cout << "PARENT_METHOD";  } 
      void methodChild2() { cout << "CHILD_2_METHOD"; } 
}; 

提前感谢您的帮助!

桑托什

2 个答案:

答案 0 :(得分:1)

这个问题有点奇怪,因为您通常会使用虚拟实际覆盖具有类的特定行为的方法,但在您的情况下,methodParent()始终是相同的。

您可以添加实现的私有继承(请参阅有效的C ++,明智地使用多重继承作为示例)

class InterfaceParentImpl() //Interface class 
{
public:
  void methodParent() { cout << "PARENT_METHOD";  }
}; 

class Child1 : public InterfaceChild1, private InterfaceParentImpl
{
  void methodParent() { InterfaceParentImpl::methodParent();  } 
}

您仍然需要多次写入,但如果您想要更改它,则只需要在一个地方进行。

答案 1 :(得分:0)

  

如果你看到&#34; methodParent()&#34;,我需要在所有孩子中实现它   类和重复的代码(以及维护   (对我而言)。

然后实现Abstract class而不是您的界面。基本上它是一个界面(包含需要在儿童中实现的纯虚拟方法),但也包含一些非纯粹的&#34; (已经实现)虚拟方法,可以在以后需要时覆盖。

  

通常,抽象类用于定义实现和   旨在从具体类继承。   More here

我会这样做:

// abstract
class AParent() //Abstract class 
{
   public:
    virtual void methodParent() { ... }; // give a first implementation that can be overriden later on, only if needed
    virtual void methodeChild() = 0

}; 
//Now the concretes
class Child1() : public AParent 
{
    public:
     virtual void methodParent() { ... }; // Override (as an example, only if needed)
     virtual void methodChild() { ... }; //implement
}; 

class InterfaceChild() : public AParent 
{
    public:
    //void methodParent() // is inherited from AParent
    virtual void methodChild() { ... }; // implement
};

编辑如果您无法改变任何内容,请执行以下操作:

但是......这太丑了:)

//Interfaces
//=======================================================

class InterfaceParent() //Interface class 
{
public:
 virtual void methodParent() = 0;    
}; 
class InterfaceChild1() : public InterfaceParent //Interface class 
{
public:
 virtual void methodParent() = 0; 
 virtual void methodChild1() = 0; 
}; 
class InterfaceChild2() : public InterfaceParent //Interface class 
{
public:
virtual void methodParent() = 0; 
virtual void methodChild2() = 0;
}; 

//Abstract
//=======================================================
//an abstract class to do the transition betwin interfacesChildXX and concrete classes
class AChildXX() : public InterfaceChildXX  // Concrete Class
{
public:
 virtual void methodParent() { cout << "PARENT_METHOD";  } //It's implemented here for all your childrens, but can still be overriden
 virtual void methodChildXX() = 0; 
};

// Concrete Classes
//=========================================================

class Child1() : public AChildXX  // Concrete Class
{
public:
 //void methodParent() { cout << "PARENT_METHOD";  } //It's inherited
 void methodChild1() { cout << "CHILD_1_METHOD"; } 
}; 
class Child2() : public AChildXX // Concrete Class
{
public:
  // void methodParent() { cout << "PARENT_METHOD";  } //It's inherited
  void methodChild2() { cout << "CHILD_2_METHOD"; } 
};