Home >Backend Development >C#.Net Tutorial >.NET adapter pattern explained

.NET adapter pattern explained

高洛峰
高洛峰Original
2016-12-20 13:12:521202browse

Introduction to the adapter pattern:

Convert the interface of a class into another interface that the customer wants. The Adapter pattern enables classes that would otherwise not work together due to incompatible interfaces to work together.

In computer programming, the adapter pattern (sometimes also called wrapper style or wrapper) adapts the interface of a class to what the user expects. An adaptation allows classes that normally wouldn't work together because of incompatible interfaces to work together by wrapping the class's own interface in an existing class.

Adapter pattern structure diagram:

.NET adapter pattern explained

Introducing an example to illustrate:

Using the logging program as the demo description, there will be a corresponding log management module in any set of software. If we develop a log in the software Recording uses a third-party log component for logging, which uses the Log.Write("write log"); method. During development of our program, a large number of diary recording objects are instantiated, using the Log.Write() method. Logging, but now third-party logging components are no longer free and need to be charged, so we plan to use a new log management module, but the API interface it provides us uses Log.WriteLog("New Write Log method"); perform logging. At this time, the question arises, how to deal with this migration change

Class Adapter Mode

1. The original log interface used the Write ("write log"); method

/// <summary>
/// 原来的日志记录接口
/// </summary>
public interface ILogTarget
{
  /// <summary>
  /// 原来的写日志方法
  /// </summary>
  void Write(string info);
}

2. However, the current log writing interface The interface uses WriteLog ("write log"); it implements a new way of writing logs: writing logs to files and databases

/// <summary>
/// 抽象写日志类
/// </summary>
public abstract class LogAdaptee
{
  /// <summary>
  /// 写日志
  /// </summary>
  public abstract void WriteLog(string info);
}
/// <summary>
/// 写文件日志记录
/// </summary>
public class FileLog:LogAdaptee
{
  /// <summary>
  /// 写日志到文件中
  /// </summary>
  public override void WriteLog(string info)
  {
    Console.WriteLine("记录到文本文件:"+info);
  }
}
/// <summary>
/// 往数据库中写日志
/// </summary>
public class DatabaseLog:LogAdaptee
{
  /// <summary>
  /// 重写写日志方法
  /// </summary>
  public override void WriteLog(string info)
  {
    Console.WriteLine("记录到数据库:"+info);
  }
}

3. How to use two new objects way to replace the original way of writing logs?

/// <summary>
/// 采用新的写日志的方式,写入到数据库中
/// </summary>
public class DatabaseLogAdapter:DatabaseLog,ILogTarget
{
  /// <summary>
  /// 在重写ILogTarget接口中的的Write方法里面调用新的写日志的方式WriteLog
  /// </summary>
  public void Write(string info)
  {
    WriteLog(info);
  }
}
/// <summary>
/// 采用新的写日志的方式,写入到文本文件
/// </summary>
public class FileLogAdapter : FileLog, ILogTarget
{
  /// <summary>
  /// 在重写ILogTarget接口中的的Write方法里面调用新的写日志的方式WriteLog
  /// </summary>
  public void Write(string info)
  {
    this.WriteLog(info);
  }
}

4. The original logging method is used, but the new logging method is indeed used:

/// <summary>
/// 类 .NET adapter pattern explained(Adapter Pattern)
/// </summary>
class Program
{
  static void Main(string[] args)
  {
    ILogTarget dbLog = new DatabaseLogAdapter();
    dbLog.Write("程序启动成功");
    dbLog = new FileLogAdapter();
    dbLog.Write("程序启动成功");
  }
}

Object Adapter Pattern

1. The method uses a class adapter The migration changes of the new log function are implemented in this way. Next, we use the object adapter to distinguish the special features of the two methods. The original method of writing logs remains the same: Write("Write Log");

/// <summary>
/// 原来的日志记录接口
/// </summary>
public interface ILogTarget
{
  /// <summary>
  /// 原来的写日志方法
  /// </summary>
  void Write(string info);
}

2. The current log writing interface uses WriteLog("Write Log"); it implements the function of writing logs New way: write logs to files and databases:

/// <summary>
/// 抽象写日志类
/// </summary>
public abstract class LogAdaptee
{
  /// <summary>
  /// 写日志
  /// </summary>
  public abstract void WriteLog(string info);
}
/// <summary>
/// 写文件日志记录
/// </summary>
public class FileLog:LogAdaptee
{
  /// <summary>
  /// 写日志到文件中
  /// </summary>
  public override void WriteLog(string info)
  {
    Console.WriteLine("记录到文本文件:"+info);
  }
}
/// <summary>
/// 往数据库中写日志
/// </summary>
public class DatabaseLog:LogAdaptee
{
  /// <summary>
  /// 重写写日志方法
  /// </summary>
  public override void WriteLog(string info)
  {
    Console.WriteLine("记录到数据库:"+info);
  }
}

3. Above we added the FileLogAdapter class, the DatabaseLogAdapter class, which inherits the FileLog, DatabaseLog, and ILogTarget interfaces, and overrides the Write method to call the new The method of writing logs is WriteLog. Migration changes have been carried out using this method. Object adaptation is used below:

/// <summary>
/// 对象适配,继承ILogTarget,里面有LogAdaptee抽象日志类对象。
/// </summary>
public class LogAdapter:ILogTarget
{
  /// <summary>
  /// 抽象写日志类
  /// </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>
/// 对象.NET adapter pattern explained(Adapter Pattern)
/// </summary>
class Program
{
  static void Main(string[] args)
  {
    ILogTarget dbLog = new LogAdapter(new DatabaseLog());
    dbLog.Write("程序启动成功");
    ILogTarget fileLog = new LogAdapter(new FileLog());
    fileLog.Write("程序启动成功");
  }
}

Compare the migration changes of the two. In the class adaptation method, we get the adapter classes DatabaseLogAdapter and FileLogAdapter It has all the behaviors of the parent class it inherits, and also has all the behaviors of the interface ILogTarget. This actually violates the single responsibility principle of classes in the object-oriented design principles, while the object adapter is more in line with the object-oriented spirit, so Class adaptation is not recommended in practical applications. Assume that the adapted class we want to write to the file and database at the same time when recording logs, then using the object adapter we will write like this:

/// <summary>
/// 对象适配,继承ILogTarget,里面有LogAdaptee抽象日志类对象。
/// </summary>
public class LogAdapter:ILogTarget
{
  /// <summary>
  /// 抽象写日志类
  /// </summary>
  private LogAdaptee _adapteed;
 
  /// <summary>
  /// 抽象写日志类
  /// </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>
/// 对象.NET adapter pattern explained(Adapter Pattern)
/// </summary>
class Program
{
  static void Main(string[] args)
  {
    //同时写日志到文件和数据库
    ILogTarget dbLog = new LogAdapter(new FileLog(), new DatabaseLog());
    dbLog.Write("程序启动成功");
  }
}

If we use a class adapter instead : Can we use this way of writing to achieve our goal?

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

The result is definitely not possible. A class cannot have multiple base classes. It is wrong to write the details like this. For different situations, we should adopt appropriate methods for adaptive scheduling.

The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn