Beauty of design patterns: Adapter)

Source: Internet
Author: User

It is intended to convert the interface of a class into another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together. Convert the interface of a class into another interface clients CT. adapter lets classes work together that couldn't otherwise because of incompatible interfaces. the structure class adapter uses multiple inheritance to match one interface with another interface. The Object Adapter depends on the object combination. The participant Target defines the APIs used by the Client in specific fields. The Client works with objects that comply with the Target interface. Adaptee defines an existing interface, which needs to be adapted. The Adapter adapts the Adaptee interface to the Target interface. Applicability: You can use the Adapter mode in the following situations: You want to use an existing class, and its interface does not meet your needs. You want to create a reusable class that can work together with other unrelated or unpredictable classes. You want to use some existing classes, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface. The effect allows an Adapter to work with multiple Adaptee (Adaptee itself and its subclass) at the same time. The Adapter can also add functions to all Adaptee at a time. This makes it difficult to redefine Adaptee behavior. This requires generating a subclass of Adaptee and making the Adapter reference this subclass instead of referencing the Adaptee itself. The structure of the Bridge mode in the relevant mode is similar to that of the Object Adapter mode, but the Bridge mode has different starting points: the purpose of Bridge is to separate the Interface part and the implementation part, therefore, they can be easily and independently changed. The Adapter means to change the interface of an existing object. The Decorator mode enhances the functions of other objects without changing their interfaces. Therefore, Decorator is more transparent to applications than Adapter. The result is that the Decorator supports recursive combination, which cannot be implemented by the Adapter. The Proxy mode defines a Proxy for another object without changing its interface. Implementation Method (1): Simple and Direct Object Adapter. Copy code 1 namespace AdapterPattern. implementation1 2 {3 public class ParentAdaptee 4 {5 public virtual string SpecificRequest () 6 {7 return "ParentAdaptee"; 8} 9} 10 11 public class ChildAdaptee: parentAdaptee12 {13 public override string SpecificRequest () 14 {15 return "ChildAdaptee"; 16} 17} 18 19 public class Target20 {21 public virtual string Request () 22 {23 return "Target"; 24} 25} 26 27 publi C class Adapter: Target28 {29 private ParentAdaptee _ adaptee; 30 31 public Adapter (ParentAdaptee adaptee) 32 {33 _ adaptee = adaptee; 34} 35 36 public override string Request () 37 {38 return _ adaptee. specificRequest (); 39} 40} 41 42 public class Client43 {44 public void TestCase1 () 45 {46 ParentAdaptee adaptee = new ChildAdaptee (); 47 Target target = new Adapter (adaptee); 48 var result = target. request (); 49} 50} 51} copy the code implementation method (2): implement a bidirectional class adapter. Copy code 1 namespace AdapterPattern. implementation2 2 {3 public class ParentAdaptee 4 {5 public virtual string SpecificRequest () 6 {7 return "ParentAdaptee"; 8} 9} 10 11 public class ChildAdaptee: parentAdaptee12 {13 public override string SpecificRequest () 14 {15 return "ChildAdaptee"; 16} 17} 18 19 public interface ITarget20 {21 string Request (); 22} 23 24 public class Adapter: ChildAdaptee, ITarget25 {26 public Adapter () 27 {28} 29 30 public string Request () 31 {32 return base. specificRequest (); 33} 34} 35 36 public class Client37 {38 public void TestCase2 () 39 {40 ITarget target = new Adapter (); 41 var result = target. request (); 42} 43} 44}

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.