Adapter adapter (Structural Mode)

Source: Internet
Author: User

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

 

Structure -- Object Adapter

 

Structure -- class Adapter

 

DemoCode

Code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

UsingSystem. collections;

Namespace Testwpf
{
Interface Istack // CustomerProgramDesired Interface
{
Void Push ( Object ITEM );
Object Pop ();
Object Peek ();
}

///   <Summary>
// The adaptation object should not be: Class adapter: arraylist, istack. This is a class adapter.
/// Method of Object Adapter used in this class
///   </Summary>
Class Adapter: istack
{
Arraylist adpatee; // Is to use an adapted object
// Arraylist adpatee2 // Of course, there can also be multidimensional adaptation objects.

Public Adapter ()
{
Adpatee =   New Arraylist ();
}

Public VoidPush (ObjectItem)
{
Adpatee. Add (item );
}

Public   Object Pop ()
{
Object Object = Adpatee [adpatee. Count - 1 ];
Adpatee. removeat (adpatee. Count - 1 );
Return Object;
}

Public ObjectPeek ()
{
ReturnAdpatee [adpatee. Count-1];
}
}
}

 

 

Key Points of adapter Mode

    1. 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.
    2. 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.
    3. 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.
    4. 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

• Reuse COM objects 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.
•. NET data handler class (adapter variant ):
-Dataset interfaces are not provided for various databases.
-You can use dbdataadapter to access/access any database
Assigned to a DataSet object.
• Sort existing objects in the Collection class (adapter variation ):
-The icomparable interface is not implemented for existing objects.
-Implement a sort adapter (inheriting from the icomparer Interface) and then compare the two objects in its 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.