Design Pattern Learning Notes-Adapter mode

Source: Internet
Author: User

1. Features: The interface incompatible classes can work together. Mend

2. Concept: Adapter mode, which transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

3. Class Diagram:

4. Program implementation:

<summary>///Define client-expected interface///</summary> public class Target {///<summary> Use virtual adornments so that subclasses can rewrite//</summary> public virtual void Request () {Console.wri        Teline ("This is a common request"); }}///<summary>///define the classes that need to be adapted////</summary> public class Adaptee {public void Spec        Ificrequest () {Console.WriteLine ("This is a special request."); }}///<summary>//define Adapter///</summary> public class Adapter:target {//Establish a private ad        Eptee Object Private Adaptee adaptee = new Adaptee (); <summary>/////By rewriting the request () method on the surface, it becomes the actual call to Specificrequest ()///</summary> public ov erride void Request () {adaptee.        Specificrequest ();   }}class program {static void Main (string[] args) {//For the client, call is the request of target ()         Target target = new Adapter (); Target.            Request ();        Console.read (); }    }

  

Design Pattern Learning Notes-Adapter 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.