. NET Adapter mode explained

Source: Internet
Author: User
Introduction to Adapter Mode:

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.

In computer programming, the adapter pattern (sometimes referred to as a wrapper style or wrapper) fits the interface of a class into what the user expects. An adaptation allows classes that are usually not working together because of incompatible interfaces, by wrapping the class's own interface in an existing class.

Adapter Mode structure diagram:

Introduce an example to illustrate:

To the logging program for the demo, in any set of software will have a corresponding log management module, if we are in the development software journal Records using third-party log components for logging, it is log.write ("Write Log"), the way our programs in the development, A large number of instanced journaling objects, using the Log.write () method to log records, but now the third party's log components are not free now, need to charge, so we intend to use a new log management module, but it is provided to our API interface is the use of Log.writelog ( "New Way to write logs"); Log records. This is when the problem arises and how to deal with this shift.

Class Adapter Mode

1. The original log interface, using Write ("Write log");

<summary>///Original Logging interface///</summary>public interface ilogtarget{//<summary>//  Original Write Log method  ///</summary> void Write (string info);}

2. The current log interface, however, is Writelog ("Write Log"), which implements a new way of writing the log: writes the log to the file, the database

<summary>///Abstract Write Log class///</summary>public abstract class logadaptee{//<summary>///  write Log  //</summary> public abstract void Writelog (string info);}
<summary>///Write file log records//</summary>public class filelog:logadaptee{///<summary>//  Write log to file in/  //</summary> public  override void Writelog (string info)  {    Console.WriteLine (" Log to text file: "+info);}  }
<summary>///write a log to the database//</summary>public class databaselog:logadaptee{//  <summary>  ///Rewrite the Write log method  ///</summary> public override void Writelog (string info)  {    Console.WriteLine ("Logging to Database:" +info);}  }

3. How do I replace the original write log in the way that the user two new objects?

<summary>///is written to the database in a new write log,//</summary>public class Databaselogadapter:databaselog,ilogtarget {//<summary>/////Ilogtarget The new write log in the Write method in the  rewrite interface writelog///  </summary>  public void Write (string info)  {    writelog (info);}  }
<summary>///is written to a text file using a new write Log///</summary>public class Filelogadapter:filelog, ilogtarget{  // /<summary>////  The method of calling the new write log inside the Write method in the Ilogtarget interface Writelog///  </summary> public  Void Write (string info)  {this    . Writelog (info);}  }

4. Call the method by which the original write log was used, but it does use a new way of writing the log:

<summary>///class Adapter mode (Adapter pattern)///</summary>class program{  static void Main (string[] args) c1/>{    ilogtarget dbLog = new Databaselogadapter ();    Dblog.write ("program starts successfully");    DbLog = new Filelogadapter ();    Dblog.write ("program started successfully");}  }

Object Adapter Mode

1. The way to use the class adapter to implement the new log functionality of the migration changes, the following we use the object adapter, you can distinguish between two ways to find the special. The original method of writing the log is still the same: write ("Write log");

<summary>///Original Logging interface///</summary>public interface ilogtarget{//<summary>//  Original Write Log method  ///</summary> void Write (string info);}

2. The current log interface is Writelog ("Write Log"), which implements a new way to write the log: write the log to the file, in the database:

<summary>///Abstract Write Log class///</summary>public abstract class logadaptee{//<summary>///  write Log  //</summary> public abstract void Writelog (string info);}
<summary>///Write file log records//</summary>public class filelog:logadaptee{///<summary>//  Write log to file in/  //</summary> public  override void Writelog (string info)  {    Console.WriteLine (" Log to text file: "+info);}  }
<summary>///write a log to the database//</summary>public class databaselog:logadaptee{//  <summary>  ///Rewrite the Write log method  ///</summary> public override void Writelog (string info)  {    Console.WriteLine ("Logging to Database:" +info);}  }

3. Above we add Filelogadapter class, Databaselogadapter class, Inherit Filelog,databaselog, Ilogtarget interface, rewrite write method inside call new write log way Writelog , the migration changes are made in this way. Use object adaptation below:

<summary>///object adaptation, Inherit Ilogtarget, there is logadaptee abstract log class object. </summary>public class logadapter:ilogtarget{//<summary>///  Abstract Write Log class///  </summary >  private logadaptee _adaptee;   Public Logadapter (Logadaptee adaptee)  {    this._adaptee = adaptee;  }   public void Write (string info)  {    _adaptee. Writelog (info);}  }

4. Calls in the program:

<summary>///Object Adapter mode (Adapter pattern)///</summary>class program{  static void Main (string[] args)  {    Ilogtarget dbLog = new Logadapter (new DatabaseLog ());    Dblog.write ("program starts successfully");    Ilogtarget FileLog = new Logadapter (new FileLog ());    Filelog.write ("program started successfully");}  }

Comparing the migration changes between the two, in the class adaptation mode, we get the adapter class Databaselogadapter and Filelogadapter with all the behavior of the parent class it inherits, as well as all the behavior of the interface Ilogtarget. This is in fact contrary to the object-oriented design principle of the single principle of responsibility, and the object adapter is more in line with the spirit of object-oriented, so in practical applications is not recommended in the way of the class adaptation. Let's say we're going to use the object adapter to write the class that we want to adapt to write to both the file and the database when we log it:

<summary>///object adaptation, Inherit Ilogtarget, there is logadaptee abstract log class object. </summary>public class logadapter:ilogtarget{//<summary>///  Abstract Write Log class///  </summary >  private logadaptee _adapteed;   <summary>///  Abstract Write Log class///  </summary>  private logadaptee _adapteef;   Public Logadapter (Logadaptee adapteed, logadaptee Adapteef)  {    this._adapteed = adapteed;    This._adapteef = Adapteef;  }   public void Write (string info)  {    _adapteed. Writelog (info);    _adapteef. Writelog (info);}  }

Call:

<summary>///Object Adapter mode (Adapter pattern)///</summary>class program{  static void Main (string[] args) c1/>{    //write log to file and database    ilogtarget dbLog = new Logadapter (new FileLog (), New DatabaseLog ());    Dblog.write ("program started successfully");}  }

If you use class adapters instead: Are we using this notation to achieve our goals?

public class Databaselogadapter:databaselog, FileLog, ilogtarget{public  void Write (string info)  {this    . Writelog (info);}  }

The result must not be that a class cannot have more than one base class, so there is an error in the write detail. For all the different situations, we should use the appropriate way to adapt the scheduling.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

  • Related Article

    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.