IChargingLine.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AdapterPattern
  7. {
  8. /// <summary>
  9. /// 充电线
  10. /// 最终要适配成的目标角色
  11. /// </summary>
  12. public interface IChargingLine
  13. {
  14. /// <summary>
  15. /// 充电方法
  16. /// </summary>
  17. void Charging();
  18. }
  19. /// <summary>
  20. /// USB数据线(支持USB-Micro端口的设备)
  21. /// </summary>
  22. public class USBMicroLine : IChargingLine
  23. {
  24. public void Charging()
  25. {
  26. Console.WriteLine("为支持USB-Micro端口的设备充电!");
  27. }
  28. }
  29. /// <summary>
  30. /// 原装数据线
  31. /// 未定义充电标准的充电方法
  32. /// </summary>
  33. public class USBLine
  34. {
  35. public void Charge()
  36. {
  37. Console.WriteLine("为设备充电!");
  38. }
  39. }
  40. /// <summary>
  41. /// 苹果充电线适配器
  42. /// 类适配器模式
  43. /// </summary>
  44. public class USBlightingLineAdapter : USBLine, IChargingLine
  45. {
  46. public void Charging()
  47. {
  48. Console.WriteLine("对USB-Lighting端口的数据线进行适配!");
  49. base.Charge();
  50. }
  51. }
  52. /// <summary>
  53. /// 小米5充电线适配器
  54. /// 对象适配器模式
  55. /// </summary>
  56. public class USBTypecLineAdapter: IChargingLine
  57. {
  58. private readonly USBLine _usbLine;
  59. public USBTypecLineAdapter(USBLine usbLine)
  60. {
  61. this._usbLine = usbLine;
  62. }
  63. public void Charging()
  64. {
  65. Console.WriteLine("对USB-TypeC端口的数据线进行适配!");
  66. this._usbLine.Charge();
  67. }
  68. }
  69. }