C # Design Pattern Series Tutorials-Adapter Mode _c# Tutorial

Source: Internet
Author: User

1. Overview

Converts the interface of a class into another interface that the customer expects. The adapter mode makes it possible for those classes to work together because the interface is incompatible and cannot work together.

2. The problem addressed

That is, the adapter mode makes it possible for those classes to work together because the interfaces are incompatible.

3. Role in the pattern

3.1 Destination Interface (target): the interface that the customer expects. A target can be a specific or abstract class, or it can be an interface.

3.2 Matching Classes (adaptee): Matching class or adapter class is required.

3.3 Adapter (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be fitted.

4. Pattern Interpretation

Note: In Gof design mode, there are two types of adapter patterns, class adapter mode and object adapter pattern. Because the class adapter pattern matches one interface to another through multiple inheritance, and languages like C # and Java do not support multiple inheritance, this is only an introduction to object adapters.

4.1 Class diagram of Adapter mode

4.2 Code implementation for Adapter mode

 <summary>
 ///defines the interface expected by the client
 ///</summary> public
 class Target
 {
  ///<summary >
  ///use virtual decoration so subclasses can override
  ///</summary> public
  virtual void Request ()
  {
   Console.WriteLine ("This is a common request");
  }

 <summary>
 ///defines the classes that need to be matched
 ///</summary> public
 class Adaptee
 {public
  void Specificrequest ()
  {
   Console.WriteLine ("This is a special request.");
  }

 <summary>
 ///Definition Adapter
 ///</summary> public
 class Adapter:target
 {
  // Create a private Adeptee object private
  adaptee adaptee = new Adaptee ();

  <summary>
  ///by rewriting, ostensibly invoking the request () method, becomes the actual call to Specificrequest ()
  ///</summary> public
  override void Request ()
  {
   adaptee. Specificrequest ();
  }
 

4.3 Client code

 Class program
 {
  static void Main (string[] args)
  {
   //to the client, the call to Target's request ()
   Target target = New Adapter ();
   Target. Request ();

   Console.read ();
  }
 

Run results

5. Model Summary

5.1 Advantages

5.1.1 through adapters, clients can invoke the same interface, which is transparent to the client. This is simpler, more direct, and more compact.

5.1.2 has reused existing classes to solve the problem of inconsistency between existing classes and reuse environment requirements.

5.1.3 the target class and the adapter class, by introducing an adaptor class to reuse the existing adaptor class without modifying the original code.

5.1.41 Object adapters can match a number of different adapter classes to the same target, that is, the same adapter can fit the adaptor class and its subclasses to the target interface.

5.2 Disadvantages

The implementation of the replacement adapter is more complex for an object adapter.

5.3 Applicable Scenarios

5.3.1 systems need to use existing classes, and the interfaces of these classes do not conform to the system's interfaces.

5.3.2 wants to build a reusable class that works with some classes that don't have much to do with each other, including classes that might be introduced in the future.

5.3.32 classes do things the same or similar, but with different interfaces.

5.3.4 the old system developed classes have implemented some functionality, but the client can only be accessed in the form of a different interface, but we do not want to manually change the original class.

5.3.5 uses Third-party components, the component interface definition differs from its own definition, and does not want to modify its own interfaces, but uses the functionality of Third-party component interfaces.

6. Adapter Application Examples

6.1 Developers who have used ado.net should have used DataAdapter, which is used as an adapter between the dataset and the data source. DataAdapter provides this adapter by mapping fill and update.

6.2 Mobile Power Adapter


The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.