Answer the question
In order to leave comments, you need to log in
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)
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;
};
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question