K
K
Konstantin Knatarev2019-08-14 17:35:22
OOP
Konstantin Knatarev, 2019-08-14 17:35:22

Should the Adapter pattern look like this?

I tried to implement the adapter pattern with object composition and inheritance, it turned out something like this:
The question is whether I implemented this pattern correctly and, if not, what is the error or inaccuracy, please tell me?
(implementation with object composition)
5d541bae7288b794329331.png

class AbstractTarget {
public:
    virtual ~AbstractTarget() {};
    virtual void Request() = 0;
};

class ConcreteTarget : public AbstractTarget {
public:
    void Request() {
        //...реализация
    }
};

class AbstractAdaptee {
public:
    virtual ~AbstractAdaptee() {};
    virtual void SpecificRequest() = 0;
};

class ConcreteAdaptee : public AbstractAdaptee {
public:
    void SpecificRequest() {
        //...адаптируемая реализация
    }
};

class Adapter : public AbstractTarget {
public:
    Adapter(AbstractAdaptee* other_adaptee): adaptee(other_adaptee) {};
    void Request() {
        this->adaptee->SpecificRequest();
    }
protected:
    AbstractAdaptee* adaptee;
};

(implementation with inheritance)
5d541bd4ac014765922657.png
class AbstractTarget {
public:
    virtual ~AbstractTarget() {};
    virtual void Request() = 0;
};

class ConcreteTarget : public AbstractTarget {
public:
    void Request() {
        //...реализация
    }
};

class AbstractAdaptee {
public:
    virtual ~AbstractAdaptee() {};
    virtual void SpecificRequest() = 0;
};

class ConcreteAdaptee : public AbstractAdaptee {
public:
    void SpecificRequest() {
        //...адаптируемая реализация
    }
};

class Adapter: public AbstractTarget, private AbstractAdaptee {
public:
    void Request() {
        SpecificRequest();
    }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bonce, 2019-08-17
@Bonce

Yes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question