. NET adapter mode and. net Adapter

Source: Internet
Author: User

. NET adapter mode and. net Adapter

Adapter mode introduction:

Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

In computer programming, the adapter mode (sometimes called packaging style or packaging) adapts a class interface to what the user expects. An adaptation allows a class that cannot work together because the interface is not compatible, by encapsulating its own interface in an existing class.

Adapter mode structure:

Instance introduction:

The log recording program is used as the Demo to describe the corresponding log management module in any software. If we use a third-party log component to record the log in the development software, it uses Log. write ("Write Log"); in the development of our program, a large number of instantiated diary record objects, using Log. the Write () method is used to record logs, but the third-party log components are not free now, so we plan to use a new log management module, however, the API provided to us uses Log. writeLog ("New log Writing Method"); logs are recorded. At this point, the problem arises. How can we cope with this migration change?

Class adapter Mode

1. The original log interface adopts the Write ("Write log") method.

/// <Summary> /// the original log record interface /// </summary> public interface ILogTarget {/// <summary> /// the original log Writing Method // /</summary> void Write (string info );}

2. However, the current log writing interface uses WriteLog ("log writing"), which implements a new log Writing Method: Writing logs to files and databases.

/// <Summary> /// abstract write log class /// </summary> public abstract class LogAdaptee {/// <summary> /// write log /// </summary> public abstract void WriteLog (string info );}
/// <Summary> /// Write File logging /// </summary> public class FileLog: logAdaptee {/// <summary> /// write the log to the file /// </summary> public override void WriteLog (string info) {Console. writeLine ("record to text file:" + info );}}
/// <Summary> /// write logs to the database /// </summary> public class DatabaseLog: logAdaptee {// <summary> /// rewrite the log Writing Method /// </summary> public override void WriteLog (string info) {Console. writeLine ("record to database:" + info );}}

3. How do users replace the original log Writing Method in two new objects?

/// <Summary> /// write logs to the database using the new log writing method. /// </summary> public class DatabaseLogAdapter: DatabaseLog, ILogTarget {// <summary> /// call the new log Writing Method WriteLog // </summary> public void Write (string info) {WriteLog (info) ;}//< summary> /// use the new log Writing Method to write data to a text file /// </summary> public class FileLogAdapter: fileLog, ILogTarget {// <summary> /// call the new log Writing Method WriteLog // </summary> public void Write (string info) {this. writeLog (info );}}

4. Call the method based on the original log writing method, but the new log writing method is used:

/// <Summary> /// Adapter Pattern // </summary> class Program {static void Main (string [] args) {ILogTarget dbLog = new DatabaseLogAdapter (); dbLog. write ("program started successfully"); dbLog = new FileLogAdapter (); dbLog. write ("program started successfully ");}}

Object Adapter Mode

1. The method adopts the class adapter method to implement the migration change of the new log function. Next we use the Object Adapter to identify the differences between the two methods. The original log writing method remains unchanged: Write ("Write log ");

/// <Summary> /// the original log record interface /// </summary> public interface ILogTarget {/// <summary> /// the original log Writing Method // /</summary> void Write (string info );}

2. The current log writing interface uses WriteLog ("log writing"), which implements a new log Writing Method: Write the log to a 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 logging /// </summary> public class FileLog: logAdaptee {/// <summary> /// write the log to the file /// </summary> public override void WriteLog (string info) {Console. writeLine ("record to text file:" + info );}}
/// <Summary> /// write logs to the database /// </summary> public class DatabaseLog: logAdaptee {// <summary> /// rewrite the log Writing Method /// </summary> public override void WriteLog (string info) {Console. writeLine ("record to database:" + info );}}

3. we have added the FileLogAdapter class and the DatabaseLogAdapter class. It inherits the FileLog, DatabaseLog, and ILogTarget interfaces, and calls the new log Writing Method WriteLog In the rewrite Write method. This method is used for migration changes. The following uses Object adaptation:

/// <Summary> /// object adaptation, inherited from ILogTarget, which contains the 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. Call 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 started successfully"); ILogTarget fileLog = new LogAdapter (new FileLog (); fileLog. write ("program started successfully ");}}

Compare the migration changes between the two. In the class adaptation mode, the adapter DatabaseLogAdapter and FileLogAdapter have all the behaviors of the parent class it inherits, at the same time, it also has all the actions of the interface ILogTarget, which is in violation of the single responsibility principle of the class in the object-oriented design principle, and the Object Adapter is more in line with the object-oriented spirit, therefore, this method is not recommended in practical applications. Assume that the classes we want to adapt are written into the file and database at the same time when logging, we will use the Object Adapter to write as follows:

/// <Summary> /// object adaptation, inherited from ILogTarget, which contains the 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) {// simultaneously write logs to files and databases ILogTarget dbLog = new LogAdapter (new FileLog (), new DatabaseLog (); dbLog. write ("program started successfully ");}}

If we use a class adapter: do we use this method to achieve our goal?

  public class DatabaseLogAdapter : DatabaseLog, FileLog, ILogTarget  {    public void Write(string info)    {      this.WriteLog(info);    }  }

The result is definitely not. A class cannot have multiple base classes, so there is an error in writing details. For different situations, we should adopt appropriate methods for adaptive scheduling.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.