Design Mode-adapter Pattern)

Source: Internet
Author: User

1. Overview
During software development, we often encounter inter-system integration. The most common problem during system integration is the inconsistent interfaces between systems. Many system modules that can meet the requirements are unavailable due to inconsistent interfaces. For example, the commonly used media players are MS media player and RealPlayer. Their file structure and software interfaces are completely different. The former supports the WMF Format of audio and video, the latter supports audio and video in RM format. What should we do if we want our software to play the audio and video in the corresponding formats of these two players? Start from scratch, rewrite a playback software that supports these two formats? Well, if you don't feel tired, you will rewrite it.
Adapter mode ):Convert an interface into another interface. Adapter mode that the customer expects to make the classes that originally cannot work together due to interface incompatibility work together. The purpose of the adapter mode is to enable the classes with incompatible interfaces to work together. Normally, the functions of the classes with incompatible interfaces are consistent or similar in logic. During development, the data and behavior of the system are correct, but the interface does not match, we should consider using the adapter mode, the purpose is to make an existing object out of the control scope match a certain interface. The adapter mode is mainly used to take some existing classes, but the interfaces are inconsistent with the environment requirements.
There are two types of adapters: Class adapter and Object Adapter. The two have the same intent, but the implementation method and the applicable situation are different. Class adapters are implemented by inheritance, while object adapters are implemented by combination.
Class adapter: the class adapter matches an interface with another interface through multi-inheritance. Its structure is shown in:

Target defines the interfaces used by the client in specific fields. The client calls target to implement a specific operation. Adaptee is an existing class that needs to work with the target. This interface needs to be adapted. The adapter is compatible with the adaptee and target interfaces. In the class adapter, the method in adaptee is obtained through inheritance:
/// <Summary>
/// Adaptee
/// </Summary>
Public class adaptee
{
Public void execute ()
{
}
}

/// <Summary>
/// Target
/// </Summary>
Public class target
{
Public Void Do ()
{
}
}

/// <Summary>
/// Adapter,. Net does not support multi-Inheritance
/// </Summary>
Public class adapter: adaptee, Target
{
# Region target Member
Public Void Do ()
{
This. Execute ();
}
# Endregion
}

Copy code

In. net, the use of class adapters is limited. Because. Net does not support multi-inheritance, a class can only have one parent class. Therefore, when the target is a class rather than an interface, the class adapter cannot be implemented. In this case, you need to use the Object Adapter.
Object Adapter: The Object Adapter uses an object combination to reference one class and another class interface. Its structure is as follows:

In the Object Adapter, obtain the adaptee object through combination:
/// <Summary>
/// Adaptee
/// </Summary>
Public class adaptee
{
Public void execute ()
{
}
}

/// <Summary>
/// Target
/// </Summary>
Public class target
{
Public Void Do ()
{
}
}

/// <Summary>
/// Adapter,. Net does not support multi-Inheritance
/// </Summary>
Public class adapter: Target
{
Private adaptee;
Public adapter (adaptee AP)
{
Adaptee = ap;
}
# Region target Member
Public Void Do ()
{
Adaptee. Execute ();
}
# Endregion
}

Copy code

Usage: an existing class is required, but the interface does not comply with the design requirements. you want to create a reusable class, this class can work collaboratively with other irrelevant classes or unforeseen classes in the future. Use the adapter mode when both parties are not easy to modify.
2. Instance
The basketball translation adapter in the big talk design mode. Let's take a look at the structure:

The Code is as follows:
// Basketball Players
Public abstract class player
{
Protected string name;
Public player (string name)
{
This. Name = Name;
}

Public abstract void attack ();
Public abstract void defense ();
}

// Forward
Public class forwards: Player
{
Public forwards (string name)
: Base (name)
{
}

Public override void attack ()
{
Console. writeline ("forward {0} attack", name );
}

Public override void defense ()
{
Console. writeline ("Defender {0}", name );
}
}

// Center
Public class center: Player
{
Public Center (string name)
: Base (name)
{
}

Public override void attack ()
{
Console. writeline ("center {0} attack", name );
}

Public override void defense ()
{
Console. writeline ("center {0} Defender", name );
}
}

// Defender
Public class guards: Player
{
Public guards (string name)
: Base (name)
{
}

Public override void attack ()
{
Console. writeline ("Defender {0} attack", name );
}

Public override void defense ()
{
Console. writeline ("Defender {0} Defender", name );
}
}

// Foreign Center
Public class foreigncenter
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}

Public void attack ()
{
Console. writeline ("Foreign center {0} attack", name );
}

Public void defense ()
{
Console. writeline ("Foreign center {0} Defender", name );
}
}

// Translator
Public class Translator: Player
{
Private foreigncenter wjzf = new foreigncenter ();

Public Translator (string name)
: Base (name)
{
Wjzf. Name = Name;
}

Public override void attack ()
{
Wjzf. Attack ();
}

Public override void defense ()
{
Wjzf. Defense ();
}
}

Copy code

Call code: static void main (string [] ARGs)
{
Player B = new forwards ("Battier ");
B. Attack ();

Player M = new guards ("mcgredy ");
M. Attack ();

// Player ym = new center ("Yao Ming ");
Player ym = new translator ("Yao Ming ");
Ym. Attack ();
Ym. Defense ();

Console. Read ();
}

Copy code

Result:
Forward Battier attack
Guard mcgredy attack
Foreign center Yao Ming attack
Foreign center Yao Ming defense
With the Translator (adapter), Yao Ming does not understand English, and rocket coaches and players do not need to learn Chinese, so that the entire Rocket team can communicate and cooperate well. Haha.
3. Summary
1). Net adapter mode dataadapter. Dataadapter is used as an adapter between dataset and the data source to retrieve and save data. By ing fill (changing the data in dataset to match the data in the data source) and update (changing the data in the data source to match the data in the dataset ), to provide this adapter.

Implementation points
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; the adapter mode has two forms of implementation structure: Object Adapter and class Adapter. However, the class adapter adopts 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 implementation of 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.

Effect:
For Class adapters: Use a specific adapter class to match adaptee and taget. The result is that when we want to match a class and all its sub-classes, the class adapter will not be competent; so that the adapter can redefine some behavior of adaptee, because the adapter is a subclass of adaptee; only one object is introduced, and no additional pointer is required to indirectly obtain adaptee.
For object adapters: allows an adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses (if any) simultaneously. The adapter can also add functions to all adaptee at a time. This makes it difficult to redefine adaptee behavior. This requires generating a subclass of adaptee and making the adapter reference this subclass instead of referencing the adaptee itself.
Applicability
Use the adapter mode in the following situations: the system needs to use existing classes, and such interfaces do not meet the requirements of the system; to create a class that can be reused, it is used for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces; (for object adapters) in the design, you need to change multiple existing subclass interfaces. If you use the class adapter mode, an adapter is required for each subclass, which is not practical.
When adapting two irrelevant classes, consider the adaptation cost. A very large adapter may have an impact on the system.


References: http://terrylee.cnblogs.com/archive/2006/02/18/333000.html

Reprinted from: http://www.cnblogs.com/peida/archive/2008/07/15/1235214.html

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.