Adapter Mode-Object Adapter mode

Source: Internet
Author: User

Explain:

Transforms the interface of one class into another interface that the customer wants. Those classes that would otherwise not work together because of incompatible interfaces can work together.

Solve the problem:

What is needed is, but cannot be used, and cannot be modified in a short time. That is, make a function suitable for different environments.

In the development, the system data, behavior are matched, but the interface does not match, you can consider the adapter.

It is desirable to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment and should be considered in adapter mode. (Use a class that already exists, but its interface (that is, the method), when it is not the same as needed)

Objective:

Matches an existing object outside the control range with an interface.

Without changing the functionality and environment, use adapters to solve the generation gap between them.

Classification:

Class Adapter mode: One interface is matched to another through multiple inheritance. However, many languages do not support, (a class can inherit only one parent class), so the use of the environment is limited, the effect is limited.

Object adapter mode;

Structure diagram:

  

Target, which can be a concrete, abstract class, interface

  

  

  

Advantages:

Two classes do things the same, similar. However, there are different interfaces to use, and the classes share the same interface. So that customers can unified call unified interface on the heart. Simpler, more direct, and more compact.

Usage Environment:

Consider using during the period of software development and maintenance.

Adapter mode is used when both sides are not easy to modify. Instead of using it when it's already different.

When designing your system, consider using third-party development components. The interface of the component is not the same as our own interface.

. The class library DataAdapter in NET is an already implemented, very important adapter. DataAdapter is used as an adapter between a dataset and a data source to retrieve and save data. DataAdapter provides this adapter by mapping fill (changing the data in the dataset to match the data in the data source), Update (changing the data in the data source to match the data in the dataset). For example, the source of the data source is different (SQL Server, Oracle ...). ), its data is organized differently, but it is possible to get a unified dataset data through DataAdapter. You can use data flexibly without having to focus on the data details of the database.

Example:

  

Use Note:

Adapter mode is a good mode, the single can not ignore the application and blindly use, leading to the cart before the horse.

If the interface can be prevented in advance of different problems, the mismatch will not occur, the effective interface is not unified when the problem occurs, the timely reconstruction, the problem does not expand; only when the original design, code can not change the situation, consider the adaptation.

Afterwards control is inferior to the control, the control of the matter is inferior to beforehand control.

Extended:

Early stage of software development. Within the company, the name of the class, method should be your side, preferably in the early design. If a similar feature class appears, different programmer-defined methods are not in the same name. You should consider refactoring the unified interface instead of using an adapter.

Adapter Mode-Object 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.