Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to .NET Adapter pattern

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "introduction to .NET Adapter pattern". In daily operation, I believe many people have doubts about the introduction of .NET Adapter pattern. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "introduction to .NET Adapter pattern"! Next, please follow the editor to study!

Introduction to Adapter pattern:

Convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.

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

Adapter pattern structure diagram:

An example is introduced to illustrate:

Taking the logging program as Demo, there will be a corresponding log management module in any set of software. If we use a third-party logging component for logging in the development software, it uses Log.Write ("logging"). Our program in the development, a large number of instantiated diary recording objects, using the Log.Write () way for logging, but now the third-party logging components are not free, need to charge, so we intend to use a new log management module, but it provides us with the API interface is the use of Log.WriteLog ("new way of logging"); logging. At this time, the question arises, how to deal with this change of migration.

Class adapter pattern

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

/ original logging interface / public interface ILogTarget {/ original logging method / void Write (string info);}

two。 However, the current log writing interface uses WriteLog ("write log"), which implements a new way of writing log: write the log to a file, in the database

/ Abstract write log class / public abstract class LogAdaptee {/ write log / public abstract void WriteLog (string info) } / write file logging / public class FileLog:LogAdaptee {/ write log to file / public override void WriteLog (string info) {Console.WriteLine ("record to text file:" + info) }} / write log to database / public class DatabaseLog:LogAdaptee {/ rewrite log method / public override void WriteLog (string info) {Console.WriteLine ("log to database:" + info);}}

3. How to use the way in the two new objects to replace the original way of logging?

/ write to the database with a new log writing method / public class DatabaseLogAdapter:DatabaseLog,ILogTarget {/ call the new log writing method in the Write method in the rewrite ILogTarget interface WriteLog / public void Write (string info) {WriteLog (info) } / write to the text file / public class FileLogAdapter: FileLog, ILogTarget {/ call the new log writing method in the Write method in the rewrite ILogTarget interface WriteLog / public void Write (string info) {this.WriteLog (info);}}

4. Call the new way to write the log based on the original method used, but do use:

/ Adapter Pattern / / class Program {static void Main (string [] adapter) {ILogTarget dbLog = new DatabaseLogAdapter (); dbLog.Write ("Program started successfully"); dbLog = new FileLogAdapter (); dbLog.Write ("Program started successfully");}}

Object adapter pattern

1. The way is a class adapter to achieve the migration changes of the new logging function, let's use the object adapter to distinguish between the two ways to find the special features. The original method of logging remains the same: Write ("write log")

/ original logging interface / public interface ILogTarget {/ original logging method / void Write (string info);}

two。 The current log writing interface uses WriteLog ("write log"). It implements a new way of writing log: write the log to a file and in the database:

/ Abstract write log class / public abstract class LogAdaptee {/ write log / public abstract void WriteLog (string info) } / write file logging / public class FileLog:LogAdaptee {/ write log to file / public override void WriteLog (string info) {Console.WriteLine ("record to text file:" + info) }} / write log to database / public class DatabaseLog:LogAdaptee {/ rewrite log method / public override void WriteLog (string info) {Console.WriteLine ("log to database:" + info);}}

3. Above we added FileLogAdapter class, DatabaseLogAdapter class, inherited FileLog,DatabaseLog, ILogTarget interface, overridden the Write method to call the new logging method WriteLog, using this way to make the migration change. The following uses object adaptation:

/ object adaptation, inheriting ILogTarget, which contains LogAdaptee abstract log class objects. / / public class LogAdapter:ILogTarget {/ private LogAdaptee _ adaptee; public LogAdapter (LogAdaptee adaptee) {this._adaptee = adaptee;} public void Write (string info) {_ adaptee.WriteLog (info);}}

4. Calls in the program:

/ object adapter pattern (Adapter Pattern) / 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");}}

Comparing the migration changes of the two, in the class adaptation mode, the adapter classes DatabaseLogAdapter and FileLogAdapter have all the behaviors of the parent class it inherits, as well as all the behaviors of the interface ILogTarget, which actually violates the single responsibility principle of the class in the object-oriented design principle, while the object adapter is more in line with the object-oriented spirit, so class adaptation is not recommended in practical application. Assuming that the class we want to adapt writes to both the file and the database when logging, we would write this with the object adapter:

/ object adaptation, inheriting ILogTarget, which contains LogAdaptee abstract log class objects. / public class LogAdapter:ILogTarget {/ abstract write log class / private LogAdaptee _ adapteed; / abstract write log class / 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:

/ object adapter pattern (Adapter Pattern) / class Program {static void Main (string [] args) {/ / write log to both file and database ILogTarget dbLog = new LogAdapter (new FileLog (), new DatabaseLog ()); dbLog.Write ("Program started successfully");}}

If we use class adapters instead: do 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 allowed, a class can not have more than one base class, so it is wrong to write the details. Therefore, according to different situations, we should adopt an appropriate way to carry out adaptive scheduling.

At this point, the study on "introduction to the .NET Adapter pattern" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report