Adapter Pattern)

Source: Internet
Author: User
Overview:
In the process of software development, we often need to reuse some of the "existing objects" we have previously developed, but these "existing objects" cannot meet our new application environment. How can we reuse these objects well to meet our application environment? This is what the adapter is trying to solve.

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
Examples in life:

In real life, we often see this kind of adapter and often use it, for example, mobile phones (I believe everyone has seen it). When our mobile phones are charging, it is impossible to directly charge on V power supply
Electricity, but the cell phone "charger" to convert to the voltage required by the mobile phone can be normally charged, otherwise it will not be able to complete charging, this "charger" played a role in adaptation.

1. Adapter pattern structure of the class

Class Structure:

CodeImplementation:

Namespace Adapter_pattern
{
Public   Interface Itarget
{
Void Getpower ();
}
/**/ ///   <Summary>
/// V power supply;
///   </Summary>
Public   Class Power
{
/**/ ///   <Summary>
/// Obtain the voltage of V from the power supply;
///   </Summary>
Public   Void Getpoer220v ()
{

}
}
/**/ ///   <Summary>
/// Adapter (Charger );
///   </Summary>
Public   Class Adapter: power, itarget
{
/**/ ///   <Summary>
/// Obtain the desired voltage;
///   </Summary>
Public   Void Getpower ()
{
This . Getpoer220v ();
Console. writeline ( " Get the charging voltage of your phone! " );
}
}

Client call:

Namespace Adapter_pattern
{
Class Program
{
/**/ ///   <Summary>
/// This is equivalent to a mobile phone;
///   </Summary>
///   <Param name = "ARGs"> </param>
Static   Void Main ( String [] ARGs)
{
Console. Write ( " Mobile phone: " );
Itarget target =   New Adapter ();
Target. getpower ();
Console. Read ();
}
}
}


Features:
It is implemented using multiple inheritance methods. We only need to call the getpower () method by using the charger (adapter) convert the V voltage to the required voltage for our use.

2. Adapter pattern structure of the object

Class Structure:

Code implementation:

Namespace Adapter_pattern_object _
{
Public   Interface Itarget
{
Void Getpower ();
}
/**/ ///   <Summary>
/// 220v Power Supply
///   </Summary>
Public   Class Power
{
/**/ ///   <Summary>
/// Obtain the 220v voltage;
///   </Summary>
Public   Void Getpoer220v ()
{
// In this case, the voltage is v;
}
}
/**/ ///   <Summary>
/// Adapter (Charger)
///   </Summary>
Public   Class Adapter: itarget
{
Power;
/**/ ///   <Summary>
/// Constructor of the adapter;
///   </Summary>
///   <Param name = "power"> Voltage to be adapted </Param>
Public Adapter (power)
{
This . Power = Power;
}
/**/ ///   <Summary>
/// Obtain the desired voltage;
///   </Summary>
Public   Void Getpower ()
{
Power. getpoer220v ();
Console. writeline ( " Get the charging voltage of your phone! " );
}
}
}


Client call:

Namespace Adapter_pattern_object _
{
Class Program
{
/**/ ///   <Summary>
/// This is equivalent to a mobile phone;
///   </Summary>
///   <Param name = "ARGs"> </param>
Static   Void Main ( String [] ARGs)
{
Console. Write ( " Mobile phone: " );

Power=NewPower ();
Itarget target= NewAdapter (power );
Target. getpower ();

Console. Read ();
}
}
}



Summary:

In this example, we need to charge our mobile phones. The power department cannot provide us with the voltage we need, and sometimes it does not necessarily meet our needs. This means we need to switch it ourselves, this requires an adapter ).
In a software system, if you want to reuse the previous "existing objects ",However, the interfaces provided by these objects do not necessarily adapt to our new environment. We need to convert them into the interfaces we need..

Application of adapter in. Net:

In ADO. net, the data we extract from the database must be put into a dataset, whether you are an Access database, an SQL database, or
All Oracle databases must be placed in dataset .. Net does not provide: sqldataset, oledbdataset, oracledataset
Etc. It only provides one kind of dataset, that is, using sqldataadapte to fill in data. Why can this dataset store different data? These adapters are suitable
Configuration.
Dataadapter structure:

Key points:

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. 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.

Applicability:
Use the adapter mode in the following situations:
1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.
2. Create a reusable class 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.
3. (For object adapters) in the design, you need to change the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each sub-class, which is not practical.

Summary:
In short, by using the adapter mode, you can fully enjoy the fun of migrating class libraries and reusing class libraries to achieve better reuse.

From: Http://www.cnblogs.com/houleixx/archive/2008/03/04/1090214.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.