Adapter
First, the function converts a class's interface into another interface that customers want,
Solve the problem of mismatch between two existing interfaces. Adapter mode makes it possible to work with those classes that are not compatible with interfaces.
Second, the structure map (1) Class Adapter
(2) Object Adapter
Third, advantages and disadvantages
Fourth, like many other modes, the focus of learning design model is to learn the idea of each mode, and should not be stabilized to its specific structural map and implementation. Because the mode is flexible, it can be a thousand variable, but the so-called change does not leave the Zone. A large amount of Adapter mode is used in STL, like Function Adapter, Iterator Adpter, which is not the same as the Adapter structure here, but the thoughts are the same. Specific introduction can go to Houjie website to find related articles, he is very good.
V. Sample Code (1) Class Adapter
namespace DesignPattern_Adapter {// class Adaptee class Adaptee {public: void SpecialRequest () {}}; // class Target class Target {public: virtual void Request () = 0;}; // class Adapter class Adapter: public Target, private Adaptee {public: Virtual void request () {specialRequest ();}};} client code: {using namespace designPattern_Adapter; target * p = new adapter (); p-> request (); // actually call Adaptee :: SpecialRequest ()} (2) Object Adapter
namespace DesignPattern_Adapter {// class Adaptee class Adaptee {public: void SpecialRequest () {}}; // class Target class Target {public: virtual void Request () = 0;}; // class Adapter class Adapter: public Target {public : virtual void request () {_adaptee.specialRequest ();} private: adaptee _adaptee;};} client code: {using namespace design_adapter; target * p = new adapter (); p-> request (); // actual Adaptee :: specialRequest ()}
Six, instances (1) Class Adapter in STL
Adapter Class in STL includes: a.stack (corresponding Adaptee is Deque). B.Queue (the corresponding Adaptee is Deque). C.Priority_Queue (the corresponding Adaptee is vector). Below is the class definition of the class definition from
template
The use of Stack is very simple, as follows:
{INT IA [] = {1, 3, 2, 4}; Deque
(2) Recently, I have seen an article "Generic
Class T1 {public: void proc () {}}; class t2 {public: void proc () {}}; // ...
How to call these behaviors in a unified way?
Solution 1: It will be very natural, you will think of template, such as:
Template
Workaround 2: Difficulties come from these classes without a common base class, so we create a base class and then Adapt.
// Class Iadaptor, abstract base class class adaptor {public: Virtual void proc () = 0;}; // Class Adaptortemplate
& SP)
{
SP-> proc ();
}
Client code:
TEST (std :: auto_ptr
New Adaptor
));
TEST (std :: auto_ptr
New Adaptor
));
The above example is very simple, and the template function in the method can be solved very well. Here is a slightly complicated example, create an appropriate object according to the parameter type:
Class T1 {public: T1 (int) {/ *...*/} void proc () {/ / {/ {/ {/}; Class T2 {public: T2 (char) {/*...// } void proc () {/ /}}; // class}}; // Class Iadaptor, abstract base class class odaptor {public: virtual void proc () = 0;}; // Class Adaptortemplate