Design Patterns ---- adapter patterns

Source: Internet
Author: User

Design Patterns ---- adapter patterns

Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

Solved problems:
That is, the Adapter mode makes those classes that cannot work together due to interface incompatibility work together.

Class diagram:

 

Role in the Mode
Target Interface (Target): The interface the customer expects. The target can be a specific or abstract class or interface.
Adaptee: the class to be adapted or the adaptor class.
Adapter: encapsulate an object to be adapted and convert the original interface to the target interface.
Note: In the GoF design mode, there are two types of adapter modes: Class adapter mode and Object Adapter mode. Because the class adapter mode matches one interface with another through multi-inheritance, and C #, java and other languages do not support multi-inheritance, the Object Adapter is described here.
Advantages:
The client can call the same interface through the adapter, which is transparent to the client. This is simpler, more direct, and more compact. Existing classes are reused to solve the inconsistency between existing classes and reuse environments. Decouple the target class and adaptation class, and introduce an adapter class to reuse the existing adapter class without modifying the original code. An Object Adapter can adapt multiple adaptation class to the same target. That is, the same adapter can adapt both the adapter class and its subclass to the target interface.

Disadvantage: for object adapters, the implementation of adapter replacement is complicated.

Applicable scenarios: The system needs to use existing classes, and the interfaces of these classes do not conform to the system interfaces. You want to create a reusable class for some classes that are not closely related to each other, including some classes that may be introduced in the future. When the two classes do the same or similar things but have different interfaces. The class developed by the old system has implemented some functions, but the client can only access it in the form of another interface, but we do not want to manually change the original class. When using third-party components, the definition of component interfaces is different from that defined by the user. You do not want to modify your own interfaces, but use the functions of third-party component interfaces.

 

 

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.