COM design mode

xiaoxiao2021-03-05  26

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 copied from the VC:

template > class stack {// LIFO queue implemented with a containerpublic: typedef _Container container_type; typedef typename _Container :: value_type value_type; typedef typename _Container :: size_type size_type; stack (): c () {// construct with empty container} explicit stack (const _Container & _Cont): c (_Cont) {// construct by copying specified container} bool empty () const {// test if stack is empty return (c.empty ( )));} size_type size () const {// test length of stack return ());} value_type & top () {// Return Last Element of Mutable Stack Return (C.back ());} const Value_type & top () const {// return rust element of nonmutable stack return (c.back ());} void push (const value_type & _val) {// INSERT Element at end c.push_back (_val);} Void Pop () {// ERASE Last Element C.POP_BACK ();} Bool_eq (const stack <_ty, _container> & _right) const {// Test for stack equality return (c == _right.c);} bool _lt (con STT STACK <_TY, _CONTAINER> & _Right) const {// test if this <_right.c);} protected: _container c; // the underlying container}; key is _Container C All operations of Stack are transferred to C to handle it. (This is actually the "Object Adapter" mentioned earlier, pay attention to the Class Adapter in STL is incompletely consistent with the Class Adapter concept mentioned above)

The use of Stack is very simple, as follows:

{INT IA [] = {1, 3, 2, 4}; Deque ID (IA, IA 4); Stack IS (ID);}

(2) Recently, I have seen an article "Generic : Simplified Abnormal Security Code", original from http://www.cuj.com/experts/1812/alexandr.htm?topic=experts, Chinese translation from "C " VIEW No. 5. The article is absolutely first-class, and the Adaptor mode is also used in the code given, and there is also a representative. I generalize it, summarize the following example: Question: Suppose there are several existing classes, they have some common behaviors, but they are independent (no common base classes). Such as:

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 void test (t) {T.Proc ();} is really good, but this only applies to simple situations, sometimes it is very complicated, such as we can't put the type in the template parameter!

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 class adaptor: public odaptor, private t // Implement inheritance {public: Virtual Void Proc ( ) {T :: proc ();}}; // Call the function proc in a unified manner, instead of concern is T1, T2 or other Void Test (const std :: auto_ptr

& 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 Class Adaptor: Public Iadaptor, Private T / / Implement Inheritance {Public: Adaptor (INT N): T (N) {} Adaptor (CHAR C): T (c) {} Virtual Void Proc () {T :: proc ();} }; Class test {public: test (int N): sp (New Adaptor (N)) {} test (char C): SP (New Adaptor (C)) {} void proc () { SP-> Proc ();} private: std :: auto_ptr sp;}; client code: test t1 (10); t1.proc (); test t2 ('c'); t2.proc () The above is an example rather than an example, you may be more willing to look at its actual use. Go to download the author written by the author, enjoy it.

转载请注明原文地址:https://www.9cbs.com/read-33671.html

New Post(0)