Adapter mode analysis, structure diagram and basic code

Source: Internet
Author: User
Zookeeper

The following describes the adapter mode:

Definition: 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.
Applicable: in software development, that is, when the data and behavior of the system are correct, but the interface is inconsistent, we should consider using the adapter, the purpose is to match an existing object out of the control scope with an interface. The adapter mode is mainly used to reuse some existing classes, but the interfaces are inconsistent with the requirements of the Reuse Environment.
Note: Use adapter mode adaptation when both parties are not easy to modify.
Open-closed principle: software entities (classes, modules, functions, etc.) should be extensible, but cannot be modified. This principle has two features: one is 'open for extension' and the other is 'Closed for change '. When we first wrote the code, we assumed that the change would not happen. When a change occurs, we create an abstraction to isolate similar changes that occur later. That is, in the face of requirements, changes to the program are made by adding code, rather than changing the existing code. This is the spirit of the open-closed principle. Following this principle can bring about the huge benefits that object-oriented technology claims, that is, it can be maintained, scalable, reusable, and flexible.
Structure:


Basic code:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace adapter Mode
{
Class Program
{
Static void main (string [] ARGs)
{
Target target = new adapter ();
Target. Request ();

Console. Read ();

}
}

Class target
{
Public Virtual void request ()
{
Console. writeline ("common request ");
}
}

Class adaptee
{
Public void specificrequest ()
{
Console. writeline ("special request ");
}
}

Class adapter: Target
{
Private adaptee = new adaptee ();

Public override void request ()
{
Adaptee. specificrequest ();
}
}
}

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.