C#适配器模式(Adapter Pattern)是一种结构型设计模式,用于将一个类的接口转换成客户端所期望的另一个接口。
适配器模式的核心思想是通过一个适配器类,将原本不兼容的接口转换成兼容的接口,使得客户端能够使用这些接口进行交互,而不需要修改原有的类或接口。适配器模式通常涉及以下几个角色:
目标接口(Target Interface):客户端所期望的接口,通过适配器进行调用。
适配器类(Adapter Class):实现目标接口,并持有源接口的实例,通过调用源接口的方法来适配目标接口。
源接口(Adaptee Interface):需要被适配的接口或类。
namespace AdapterPattern_适配器模式
{/// <summary>
/// 目标接口
/// </summary>
public interface SqlInterface
{
void Add<T>();
void Delete<T>();
void Update<T>();
void Query<T>();
}
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// SqlServer数据库
/// </summary>
public class SqlServer : SqlInterface
{
public void Add<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void Delete<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void Update<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void Query<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 源接口,新增MySql
/// </summary>
public class MySql
{
public void AddRedis<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void DeleteRedis<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void UpdateRedis<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void QueryRedis<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 类适配器模式
/// </summary>
public class MySqlClass : MySql, SqlInterface
{
public void Add<T>()
{
base.AddRedis<T>();
}
public void Delete<T>()
{
base.DeleteRedis<T>();
}
public void Update<T>()
{
base.UpdateRedis<T>();
}
public void Query<T>()
{
base.QueryRedis<T>();
}
}
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 对象适配器模式
/// </summary>
public class MySqlObject : SqlInterface
{
private MySql mySql = null;
public MySqlObject(MySql mySql)
{
this.mySql = mySql;
}
public MySqlObject()
{
this.mySql = new MySql();
}
public void Add<T>()
{
this.mySql.AddRedis<T>();
}
public void Delete<T>()
{
this.mySql.DeleteRedis<T>();
}
public void Update<T>()
{
this.mySql.UpdateRedis<T>();
}
public void Query<T>()
{
this.mySql.QueryRedis<T>();
}
}
}
namespace AdapterPattern_适配器模式
{
internal class Program
{
static void Main(string[] args)
{
{
Console.WriteLine("*****************************");
SqlInterface sqlserver = new SqlServer();
sqlserver.Add<Program>();
sqlserver.Delete<Program>();
sqlserver.Update<Program>();
sqlserver.Query<Program>();
}
{
Console.WriteLine("*****************************");
SqlInterface mysqlclass = new MySqlClass();
mysqlclass.Add<Program>();
mysqlclass.Delete<Program>();
mysqlclass.Update<Program>();
mysqlclass.Query<Program>();
}
{
Console.WriteLine("*****************************");
SqlInterface mysqlclass = new MySqlObject();
mysqlclass.Add<Program>();
mysqlclass.Delete<Program>();
mysqlclass.Update<Program>();
mysqlclass.Query<Program>();
}
{
Console.WriteLine("*****************************");
MySql mysql = new MySql();
SqlInterface mysqlclass = new MySqlObject(mysql);
mysqlclass.Add<Program>();
mysqlclass.Delete<Program>();
mysqlclass.Update<Program>();
mysqlclass.Query<Program>();
}
Console.Read();
//输出结果
//*****************************
//This is SqlServer Add
//This is SqlServer Delete
//This is SqlServer Update
//This is SqlServer Query
//* ****************************
//This is MySqlClass Add
//This is MySqlClass Delete
//This is MySqlClass Update
//This is MySqlClass Query
//* ****************************
//This is MySql Add
//This is MySql Delete
//This is MySql Update
//This is MySql Query
//* ****************************
//This is MySql Add
//This is MySql Delete
//This is MySql Update
//This is MySql Query
}
}
}
标签:AdapterPattern,mysqlclass,适配器,Update,模式,public,MySql,void From: https://www.cnblogs.com/ZHIZRL/p/17633533.html