Design Pattern-AdapterPattern)

Source: Internet
Author: User
Adaptation, that is, the original incompatible interfaces are converted into compatible interfaces without changing the original implementation.

The concept of adaptation (conversion) is everywhere ......
Adaptation, that is, the original incompatible interfaces are converted into compatible interfaces without changing the original implementation.
For example, two to three-case plug, high voltage to low voltage, etc.

Motivation(Motivate ):
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.
So how should we deal with this "migration change "? How can we make good use of the existing object and meet the interfaces required by the new application environment? This is the Adapter mode described in this article.
Intent (Intent ):
Converts an interface of a class to another interface that the customer wants.AdapterThe mode makes those classes that cannot work together due to interface incompatibility work together.
-------Design patternsGOF


Applicability:

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

2. You want to create a reusable class for some classes that are not closely associated with each other, including some classes that may 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 multiple existing subclass interfaces. if you use the class Adapter mode, you need to create an adapter for each subclass, which is not practical.


Schematic code example:

1 interface IStack
2 {
3 void Push (object item );
4 void Pop ();
5 object Peek ();
6}

1 // Object Adapter (relationship between Adapter and Adaptee)
2 public class Adapter: IStack // adaption object
3 {
4 ArrayList adaptee; // the object to be adapted
5 public Adapter ()
6 {
7 adaptee = new ArrayList ();
8}
9 public void Push (object item)
10 {
11 adaptee. Add (item );
12}
13 public void Pop ()
14 {
15 adaptee. RemoveAt (adaptee. Count-1 );
16}
17 public object Peek ()
18 {
19 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}
11 public object Peek ()
12 {
13 return this [this. Count-1];
14}
15}

 

Several 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. Therefore, 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.
The Adapter application in the. NET Framework:
(1)Reuse the com object in. Net:
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). NETData Category (Adapter variant ):
Various databases do not provide DataSet interfaces
You can use DBDataAdapter to adapt any database access/access to a DataSet object.
(3)Sort existing objects in the collection class (Adapter variation);
The existing object does not implement the IComparable interface
Implement a sort adapter (inheriting the IComparer interface), and then Compare the two objects in the Compare method.

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.