7th Lecture: Adapter Mode

Source: Internet
Author: User

2006.1.5 Li Jianzhong

The concept of adaptation (conversion) is everywhere.

Adaptation, that is, the original incompatible interfaces are converted into compatible interfaces without changing the original implementation.

 

Motivation)

In software systems, due to changes in the application environment, we often need to put "some existing objects" in the new environment for application, however, the interfaces required by the new environment are not met by these existing objects.

How can we cope with this "Migration change "? How can we make good use of the existing object and meet the interfaces required by the new application environment?

 

Intent)

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.

-- Design Pattern GoF

 

For example, the Adapter Application

This is actually a delegate call. A request is sent to MyStack, but MyStack is actually delegated to list for processing. MyStack is actually an Adapter (Adaptive object), list is Adaptee (adapted object), and IStack is the interface that the customer expects.

 

Structure)

The adapter has two structures:

-Object Adapter (more common)

The Object Adapter uses an object Combination Scheme. Its Adapter core Adaptee relationship is a combination, that is, in the above example, MyStack and list are a combination.

In OO, the combination mode is preferred, and inheritance is not applicable to the combination mode. Because the combination mode is more loosely coupled, and inheritance is tightly coupled, any changes to the parent class will cause subclass changes.

The above example is the Object Adapter.

 

-Class Adapter

The following example shows a class adapter.

The Adapter inherits the ArrayList and IStack interfaces. It can either use the methods in the ArrayList or use the methods in the IStack interface, which makes it a bit nondescribable. This class violates the principle that the class should have a single responsibility. It has both the role of ArrayList and the role of IStack. Therefore, this type of adaptation is not very common and is not recommended.

In addition, if a method may delegate to two or more objects, or two or more classes, the Object Adapter, you only need to add several internal attributes for adaptation.

For the class adapter, because the class in C # can only be a single inheritance, it cannot inherit from two or more classes, so the class adapter cannot be used here.

 

Key Points of Adapter Mode

The Adapter mode is mainly used in scenarios where you want to reuse some existing classes, but the interfaces are inconsistent with the requirements of the reuse environment. It is useful in legacy code reuse and class library migration.

GoF23 defines the implementation structure of two Adapter modes: Object Adapter and class Adapter. However, class adapters use the "Multi-inheritance" implementation method, resulting in poor high coupling, so it is generally not recommended. The Object Adapter adopts the "Object combination" method, which is more compliant with the loose coupling spirit.

The Adapter mode can be very flexible and does not have to stick to the two structures defined in GoF23. For example, the "existing object" in the Adapter mode can be used as a new interface method parameter to achieve adaptation.

The Adapter mode requires us to use the "interface-oriented programming" style as much as possible, so that we can easily adapt to it later.

 

 

Adapter application in. NET Framework

1. Reuse the COM Object in. NET:

-The COM object does not comply with the. NET object interface.

-Use tlbimp.exe to create a Runtime Callable Wrapper (RCW) interface that conforms to the. NET object

2.. NET data handler class (Adapter variant ):

-DataSet interfaces are not provided for various databases.

-You can use DbDataAdapter to adapt any database access/access to a DataSet object.

Microsoft separates the adapter from the adapted object. It uses EmployeeDAO as the adapter. This is a little different from the above pattern, but the principle is the same. If it is written as below, it is better to understand.

DataSet is an adapter.

3. Sort existing objects in the Collection class (Adapter variation ):

-The existing object does not implement the IComparer interface.

-Implement a sort adapter (inheriting from the IComparer Interface) and then Compare the two objects in the Compare method.

In this case, an error is reported, because the static method of Array. Sort is used, the input parameter type 'employe' must implement the IComparer interface. However, if the Employee class is already used in many places, we cannot change it. In this case, we can consider adaptation.

The Array. Sort method itself provides an overload that can be passed into a comparison method.

Therefore, we can re-write a class to implement the IComparer interface, which allows employees to sort by age

The implementation adaptation here does not place the adapter in the class.

There is no need to make a textbook structure to call an adapter. It is a good practice to pass the adaptation object directly as a parameter.

2010.9.30

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.