6. Adapter mode (Adapter pattern)

Source: Internet
Author: User

The concept of adaptation (conversion) is everywhere ...
Adaptation, that is, the original incompatible interface is converted to a compatible interface on the basis of not changing the original implementation.
For example: two converted to three box plug, the high voltage conversion to low voltage and so on.

Motive (motivate):
In the software system, because of the change of the application environment, it is often necessary to put "some existing objects" in the new environment, but the interfaces required by the new environment are not satisfied by these existing objects.
So how do you deal with this "migration change"? How can you take advantage of the good implementations of existing objects while satisfying the interfaces required by the new application environment? This is the adapter model to be said in this paper.
Intention (Intent):
Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
-------"design mode" GOF
Structure (struct):

Figure 1: Object Adapter


Figure 2: Class Adapter

Examples of life:


Applicability:

1. The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system.

2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future. These source classes do not necessarily have very complex interfaces.

3. (for object adapters) in the design, you need to change the interfaces of several existing subclasses, and if you use the class's adapter pattern, you should make an adapter for each subclass, which is not practical.
Schematic code example:

1 interface Istack
2 {
3 void Push (object item);
4 void POPs ();
5 Object Peek ();
6} 1//object Adapter (adapter relationship with adaptee combination)
2 public class Adapter:istack//Adaptation objects
3 {
4 ArrayList adaptee;//objects to be adapted
5 Public Adapter ()
6 {
7 Adaptee = new ArrayList ();
8}
9 public void Push (object item)
10 {
One by one adaptee. ADD (item);
12}
public void Pop ()
14 {
Adaptee. RemoveAt (adaptee. COUNT-1);
16}
+ public Object Peek ()
18 {
Return adaptee[adaptee. COUNT-1];
20}
21}

Class Adapter

1 public class Adapter:arraylist, Istack
2 {
3 public void Push (object item)
4 {
5 this. ADD (item);
6}
7 public void Pop ()
8 {
9 this. RemoveAt (this. COUNT-1);
10}
public Object Peek ()
12 {
Return this[this. COUNT-1];
14}
15}

Several points of the adapter model:
The adapter mode is mainly used in cases where "it is desirable to reuse some existing classes, but the interface is inconsistent with the reuse environment", which is useful in legacy code reuse, class library migration, and so on.
GOF23 defines the implementation structure of the two adapter modes: Object adapters and Class adapters. However, the class adapter adopts the implementation of "multi-Inheritance", which brings bad high coupling, so it is generally not recommended. Object adapters Use the "object combination" approach, more in line with the loose coupling spirit.
The adapter pattern can be implemented flexibly without having to stick to the two structures defined in the GOF23. For example, the "existing object" in adapter mode can be used as a new interface method parameter to achieve the purpose of adaptation.
The adapter model itself requires us to use the "interface-oriented programming" style as much as possible, so that it can be easily adapted at a later stage.
. NET Framework for adapter applications:
(1) Reusing COM objects in. NET:
Com objects do not conform to the interfaces of. NET objects
Use Tlbimp.exe to create a runtime callable Wrapper (RCW) that conforms to the interface of the. NET object.
(2). NET data Access Class (adapter variant):
A dataset interface is not available for various databases
Using DbDataAdapter, you can adapt any database access/access to a DataSet object.
(3) Sorting of existing objects in the collection Class (adapter variant);
Existing object does not implement IComparable interface
Implement a sort adapter (inheriting the IComparer interface), and then compare the two objects in its compare method.

6. Adapter mode (Adapter pattern)

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.