Adapter mode for C + + design mode (iii)

Source: Internet
Author: User
Tags ticket

4. Adapter Mode Summary

In the object adapter mode, the adapter is associated with the adaptation, and in class adapter mode, the adapter is an inheritance relationship to the appropriate person. whether an object adapter or a class adapter, the adapter pattern transforms an existing interface into an interface expected by the client class, enabling the reuse of existing classes. How do you assign responsibilities in order to avoid direct coupling between two or more things? Most cases can be solved by adding a layer of indirection, assigning responsibility to the intermediary object as a medium between other artifacts or services to avoid direct coupling between them. In adapter mode, in order to avoid the customer class and the direct coupling with the adapter class, that is, the customer class directly access the adapter class, you can add an adaptor class to reduce the coupling between the client class and the adapter class. When the client class invokes the adapter's method, the method of the adapter class is called inside the adaptor class, and the process is transparent to the client class, and the client class does not directly access the adapter class. It is a design pattern with very high frequency and is widely used in software development.

1. Key Benefits

Both the object adapter mode and the class adapter pattern have the following advantages:

(1) decoupling the target class and the adapter class, by introducing an adapter class to reuse the existing adaptive class, without modifying the original structure.

(2) It increases the transparency and reusability of the class, encapsulates the specific business implementation process in the adapter class, is transparent to the client class, and improves the reusability of the ligand, the same adapter class can be reused in many different systems.

(3) flexibility and scalability are very good , through the use of configuration files, you can easily replace the adapter, you can not modify the original code based on the addition of new adapter classes, fully comply with the "open and close principle."

Specifically, the class adapter pattern has the following advantages:

Because the adapter class is a subclass of the adaptive class, it is possible to displace some of the adapters ' methods in the adapter class , making the adapter more flexible.

The object adapter pattern also has the following advantages:

(1) An object adapter can adapt multiple different adapters to the same target ;

(2) can be adapted to a subclass of an adapter, due to the correlation between the adaptor and the adaptation, according to the "Richter substitution principle", the sub-class of the adapter can also be adapted through the adaptor.

2. Main disadvantages

the disadvantages of the class adapter pattern are as follows:

(1) for Java, C # and other languages that do not support multiple-class inheritance, a maximum of one adapter class can be adapted at a time, not suitable for multiple adapters at the same time;

The disadvantages of the object adapter pattern are as follows:

Some of the methods to displace the adapter class in the adapters are cumbersome compared to the class adapter pattern. If we must replace one or more methods of the adapter class, we can make a subclass of the adaptive class, replace the method of the adaptive class, and then adapt the sub-class of the adaptive class as a true adapter, and the process is more complicated.

3. Adapter Mode specific Application

(1) Reuse has been written functional modules, such as: has written a staff information module, to re-develop a new enterprise management system, you can reuse the staff information module.

(2) Log information when the program is run, can be recorded to the database, can also be recorded in the TXT file. Now change the requirements so that log information can be recorded in both the database and the TXT file. You can add an adapter class to fit database operations and file operations.

(3) In the software development process, we will develop a number of functional modules. such as sorting algorithm module, find module, or encapsulate chain list, queue, tree and other operations. When these functions need to be reused, they are used directly without the need to re-develop these functional modules.

(4) Online Banking encryption Module development: has developed a set of net-Silver encryption module, now need to re-develop a similar software, you can directly reuse the encryption module has been developed.

(5) A company has developed a train ticket recognition software, including two value, denoising, layout analysis, column extraction and other modules. Now to develop a bank card recognition project, you can directly reuse the fire ticket recognition in the function module.

(6) A company wants to develop a set of input method software, can buy Sogou Input method search module, directly to reuse, no need to develop their own.

(7) Life of the adapter: Power adapter, USB to serial cable, water pipe specialized workers use a variety of interface connections can not be connected to the pipeline.

Adapter mode for C + + design mode (iii)

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.