Design Mode-adapter mode (C ++ implementation)

Source: Internet
Author: User

In the design mode,Adapter ModeAdapter pattern is sometimes called a packaging style or packaging. Transfers the interface of a class to what the user expects. An adaptation means that a class cannot work together because the interface is incompatible. The method is to wrap its own interface in an existing class.

There are two types of adapter modes:

· Object Adapter mode-in this adapter mode, the adapter holds an instance of the class that it wraps. In this case, the adapter calls the physical entity of the wrapped object.

· Class adapter mode-in this adapter mode, the adapter inherits its own implemented classes (generally multiple inheritance is implemented ).


I can't think of any better examples of class adapters here. Let's talk about object adapters first. We all know the stacks and queues in C ++ STL? There is a standard container deque dual-end queue in the standard library, which supports

The preceding deletion and insertion operations also support deletion and insertion. However, our queue is a forward and backward entry, while the stack is a backward entry. The standard library does not need to redefine them, it is implemented by using the base container. By default, if you want to change the deque, you can also use the vector, which only needs to be written as follows:Code:

Stack <int, vector <int> sta;

Now, let's fully implement an adapter Stack:

Template <class type, class basecon = deque <type> // The default parameter of the template is dequeclass stack {PRIVATE: basecon stack; // defines the base container public: void push (type ELEM) {stack. push_back (ELEM);} void POP () {stack. pop_back ();} type & Top () {return stack. back ();} bool empty () {return stack. empty ();} signed int size () {return stack. size () ;}}; int main () {stack <int, vector <int> stack; For (INT I = 1; I <10; I ++) stack. push (I); While (! Stack. Empty () {cout <stack. Top (); stack. Pop ();} system ("pause"); Return 0 ;}

I do not know whether I have made any effort in the above example. I personally prefer STL and have studied his source code. Recently I am also thinking about imitating STL to write my own library.

Back to the question, thisProgramSatck in is the standard stack implemented in the standard library, but I cannot find the memory distributor previously written, or I can simulate and adapt it completely. This stack is called the container adapter, he switched from an existing interface to the interface he wanted. This is the adapter mode. The iterator adapter in the standard library and the function Object Adapter both adopt this mode, for example, the back_insert iterator in the iterator adapter is based on the push_back function of the standard container, so the container with this function must be able to use this back_insert. I believe it is easy to understand and it is too late, sleeping !!




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.