Adapter模式(适配器模式)

- 日理万妓 2024-03-30 09:59 176阅读 0赞

模式简介

适配器模式在生活中也比较常见,例如我们的手机充电头就是一个适配器,我们无法将家庭电源直接与手机进行连接,因为这可能带来较大的安全隐患,并且也不太便利,所以我们需要一个中间的适配器,这个适配器用来保证我们对手机进行充电时的安全性和便利性,家用电的输出功率是比较大的,通过电源适配器来控制手机的输入功率,而且还需要考虑到电池寿命的问题,长期向手机电池输入大功率可能会导致电池寿命变短,并且220V家用电如果不通过电源适配器来保证安全就很有可能导致触电事故,综上采用适配器模式不仅能够帮助我们将手机与家用电源进行间接连接,同时我们还能控制家用电源面向手机的输出功率。

模式UML图

48537ea2e610400bb2bca8fa94fc0f51.jpeg

代码示例(C#)

提示:可在本栏目的资源篇“设计模式代码示例合集”下载所有完整代码资源。

  1. //接口类型
  2. public enum InterfaceType
  3. {
  4. USB_TypeC, USB, Micro_USB, Lightning
  5. }
  6. //安卓手机
  7. public class AndroidPhone : Phone
  8. {
  9. public AndroidPhone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage) :
  10. base(p_name, p_phoneInterfaceType, p_inputCurrent, p_inputVoltage)
  11. { }
  12. }
  13. //苹果手机
  14. public class ApplePhone : Phone
  15. {
  16. public ApplePhone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage) :
  17. base(p_name, p_phoneInterfaceType, p_inputCurrent, p_inputVoltage)
  18. { }
  19. }
  20. //安卓充电线
  21. public class AndroidChargerCable : ChargerCable
  22. {
  23. public override void ConnectPhone(Phone p_phone)
  24. {
  25. if (phoneInterfaceType.Equals(p_phone.phoneInterfaceType))
  26. {
  27. if (transmissionCurrent <= p_phone.inputCurrent)
  28. Console.WriteLine("【ChargerCable:" + name + "】 successfully connected to 【Phone:" + p_phone.name + "】.");
  29. else Console.WriteLine("The transmissionCurrent of 【ChargerCable:" + name + "】 greater than inputCurrent of 【Phone:" + p_phone.name + "】.");
  30. }
  31. else Console.WriteLine("The phoneInterfaceType of 【ChargerCable:" + name + "】 doesn't match phoneInterfaceType of 【Phone:" + p_phone.name + "】.");
  32. // p_phone.Print();
  33. }
  34. public class Builder
  35. {
  36. private AndroidChargerCable acc = new AndroidChargerCable();
  37. public Builder BaseInfo(string p_name, string p_material, float p_length, float p_transmissionCurrent)
  38. {
  39. acc.name = p_name;
  40. acc.material = p_material;
  41. acc.length = p_length;
  42. acc.transmissionCurrent = p_transmissionCurrent;
  43. return this;
  44. }
  45. public Builder InterfaceType(InterfaceType p_chargerInterfaceType, InterfaceType p_phoneInterfaceType)
  46. {
  47. acc.chargerInterfaceType = p_chargerInterfaceType;
  48. acc.phoneInterfaceType = p_phoneInterfaceType;
  49. return this;
  50. }
  51. public AndroidChargerCable Build()
  52. {
  53. return acc;
  54. }
  55. }
  56. }
  57. //苹果充电线
  58. public class AppleChargerCable : ChargerCable
  59. {
  60. public override void ConnectPhone(Phone p_phone)
  61. {
  62. if (phoneInterfaceType.Equals(p_phone.phoneInterfaceType))
  63. {
  64. if (transmissionCurrent <= p_phone.inputCurrent)
  65. Console.WriteLine("【ChargerCable:" + name + "】 successfully connected to 【Phone:" + p_phone.name + "】.");
  66. else Console.WriteLine("The transmissionCurrent of 【ChargerCable:" + name + "】 greater than inputCurrent of 【Phone:" + p_phone.name + "】.");
  67. }
  68. else Console.WriteLine("The phoneInterfaceType of 【ChargerCable:" + name + "】 doesn't match phoneInterfaceType of 【Phone:" + p_phone.name + "】.");
  69. // p_phone.Print();
  70. }
  71. public class Builder
  72. {
  73. private AppleChargerCable acc = new AppleChargerCable();
  74. public Builder BaseInfo(string p_name, string p_material, float p_length, float p_transmissionCurrent)
  75. {
  76. acc.name = p_name;
  77. acc.material = p_material;
  78. acc.length = p_length;
  79. acc.transmissionCurrent = p_transmissionCurrent;
  80. return this;
  81. }
  82. public Builder InterfaceType(InterfaceType p_chargerInterfaceType, InterfaceType p_phoneInterfaceType)
  83. {
  84. acc.chargerInterfaceType = p_chargerInterfaceType;
  85. acc.phoneInterfaceType = p_phoneInterfaceType;
  86. return this;
  87. }
  88. public AppleChargerCable Build()
  89. {
  90. return acc;
  91. }
  92. }
  93. }
  94. //公牛插座
  95. public class BullSocket : Socket
  96. {
  97. public BullSocket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower) :
  98. base(p_name, p_socketNumber, p_length, p_socketCurrent, p_ratedPower)
  99. { }
  100. public override void ConnectPower(int p_voltage)
  101. {
  102. if (p_voltage < ratedPower / socketCurrent)
  103. Console.WriteLine("【Socket:" + name + "】 successfully connected to power.");
  104. else
  105. Console.WriteLine("The inputMaxVoltage of 【Socket:" + name + "】 less than actualInputVoltage.");
  106. }
  107. }
  108. //德力西插座
  109. public class DelixiSocket : Socket
  110. {
  111. public DelixiSocket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower) :
  112. base(p_name, p_socketNumber, p_length, p_socketCurrent, p_ratedPower)
  113. { }
  114. public override void ConnectPower(int p_voltage)
  115. {
  116. if (p_voltage < ratedPower / socketCurrent)
  117. Console.WriteLine("【Socket:" + name + "】 successfully connected to power.");
  118. else
  119. Console.WriteLine("The inputMaxVoltage of 【Socket:" + name + "】 less than actualInputVoltage.");
  120. }
  121. }
  122. //安卓电源适配器
  123. public class AndroidCharger : Charger
  124. {
  125. private AndroidCharger() { }
  126. public override ChargerCable ConnectCable(ChargerCable p_cable)
  127. {
  128. if (chargerInterfaceType.Equals(p_cable.chargerInterfaceType))
  129. {
  130. if (outputCurrent <= p_cable.transmissionCurrent)
  131. Console.WriteLine("【Charger:" + name + "】 successfully connected to 【ChargerCable:" + p_cable.name + "】.");
  132. else
  133. Console.WriteLine("The outputCurrent of 【Charger:" + name + "】 greater than the transmissionCurrent of 【ChargerCable:" + p_cable.name + "】.");
  134. }
  135. else Console.WriteLine("The chargerInterfaceType of 【Charger:" + name + "】 is different from chargerInterfaceType of 【ChargerCable:" + p_cable.name + "】.");
  136. // p_cable.Print();
  137. return p_cable;
  138. }
  139. public override Socket ConnectSocket(Socket p_socket)
  140. {
  141. if (inputCurrent <= p_socket.socketCurrent)
  142. {
  143. if (inputCurrent <= p_socket.socketCurrent)
  144. {
  145. if (inputVoltage >= (p_socket.ratedPower - 300f) / 10)
  146. Console.WriteLine("【Charger:" + name + "】 successfully connected to 【Socket:" + p_socket.name + "】.");
  147. else Console.WriteLine("The inputVoltage of 【Charger:" + name + "】 less than outputVoltage of 【Socket:" + p_socket.name + "】.");
  148. }
  149. else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");
  150. }
  151. else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");
  152. // p_socket.Print();
  153. return p_socket;
  154. }
  155. //构建器
  156. public class Builder
  157. {
  158. private AndroidCharger c = new AndroidCharger();
  159. public Builder BaseInfo(string p_name, string p_model, InterfaceType p_chargerInterfaceType)
  160. {
  161. c.name = p_name;
  162. c.model = p_model;
  163. c.chargerInterfaceType = p_chargerInterfaceType;
  164. return this;
  165. }
  166. public Builder Current(float p_inputCurrent, float p_outputCurrent)
  167. {
  168. c.inputCurrent = p_inputCurrent;
  169. c.outputCurrent = p_outputCurrent;
  170. return this;
  171. }
  172. public Builder Voltage(float p_inputVoltage, float p_outputVoltage)
  173. {
  174. c.inputVoltage = p_inputVoltage;
  175. c.outputVoltage = p_outputVoltage;
  176. return this;
  177. }
  178. public AndroidCharger Build()
  179. {
  180. return c;
  181. }
  182. }
  183. }
  184. //苹果电源适配器
  185. public class AppleCharger : Charger
  186. {
  187. private AppleCharger() { }
  188. public override ChargerCable ConnectCable(ChargerCable p_cable)
  189. {
  190. if (chargerInterfaceType.Equals(p_cable.chargerInterfaceType))
  191. {
  192. if (outputCurrent <= p_cable.transmissionCurrent)
  193. Console.WriteLine("【Charger:" + name + "】 successfully connected to 【ChargerCable:" + p_cable.name + "】.");
  194. else
  195. Console.WriteLine("The outputCurrent of 【Charger:" + name + "】 greater than the transmissionCurrent of 【ChargerCable:" + p_cable.name + "】.");
  196. }
  197. else Console.WriteLine("The chargerInterfaceType of 【Charger:" + name + "】 is different from chargerInterfaceType of 【ChargerCable:" + p_cable.name + "】.");
  198. // p_cable.Print();
  199. return p_cable;
  200. }
  201. public override Socket ConnectSocket(Socket p_socket)
  202. {
  203. if (inputCurrent <= p_socket.socketCurrent)
  204. {
  205. if (inputCurrent <= p_socket.socketCurrent)
  206. {
  207. if (inputVoltage >= (p_socket.ratedPower - 300f) / 10)
  208. Console.WriteLine("【Charger:" + name + "】 successfully connected to 【Socket:" + p_socket.name + "】.");
  209. else Console.WriteLine("The inputVoltage of 【Charger:" + name + "】 less than outputVoltage of 【Socket:" + p_socket.name + "】.");
  210. }
  211. else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");
  212. }
  213. else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");
  214. // p_socket.Print();
  215. return p_socket;
  216. }
  217. //构建器
  218. public class Builder
  219. {
  220. private AppleCharger c = new AppleCharger();
  221. public Builder BaseInfo(string p_name, string p_model, InterfaceType p_chargerInterfaceType)
  222. {
  223. c.name = p_name;
  224. c.model = p_model;
  225. c.chargerInterfaceType = p_chargerInterfaceType;
  226. return this;
  227. }
  228. public Builder Current(float p_inputCurrent, float p_outputCurrent)
  229. {
  230. c.inputCurrent = p_inputCurrent;
  231. c.outputCurrent = p_outputCurrent;
  232. return this;
  233. }
  234. public Builder Voltage(float p_inputVoltage, float p_outputVoltage)
  235. {
  236. c.inputVoltage = p_inputVoltage;
  237. c.outputVoltage = p_outputVoltage;
  238. return this;
  239. }
  240. public AppleCharger Build()
  241. {
  242. return c;
  243. }
  244. }
  245. }
  246. //电源适配器基类,所有电源适配器继承该类
  247. public abstract class Charger
  248. {
  249. public string name;
  250. public string model;
  251. public InterfaceType chargerInterfaceType;
  252. public float outputCurrent;
  253. public float inputCurrent;
  254. public float outputVoltage;
  255. public float inputVoltage;
  256. public void Print()
  257. {
  258. Console.WriteLine("【Charger:" + name + "】");
  259. Console.WriteLine("-----------------------------------");
  260. Console.WriteLine("[model]:" + model);
  261. Console.WriteLine("[chargerInterfaceType]:" + chargerInterfaceType);
  262. Console.WriteLine("[outputCurrent]:" + outputCurrent);
  263. Console.WriteLine("[inputCurrent]:" + inputCurrent);
  264. Console.WriteLine("[outputVoltage]:" + outputVoltage);
  265. Console.WriteLine("[inputVoltage]:" + inputVoltage);
  266. Console.WriteLine("-----------------------------------");
  267. }
  268. public abstract ChargerCable ConnectCable(ChargerCable p_cable);
  269. public abstract Socket ConnectSocket(Socket p_socket);
  270. }
  271. //充电线基类,所有充电线继承该类
  272. public abstract class ChargerCable
  273. {
  274. public string name;
  275. public InterfaceType chargerInterfaceType;
  276. public InterfaceType phoneInterfaceType;
  277. public string material;
  278. public float length;
  279. public float transmissionCurrent;
  280. public void Print()
  281. {
  282. Console.WriteLine("【ChargerCable:" + name + "】");
  283. Console.WriteLine("-----------------------------------");
  284. Console.WriteLine("[chargerInterfaceType]:" + chargerInterfaceType.ToString());
  285. Console.WriteLine("[phoneInterfaceType]:" + phoneInterfaceType.ToString());
  286. Console.WriteLine("[material]:" + material);
  287. Console.WriteLine("[length]:" + length);
  288. Console.WriteLine("[transmissionCurrent]:" + transmissionCurrent);
  289. Console.WriteLine("-----------------------------------");
  290. }
  291. public abstract void ConnectPhone(Phone p_phone);
  292. }
  293. //手机基类,所有手机继承该类
  294. public abstract class Phone
  295. {
  296. public string name;
  297. public InterfaceType phoneInterfaceType;
  298. public float inputCurrent;
  299. public float inputVoltage;
  300. public Phone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage)
  301. {
  302. name = p_name;
  303. phoneInterfaceType = p_phoneInterfaceType;
  304. inputCurrent = p_inputCurrent;
  305. inputVoltage = p_inputVoltage;
  306. }
  307. public void Print()
  308. {
  309. Console.WriteLine("【Phone:" + name + "】");
  310. Console.WriteLine("-----------------------------------");
  311. Console.WriteLine("[phoneInterfaceType]:" + phoneInterfaceType.ToString());
  312. Console.WriteLine("[inputCurrent]:" + inputCurrent);
  313. Console.WriteLine("[inputVoltage]:" + inputVoltage);
  314. Console.WriteLine("-----------------------------------");
  315. }
  316. }
  317. //插座类,所有插座继承该类
  318. public abstract class Socket
  319. {
  320. public string name;
  321. public int socketNumber;
  322. public float length;
  323. public float socketCurrent;
  324. public float ratedPower;
  325. public Socket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower)
  326. {
  327. name = p_name;
  328. socketNumber = p_socketNumber;
  329. length = p_length;
  330. socketCurrent = p_socketCurrent;
  331. ratedPower = p_ratedPower;
  332. }
  333. public void Print()
  334. {
  335. Console.WriteLine("【Socket:" + name + "】");
  336. Console.WriteLine("-----------------------------------");
  337. Console.WriteLine("[socketNumber]:" + socketNumber);
  338. Console.WriteLine("[length]:" + length);
  339. Console.WriteLine("[socketCurrent]:" + socketCurrent);
  340. Console.WriteLine("[ratedPower]:" + ratedPower);
  341. Console.WriteLine("-----------------------------------");
  342. }
  343. public abstract void ConnectPower(int p_voltage);
  344. }
  345. //测试代码
  346. public void Test()
  347. {
  348. AndroidPhone mi12 = new AndroidPhone("小米 12", InterfaceType.USB_TypeC, 13.4f, 5);
  349. ApplePhone apple13 = new ApplePhone("Apple 13", InterfaceType.Lightning, 1, 5);
  350. AndroidChargerCable romosChargerCable = new AndroidChargerCable.Builder()
  351. .BaseInfo("罗马仕CB051-72-113", "TPE", 1.5f, 3)
  352. .InterfaceType(InterfaceType.USB, InterfaceType.Micro_USB)
  353. .Build();
  354. AndroidChargerCable miChargerCable = new AndroidChargerCable.Builder()
  355. .BaseInfo("小米原装USB-C数据线", "TPE", 1, 6)
  356. .InterfaceType(InterfaceType.USB, InterfaceType.USB_TypeC)
  357. .Build();
  358. AppleChargerCable appleChargerCable = new AppleChargerCable.Builder()
  359. .BaseInfo("AppleA2244", "TPE", 1, 1)
  360. .InterfaceType(InterfaceType.USB_TypeC, InterfaceType.Lightning)
  361. .Build();
  362. BullSocket bullSocket = new BullSocket("公牛GN-B3440", 8, 1.6f, 10, 2500);
  363. DelixiSocket delixiSocket = new DelixiSocket("德力西CD98J-LK4X1.8", 4, 1.6f, 10, 2500);
  364. AndroidCharger miCharger = new AndroidCharger.Builder()
  365. .BaseInfo("小米MDY-13-ES", "MDY-13-ES", InterfaceType.USB)
  366. .Current(6.1f, 1.7f)
  367. .Voltage(220, 11)
  368. .Build();
  369. AppleCharger appleCharger = new AppleCharger.Builder()
  370. .BaseInfo("AppleA2244", "A2244", InterfaceType.USB_TypeC)
  371. .Current(2.22f, 0.5f)
  372. .Voltage(240, 9)
  373. .Build();
  374. //电源适配器连接充电线,充电线连接手机
  375. miCharger.ConnectCable(romosChargerCable).ConnectPhone(mi12);
  376. //电源适配器连接插座,插座连接家用电源
  377. miCharger.ConnectSocket(delixiSocket).ConnectPower(220);
  378. }

