Overview:
The adapter mode (atapter) converts an interface of a class to another interface that the user wants, so that those classes that cannot work together due to interface incompatibility can work together.
Application scenarios:
1. You want to use an existing class, and its interface does not meet your requirements.
2. You want to create a reusable class that can work collaboratively with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces.
3. (applies only to object adapters). You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class Interface
Class diagram:
CodeStructure:
1. Target Interface
/// <Summary>
/// Target interface that the customer expects
/// </Summary>
Class Target
{
Public Virtual Void Request ()
{
Console. writeline ( " Common Request " );
}
}
2. classes to be adapted
/// <Summary>
/// Classes to be adapted
/// </Summary>
Class Adaptee
{
Public Void Specificrequest ()
{
Console. writeline ( " Special requests " );
}
}
3. Construct the adapter
/// <Summary>
/// Encapsulate an adapter object internally to convert the source interface to the target interface.
/// </Summary>
Class Adapter: Target
{
/// <Summary>
/// Create a private adapter object
/// </Summary>
Private Adaptee = New Adaptee ();
Public Override Void Request ()
{
// In this way, you can actually call specificrequest () by calling the request () method ()
Adaptee. specificrequest ();
}
}
4. Call example
///
/// test the adapter mode
///
static void testadapter ()
{< br> Target target = New adapter ();
target. request ();
console. read ();
}
Summary:
The adapter mode is the best practice for restructuring without a door. It is the most important to solve practical problems ,. the dataadapter object in the. NET class library is used as an adapter between the dataset and the data source to retrieve and save data. The dataadapter maps the data in the dataset (this changes the data in the dataset so that the data in the data source can be matched) and update (this changes the data in the data source to match the data in dataset) to provide this adapter.