Five minutes adapter mode for one design mode

Source: Internet
Author: User

Five minutes a design pattern, using the simplest method to describe the design pattern. To see more design patterns, click a design mode series for five minutes
http://blog.csdn.net/daguanjia11/article/category/3259443

Recognize adapter mode

The adapter pattern is defined by converting the interface of one class to another interface that the client wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

The main function of the adapter mode is to convert the interface, the purpose is to reuse the existing functions, instead of implementing a new interface, for the implementation of the function but the interface is incompatible with the occasion. The adapter combines the original object and provides an interface that is compatible with the client, and the actual work is done by the original interface.

Sample code

The following sample code mainly contains these sections:

    1. Target: The interface required by the client, related to a specific domain
    2. Adaptee: Adapted Object
    3. Adapter: Adapter

To the code:

/// <SUMMARY>   /// clients require interfaces that are related to a specific domain  /// </summary   public  interface  target{ Span class= "hljs-comment" >/// <summary>   /// method of Client request processing  /// </SUMMARY>   void  Request ();}  
//   <summary> ///   be adapted to the object//   </summary>  Public classadaptee{//   <summary>     ///The   original method has been implemented, but the interface has changed    //   </summary>      Public void oldrequest() {Console.WriteLine ("The original method was executed."); }}
//   <summary> ///   Adapter//   </summary>  Public classadapter:target{//   <summary>     ///   combination of objects to be adapted    //   </summary>     PrivateAdaptee adaptee; Public Adapter(Adaptee adaptee) { This. adaptee = Adaptee; } Public void Request()    { This. Adaptee.    Oldrequest (); }}

Let's see how the client uses

class Program{    staticvoid Main(string[] args)    {        //创建被适配对象        new Adaptee();        //创建客户端需要调用的接口对象        new Adapter(adaptee);        //请求处理        target.Request();    }}

Program execution Results:
The original method was executed

Five minutes adapter mode for one design mode

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.