OC Programming Path-Adapter for interface adaptation

Source: Internet
Author: User

The problem of incompatibility between existing classes and new interfaces is quite common, and one solution has been found for it. This solution is the adapter.

1 What is adapter what

The main function of the adapter is to pass the behavior of the adapter to the client at the other end of the pipeline.

Transforms the interface of one class into another interface that the customer wants. The adapter mode works with those classes that would otherwise not work together because of incompatible interfaces.

There are two ways of implementing an adapter:

1) class Adapter: Two interfaces can be adapted by inheritance. Inherit adaptee, follow <Target> agreement.

2) object adapter: Adapted by a combination. Follow the <Target> agreement, combining a reference to a team adaptee.

Difference:

Class Adapter
Only for a single specific Adaptee class, the Adaptee is adapted to target;

Easy to overload the behavior of Adaptee, because it is through the direct sub-class of adaptation;

There is only one adapter object that accesses adaptee indirectly without additional pointers.

Object Adapter

Can be adapted to a plurality of adaptee and subclasses;

It is difficult to overload the behavior of adaptee, it needs to use the object of subclass rather than adaptee itself;

Additional pointers are required to indirectly access the Adaptee and adapter behavior.

2 When to use adapter mode

1) The interfaces of the existing classes do not match the requirements

2) Want a reusable class that can write to other classes that may have incompatible interfaces

3) It is necessary to fit a few different subclasses of a ray, but to let each subclass subclass an adapter is unrealistic, you can use an object adapter (also called a delegate) to fit the interface of its parent class.

OC Programming Path-Adapter for interface adaptation

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.