[Structure (interface mode)] adapter pattern adapter Mode

Source: Internet
Author: User

1. Effect of adapter mode
assume that a developed class and its specific method exist, but there is an inconsistency between the existing interface and the reuse method, at this time, we must use a mechanism to convert existing methods into other acceptable methods. This is the adapter pattern.
(adapter pattern is the simplest and most intuitive mode in all modes)
2 adapter mode classification
adapter mode implementation is divided into two ways.
one is to create an inherented from adaptee adapter class, which of course implements the target interface that meets the client requirements. Therefore, the relationship between adapter and adaptee is-. The class diagram is as follows:

another method is to define an adaptee object in the adapter class. Use the adaptee object to implement the methods in the target interface. Therefore, the relationship between adapter and adaptee is has-. The class diagram is as follows:
3. Code implementation:
is-a adapter pattern implementation :
Public interface target
{< br> Public void requestmethod ();
}< br> public class adaptee
{< br> Public void existedmethod ()
{< br> console. writeline ("This Is A original method");
}< BR >}

public class adatper: Target, adaptee
{< br> Public void requestmethod ()
{< br> console. write ("the method is modified");
base. existedmethod ();
}< BR >}

has-a adapter pattern implementation:
Public interface target
{< br> Public void requestmethod ();
}< br> public class adaptee
{< br> Public void existedmethod ()
{< br> console. writeline ("This Is A original method");
}< BR >}

Public class adatper: Target
{
Protected adaptee adapteeobj;
Public void requestmethod ()
{
Adapteeobj = new adaptee ();
Adapteeobj. existedmethod ();
}
}

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.