经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式-适配器模式
来源:cnblogs  作者:冰乐  时间:2019/9/20 9:49:36  对本文有异议

适配器模式:解决重构的问题,新东西和旧系统不吻合,通过组合/继承进行配

适配器:插座,电源适配器,做个转接的

程序员已经确定好规范IHelper,新增了一个RedisHelper----第三方,二者规范不一致,就是没实现接口

  1. /// <summary>
  2. /// 数据访问接口
  3. /// </summary>
  4. public interface IHelper
  5. {
  6. void Add<T>();
  7. void Delete<T>();
  8. void Update<T>();
  9. void Query<T>();
  10. }
  11. public class MysqlHelper : IHelper
  12. {
  13. public void Add<T>()
  14. {
  15. Console.WriteLine("This is {0} Add", this.GetType().Name);
  16. }
  17. public void Delete<T>()
  18. {
  19. Console.WriteLine("This is {0} Delete", this.GetType().Name);
  20. }
  21. public void Update<T>()
  22. {
  23. Console.WriteLine("This is {0} Update", this.GetType().Name);
  24. }
  25. public void Query<T>()
  26. {
  27. Console.WriteLine("This is {0} Query", this.GetType().Name);
  28. }
  29. }
  30. /// <summary>
  31. /// 第三方提供的 openstack servicestack
  32. /// 不能修改
  33. /// </summary>
  34. public class RedisHelper
  35. {
  36. public RedisHelper()
  37. {
  38. Console.WriteLine($"构造RedisHelper");
  39. }
  40. public void AddRedis<T>()
  41. {
  42. Console.WriteLine("This is {0} Add", this.GetType().Name);
  43. }
  44. public void DeleteRedis<T>()
  45. {
  46. Console.WriteLine("This is {0} Delete", this.GetType().Name);
  47. }
  48. public void UpdateRedis<T>()
  49. {
  50. Console.WriteLine("This is {0} Update", this.GetType().Name);
  51. }
  52. public void QueryRedis<T>()
  53. {
  54. Console.WriteLine("This is {0} Query", this.GetType().Name);
  55. }
  56. }
View Code

继承:既满足了现有的规范,又没有去修改RedisHelper,适配器,类适配器

  1. public class RedisHelperInherit : RedisHelper, IHelper
  2. {
  3. public RedisHelperInherit()
  4. {
  5. Console.WriteLine($"构造{this.GetType().Name}");
  6. }
  7. public void Add<T>()
  8. {
  9. base.AddRedis<T>();
  10. }
  11. public void Delete<T>()
  12. {
  13. base.DeleteRedis<T>();
  14. }
  15. public void Query<T>()
  16. {
  17. base.QueryRedis<T>();
  18. }
  19. public void Update<T>()
  20. {
  21. base.UpdateRedis<T>();
  22. }
  23. }
View Code

组合:对象适配器

  1. public class RedisHelperObject : IHelper
  2. {
  3. public RedisHelperObject()
  4. {
  5. Console.WriteLine($"构造{this.GetType().Name}");
  6. }
  7. private RedisHelper _RedisHelper = new RedisHelper();//属性注入 声明写死 一定有
  8. public RedisHelperObject(RedisHelper redisHelper)//构造函数 可以替换(需要抽象) 一定有(不考虑其他构造函数)
  9. {
  10. this._RedisHelper = redisHelper;
  11. }
  12. public void SetObject(RedisHelper redisHelper)//方法注入 可以替换(需要抽象) 不一定有
  13. {
  14. this._RedisHelper = redisHelper;
  15. }
  16. public void Add<T>()
  17. {
  18. this._RedisHelper.AddRedis<T>();
  19. }
  20. public void Delete<T>()
  21. {
  22. this._RedisHelper.DeleteRedis<T>();
  23. }
  24. public void Query<T>()
  25. {
  26. this._RedisHelper.QueryRedis<T>();
  27. }
  28. public void Update<T>()
  29. {
  30. this._RedisHelper.UpdateRedis<T>();
  31. }
  32. }
View Code

组合优于继承吗?

  二者都会先构造一个RedisHelper

     1、但是,继承有强侵入,父类的东西子类必须有

     2、灵活性,继承只为一个类服务,组合可以面向抽象诶多个类型服务

  1. //继承 既满足现有的规范调用,又没有修改RedisHelper
  2. //类适配器模式
  3. Console.WriteLine("*****************************");
  4. {
  5. IHelper helper = new RedisHelperInherit();
  6. helper.Add<Program>();
  7. helper.Delete<Program>();
  8. helper.Update<Program>();
  9. helper.Query<Program>();
  10. }
  11. //组合 既满足现有的规范调用,又没有修改RedisHelper
  12. //对象适配器
  13. Console.WriteLine("*****************************");
  14. {
  15. IHelper helper = new RedisHelperObject();
  16. helper.Add<Program>();
  17. helper.Delete<Program>();
  18. helper.Update<Program>();
  19. helper.Query<Program>();
  20. }
  21. //组合优于继承?
  22. //二者都会先构造一个RedisHelper,但是继承是强侵入,父类的东西子类必须有
  23. //灵活性,继承只为一个类服务;组合可以面向抽象为多个类型服务
  24. {
  25. RedisHelperInherit redisHelperInherit = new RedisHelperInherit();
  26. redisHelperInherit.DeleteRedis<Program>();
  27. }
  28. {
  29. RedisHelperObject redisHelperObject = new RedisHelperObject();
  30. redisHelperObject.Add<Program>();
  31. }
View Code

 

原文链接:http://www.cnblogs.com/taotaozhuanyong/p/11551210.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号