Design Pattern C # language Description--Adapter mode

Source: Internet
Author: User
Tags definition class definition
Design design Pattern C # language Description-Adapter (Adapter) mode



* This article refers to "Java and the pattern" part of the content, suitable for the design pattern of beginners.



The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that cannot work together because of an interface mismatch can work together. Includes the adapter pattern of the class and the adapter pattern of the object in two different ways.



The adapter mode of the class converts the API of the applicable class to the API of the target class, and its static structure is shown as follows:







The role of the pattern is as follows:

Destination (target) Role: This is the desired interface. Note that the adapter pattern for the class is discussed here, so the target cannot be a class.

Source (adaptee) role: An interface that needs to be fit.

Adapter (Adapter) Role: Converts the source interface to the target interface.



Target;

public interface Target

{



void SampleOperation1 ();



void SampleOperation2 ()//Method not included by the source class



}//End INTERFACE DEFINITION Target



Adaptee;

public class Adaptee

{



public void SampleOperation1 ()

{



}



}//End CLASS DEFINITION adaptee



Adapter;

public class Adapter:adaptee,target

{



public void SampleOperation2 ()

{

}



}//End CLASS DEFINITION Adapter



The effect of the adapter pattern for the class:

Use a specific class to fit the source into the target, so that if the source and the subclass of the source use this kind of fit, it doesn't work.

Because the adapter class is a subclass of the source, some methods can be substituted for the (Override) source in the adapter class.



Similar to the adapter pattern of the class, the object's adapter pattern converts the API of the applicable class to the API for the target class, unlike the class's adapter pattern, where the object's adapter pattern is not connected to the Adaptee class using an inheritance relationship, but instead uses a delegate relationship, as shown in the class diagram:







Target;

public interface Target

{



void SampleOperation1 ();



void SampleOperation2 ();



}//End INTERFACE DEFINITION Target



Adaptee;

public class Adaptee

{



public void SampleOperation1 ()

{



}



}//End CLASS DEFINITION adaptee



Adapter:

public class Adapter:target

{



Private Adaptee adaptee;



public void Adapter (Adaptee adaptee)

{

This.adaptee=adaptee;

}



public void SampleOperation1 ()

{

Adaptee.sampleoperation1 ();

}



public void SampleOperation2 ()

{



}



}//End CLASS DEFINITION Adapter



Effect of Object Adapter pattern:

An adapter can fit a variety of different sources to the same target. That is, the same adapter can fit both the source class and its subclasses to the target interface.

The way to replace a source class is not easy compared to a class adapter. If you have to replace the source class method, you have to do a subclass of the source class, the method is replaced, and then the source class as a real source of the child-class fit.




Related Article

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.