Design Pattern Learning Notes-Adapter mode

Source: Internet
Author: User

I. INTRODUCTION
Learn about the adapter modes in 23 design modes today. When it comes to adapters, maybe the first thing we think about is the power adapter, yes, the adapter mode is the same as the power adapter. For example, our daily use of electricity is 220V of voltage, some appliances can be used directly, but some appliances do not use such a high voltage, may only need a few volts. And if we want to use it, we need to add a power adapter that changes the voltage through the adapter. Adapter mode is the same, for example, we have a non-source library file, only the corresponding header file, but we still want to use the library class, and the interface is not the same, we can through the adapter mode, the Library class "wrapper" through the adapter to use the library indirectly.Here's a look at the definition of the adapter pattern and the UML class diagram for the adapter pattern: Adapter mode (Adapter pattern): Convert one interface to another interface that the customer wants, and the classes that are incompatible with the interface can work together, with the alias of the wrapper (Wrapper).
Two. Adapter Mode example
Take the example of the power adapter we raised before, we now have a few household appliances, are 220V, and then we bought a foreign import from abroad, using 110V voltage, to use this stuff, we need to add a power adapter. Well, nonsense not much to say, on the code:
1. No adapter condition
Design Pattern.cpp:Defines The entry point for the console application.//#include "stdafx.h" #include <iostream> #include <vector>using namespace std;//domestic appliances, working in 220Vclass chineseappliances{public:virtual void workon220v () { cout << "220V normal work" << endl;}};/ /foreign appliances, working at 110Vclass foreignappliances{public:virtual Void workon110v () {cout << 110V normal work << endl;}};/ /Domestic Electrical Use interface, we understand the socket bar class chinesesocket{private:vector<chineseappliances*> m_socket;public://access appliance void Addappliances (chineseappliances* app) {m_socket.push_back (app);} Open work void works () {for (Vector<chineseappliances*>::iterator it = M_socket.begin (); It! = M_socket.end (); ++it) {(* IT)->workon220v ();}}; int _tmain (int argc, _tchar* argv[]) {//Domestic electrical appliances container (understood as socket bar) chinesesocket* Chinesesocket = new Chinesesocket (); chineseappliances* Chinesetv = new Chineseappliances (); foreignappliances* Foreigntv = new Foreignappliances ();//We have a domestic outlet, can directly use the domestic electrical chinesesocket->addappliances ( CHINESETV); ChinesesockeT->work ();//However, if there is no power adapter, we can not use foreign appliances on the socket//In other words, even if we can use, but also not in accordance with domestic standards to use, we must call the additional//foreigntv->workon110v (); System ("pause"); return 0;}
Results:220V Normal operation
Please press any key to continue ...
2. Use of the adapter case
Design Pattern.cpp:Defines The entry point for the console application.//#include "stdafx.h" #include <iostream> #include <vector>using namespace std;//domestic appliances, working in 220Vclass chineseappliances{public:virtual void workon220v () { cout << "220V normal work" << endl;}};/ /foreign appliances, working at 110Vclass foreignappliances{public:virtual Void workon110v () {cout << 110V normal work << endl;}};/ /Domestic Electrical Use interface, we understand the socket bar class chinesesocket{private:vector<chineseappliances*> m_socket;public://access appliance void Addappliances (chineseappliances* app) {m_socket.push_back (app);} Open work void works () {for (Vector<chineseappliances*>::iterator it = M_socket.begin (); It! = M_socket.end (); ++it) {(* IT)->workon220v ();}}};/ /power Adapter Class Adapter:public chineseappliances{private://object adapter, save an object instance that needs to be adapted foreignappliances* m_foreignappliances ;p Ublic:adapter (foreignappliances* app): M_foreignappliances (APP) {cout << "I'm the adapter! "<< Endl;} virtual void workon220v () override{cout << "through adapter"; m_foreignappliances->workon110v ();}}; int _tmain (int argc, _tchar* argv[]) {//Domestic electrical appliances container (understood as socket bar) chinesesocket* Chinesesocket = new Chinesesocket (); chineseappliances* Chinesetv = new Chineseappliances (); foreignappliances* Foreigntv = new Foreignappliances (); adapter* Adapter = new Adapter (FOREIGNTV);//We have a domestic outlet, can directly use the domestic electrical chinesesocket->addappliances (CHINESETV); With the adapter, our foreign appliances can be used as domestic appliances, sockets comrades on their chinesesocket->addappliances (adapter); Chinesesocket->work (); System ( "Pause"); return 0;}
Result: I am the adapter!
220V normal operation
Works properly with adapter 110V
Please press any key to continue ...
Through the above two examples, we can see that, if there is no adapter, we would not be able to use the domestic outlet for foreign electrical appliances, because the interface is different, foreign design interface is 110V, and the domestic interface is 220V, this is a fact can not be changed, so we can not modify both sides of any one interface , can only be remedied, through a certain way, the interface is packaged to adapt to another interface, we are indirectly called through the adapter object Foreignappliances class function.
Three. Adapter Mode Summary
Finally, summarize the pros and cons of the adapter pattern and the timing of the use. Advantages:1) It is good to reuse existing class objects without duplication of development and has good reusability.2) Modify the addition of a new adaptation class object, without modifying the original content, in line with the open-closed principle.3) because the adapter class is a subclass of an adapter class, it is possible to displace some of the adapters ' methods in the adaptor class, making the adapter more flexible. Disadvantages:1) Adapter mode is a type of compensation mode, if the interface can be designed in advance to unify, the adapter mode is not required at all. Just like the voltage standards in different countries. Use time:As pointed out in the previous shortcomings, if we can design the interface in advance to be particularly reasonable, we do not need the adapter mode at all. However, not all of us can calculate in advance. For example, a project has been in progress for a long time, the new features just have the current several modules can work together to complete this function, but the interface is different, we can not change the original interface of these modules, so only through the adapter mode, one of the objects will be wrapped up, indirect use. In addition, for example, we use some third-party libraries, their interfaces with our expectations must be different, to use, must be the appropriate encapsulation, then we can use the adapter mode!

Design Pattern Learning Notes-Adapter mode

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: info-contact@alibabacloud.com 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.