代码解说

我们这里的示例简单模拟了手机充电的过程,这个过程中需要手机Phone、充电头Charger、数据线ChargerCable、插座Socket,为了更真实地模拟,我们特意加入了输入电流、输出电流、输入电压、输出电压、接口类型等属性,并且对目前市面上主流地两种类型手机进行了模拟,一个是安卓手机AndroidPhone,另一个是苹果手机ApplePhone,数据线和充电头共同组成电源适配器,插座有两种,一个是BullSocket,另一个是DelixiSocket,我们需要模拟家用电源向手机充电的过程。

如果这篇文章对你有帮助,请给作者点个赞吧!

发表评论

表情:
评论列表 (有 0 条评论,176人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Adapter模式适配器模式

    模式简介 适配器模式在生活中也比较常见,例如我们的手机充电头就是一个适配器,我们无法将家庭电源直接与手机进行连接,因为这可能带来较大的安全隐患,并且也不太便利,所以我们需

    相关 适配器模式Adapter

    网上看到不少关于适配器模式的讲解,其中对于适配器模式解释的过于专业,一时不是特别理解适配器模式到底是用来干嘛的,具体的适用场景在哪,其最精髓的地方到底在哪。 本文结合自己的理

    相关 适配器模式adapter

    适配器模式的定义: 将一个类的接口转换成客户希望的另外一个接口,适配器模式使得原本由于接口不兼容而不能在一起的那些类可以一起工作。 主要分为三类:类的适配器模式、

    相关 适配器模式Adapter

    “适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。” 适配器模式主要通过继承、对象组合来实现适配器功能,

    相关 适配器模式adapter

    适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。首先

    相关 设计模式--适配器模式Adapter

    适配器模式 在现实生活中,经常出现两个对象因接口不兼容而不能在一起工作的实例,这时需要第三者进行适配。例如,讲中文的人同讲英文的人对话时需要一个翻译,用直流电的笔记本电脑