Avoid code duplication

Please refer to the following example.

using namespace std;
//Base interface
class IBase
{
public:
    virtual void BaseMethod1() = 0;

    virtual void BaseMethod2() = 0;
};

class IEntity1 : public IBase
{
public:
    virtual void Entity1Method1() = 0;

    virtual void Entity1Method2() = 0;
};

class Entity1 : public IEntity1
{
public:
    Entity();
//IBaseMethods
    void BaseMethod1();
    void BaseMethod2();
//IEntityMethods
    void Entity1Method1();
    void Entity1Method2();
//EntityMethods
    void Method1();
    void Method2();
};

In the above example, for all other objects that come from IBase, you need to implement BaseMethod1 () and BaseMethod2 (). What causes a lot of code duplication? Is there anyway where we can avoid the excessive implementation of IBase methods in objects derived from it?

+3
source share
4 answers

You can use virtual inheritance in combination with the default implementation base class to encapsulate the default base behavior and only have it inherited by the specific classes you want, for example:

using namespace std;
//Base interface
class IBase
{
public:
    virtual void BaseMethod1() = 0;
    virtual void BaseMethod2() = 0;
};

class IEntity1 : virtual public IBase
{
public:
    virtual void Entity1Method1() = 0;
    virtual void Entity1Method2() = 0;
};

class BaseImpl : virtual public IBase
{
public:
    virtual void BaseMethod1()
    {
        ...
    }
    virtual void BaseMethod2()
    {
        ...
    }
}

class Entity1 : public IEntity1, public BaseImpl
{
public:
    Entity1();
//IEntityMethods
    void Entity1Method1();
    void Entity1Method2();
//EntityMethods
    void Method1();
    void Method2();
};

, , , . , . .

, :

template<typename TEntity, typename TBaseImpl>
class ConcreteEntity: public TEntity, public TBaseImpl
{
public:
    ConcreteEntity() {}
};

class ConreteEntity1 : public ConcreteEntity<IEntity1, BaseImpl>
{
public:
    ConreteEntity1();

//IEntityMethods
    void Entity1Method1();
    void Entity1Method2();
//ConreteEntity1 Methods
    void Method1();
    void Method2();
};
0

, BaseMethod1(), .

- :

void BaseMethod1_common();

class Entity1 : public IEntity1
{
public:
    Entity();
//IBaseMethods
    void BaseMethod1() { BaseMethod1_common(); }
    void BaseMethod2();
//IEntityMethods
    void Entity1Method1();
    void Entity1Method2();
//EntityMethods
    void Method1();
    void Method2();
};
0

, IBase .

IBase: BaseMethod1() IBase:: BaseMethod1().

0

, , .

, , , .

, , :

  • , , .
  • .

:

//First Header and Cpp file
class Base_private
{
public:
    BaseImpl(arguments);
    ~BaseImpl();

    void BaseMethod1() {
        //Implementation
    }
    void BaseMethod2() {
        //Implementation
    }
};

//Second Header and Cpp file
class BaseInterface
{
public:
    BaseInterface(arguments);
    ~BaseInterface();

    void BaseMethod1() {
        m_pBase->BaseMethod1();
    }
    void BaseMethod2() {
        m_pBase->BaseMethod2();
    }
private:
    Base_private* m_pBase;
};

class Entity : public BaseInterface
{
public:
    Entity(arguments);
    ~Entity();

    void Method1();
    void Method2();
};
0

All Articles