Design Mode learning notes-adapter Mode

Source: Internet
Author: User

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.

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.