设计模式:适配器模式
2022/4/14 23:13:57
本文主要是介绍设计模式:适配器模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
假如现在一个已经上线了的项目,现在为了提高性能需要增加Redis。
Redis的数据操作,第三方已经给提供了,但是他提供的数据操作接口和现在的接口不同,调用端又希望按照之前定义的接口来调用。
适配器模式就是解决这类问题的。
代码演示:
/// <summary> /// 数据操作接口 /// </summary> public interface ISqlHelper { void Add<T>(); void Delete<T>(); void Update<T>(); void Query<T>(); } /// <summary> /// SqlServer 数据库操作类 /// </summary> public class SqlServerHelper : ISqlHelper { public void Add<T>() { Console.WriteLine($"This is {this.GetType().Name} Add."); } public void Delete<T>() { Console.WriteLine($"This is {this.GetType().Name} Delete."); } public void Query<T>() { Console.WriteLine($"This is {this.GetType().Name} Query."); } public void Update<T>() { Console.WriteLine($"This is {this.GetType().Name} Update."); } } /// <summary> /// Redis 数据库操作类 /// 假设是第三方提供的,而且不能修改 /// </summary> public class RedisHelper { public void AddRedis<T>() { Console.WriteLine($"This is {this.GetType().Name} Add!"); } public void DeleteRedis<T>() { Console.WriteLine($"This is {this.GetType().Name} Delete!"); } public void UpdateRedis<T>() { Console.WriteLine($"This is {this.GetType().Name} Update!"); } public void QueryRedis<T>() { Console.WriteLine($"This is {this.GetType().Name} Query!"); } } /// <summary> /// 适配器: 通过继承的方式实现适配(满足ISqlHelper接口,另外继承RedisHelper) /// </summary> public class RedisHelperInherit : RedisHelper, ISqlHelper { public void Add<T>() { base.AddRedis<T>(); } public void Delete<T>() { base.DeleteRedis<T>(); } public void Query<T>() { base.QueryRedis<T>(); } public void Update<T>() { base.UpdateRedis<T>(); } } /// <summary> /// 调用 /// </summary> static void Main(string[] args) { ISqlHelper sqlHelper = new SqlServerHelper(); sqlHelper.Add<Student>(); Console.WriteLine("**************************************"); ISqlHelper redisHelper = new RedisHelperInherit(); redisHelper.Add<Student>(); Console.ReadKey(); }
上面的示例是采用继承的方式来实现的,下面的代码将采用组合的方式来实现,代码如下:
/// <summary> /// 适配器:通过组合的方式来实现 /// </summary> public class RedisHelperCombination : ISqlHelper { /// <summary> /// 字段属性方式组合(方式一) /// 默认构造,特别强硬的 /// </summary> private RedisHelper redisHelper = new RedisHelper(); private RedisHelper _RedisHelper = null; /// <summary> /// 构造函数方式组合(方式二) /// 实例化时传入,相对字段属性灵活一点 /// </summary> public RedisHelperCombination(RedisHelper redisHelper) { this._RedisHelper = redisHelper; } /// <summary> /// 方法方式组合 /// 对象 可有可无,因为方法可调用可不调用 /// </summary> public void AddRedisHelper(RedisHelper redisHelper) { this._RedisHelper = redisHelper; } void ISqlHelper.Add<T>() { this.redisHelper.AddRedis<T>(); } void ISqlHelper.Delete<T>() { this.redisHelper.DeleteRedis<T>(); } void ISqlHelper.Query<T>() { this.redisHelper.QueryRedis<T>(); } void ISqlHelper.Update<T>() { this.redisHelper.UpdateRedis<T>(); } }
这篇关于设计模式:适配器模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?