Adapter mode for structural design patterns

Source: Internet
Author: User

Structure


Intention Transforms the interface of one class into another interface that the customer wants. A d a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
Applicability
  • You want to use a class that already exists, and its interface doesn't fit your needs.
  • You want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those that might not necessarily be compatible with the interface).
  • (Applies to object a D a P T e r) you want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces. The object adapter can be adapted to its parent class interface.

1  usingSystem;2 3     classFrameworkxtarget4     {5         Virtual  Public voidSomerequest (intx)6         {7             //Normal implementation of Somerequest goes here8         }9     }Ten  One     classFrameworkyadaptee A     { -          Public voidQuiteadifferentrequest (stringstr) -         { theConsole.WriteLine ("quiteadifferentrequest = {0}", str); -         }         -     } -  +     classOuradapter:frameworkxtarget -     { +         PrivateFrameworkyadaptee adaptee =Newframeworkyadaptee (); A         Override  Public voidSomerequest (inta) at         { -             stringb; -b =a.tostring (); - Adaptee. Quiteadifferentrequest (b); -         }         -     } in  -     /// <summary> to     ///Summary description for Client. +     /// </summary> -      Public classClient the     { *         voidGenericclientcode (Frameworkxtarget x) $         {Panax Notoginseng             //We Assume this function contains client-side code -             //knows about Frameworkxtarget. theX.somerequest (4); +             //Other calls to Frameworkx go A             // ... the         } +          -          Public Static intMain (string[] args) $         { $Client C =NewClient (); -Frameworkxtarget x =NewOuradapter (); - C.genericclientcode (x);  the             return 0; -         }Wuyi}
Adapter Mode

Adapter mode for structural design patterns

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.