Adapter模式(适配器模式)

news/2024/11/6 13:40:58/

模式简介

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

模式UML图

代码示例(C#)

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

    //接口类型public enum InterfaceType{USB_TypeC, USB, Micro_USB, Lightning}//安卓手机public class AndroidPhone : Phone{public AndroidPhone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage) :base(p_name, p_phoneInterfaceType, p_inputCurrent, p_inputVoltage){ }}//苹果手机public class ApplePhone : Phone{public ApplePhone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage) :base(p_name, p_phoneInterfaceType, p_inputCurrent, p_inputVoltage){ }}//安卓充电线public class AndroidChargerCable : ChargerCable{public override void ConnectPhone(Phone p_phone){if (phoneInterfaceType.Equals(p_phone.phoneInterfaceType)){if (transmissionCurrent <= p_phone.inputCurrent)Console.WriteLine("【ChargerCable:" + name + "】 successfully connected to 【Phone:" + p_phone.name + "】.");else Console.WriteLine("The transmissionCurrent of 【ChargerCable:" + name + "】 greater than inputCurrent of 【Phone:" + p_phone.name + "】.");}else Console.WriteLine("The phoneInterfaceType of 【ChargerCable:" + name + "】 doesn't match phoneInterfaceType of 【Phone:" + p_phone.name + "】.");// p_phone.Print();}public class Builder{private AndroidChargerCable acc = new AndroidChargerCable();public Builder BaseInfo(string p_name, string p_material, float p_length, float p_transmissionCurrent){acc.name = p_name;acc.material = p_material;acc.length = p_length;acc.transmissionCurrent = p_transmissionCurrent;return this;}public Builder InterfaceType(InterfaceType p_chargerInterfaceType, InterfaceType p_phoneInterfaceType){acc.chargerInterfaceType = p_chargerInterfaceType;acc.phoneInterfaceType = p_phoneInterfaceType;return this;}public AndroidChargerCable Build(){return acc;}}}//苹果充电线public class AppleChargerCable : ChargerCable{public override void ConnectPhone(Phone p_phone){if (phoneInterfaceType.Equals(p_phone.phoneInterfaceType)){if (transmissionCurrent <= p_phone.inputCurrent)Console.WriteLine("【ChargerCable:" + name + "】 successfully connected to 【Phone:" + p_phone.name + "】.");else Console.WriteLine("The transmissionCurrent of 【ChargerCable:" + name + "】 greater than inputCurrent of 【Phone:" + p_phone.name + "】.");}else Console.WriteLine("The phoneInterfaceType of 【ChargerCable:" + name + "】 doesn't match phoneInterfaceType of 【Phone:" + p_phone.name + "】.");// p_phone.Print();}public class Builder{private AppleChargerCable acc = new AppleChargerCable();public Builder BaseInfo(string p_name, string p_material, float p_length, float p_transmissionCurrent){acc.name = p_name;acc.material = p_material;acc.length = p_length;acc.transmissionCurrent = p_transmissionCurrent;return this;}public Builder InterfaceType(InterfaceType p_chargerInterfaceType, InterfaceType p_phoneInterfaceType){acc.chargerInterfaceType = p_chargerInterfaceType;acc.phoneInterfaceType = p_phoneInterfaceType;return this;}public AppleChargerCable Build(){return acc;}}}//公牛插座public class BullSocket : Socket{public BullSocket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower) :base(p_name, p_socketNumber, p_length, p_socketCurrent, p_ratedPower){ }public override void ConnectPower(int p_voltage){if (p_voltage < ratedPower / socketCurrent)Console.WriteLine("【Socket:" + name + "】 successfully connected to power.");elseConsole.WriteLine("The inputMaxVoltage of 【Socket:" + name + "】 less than actualInputVoltage.");}}//德力西插座public class DelixiSocket : Socket{public DelixiSocket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower) :base(p_name, p_socketNumber, p_length, p_socketCurrent, p_ratedPower){ }public override void ConnectPower(int p_voltage){if (p_voltage < ratedPower / socketCurrent)Console.WriteLine("【Socket:" + name + "】 successfully connected to power.");elseConsole.WriteLine("The inputMaxVoltage of 【Socket:" + name + "】 less than actualInputVoltage.");}}//安卓电源适配器public class AndroidCharger : Charger{private AndroidCharger() { }public override ChargerCable ConnectCable(ChargerCable p_cable){if (chargerInterfaceType.Equals(p_cable.chargerInterfaceType)){if (outputCurrent <= p_cable.transmissionCurrent)Console.WriteLine("【Charger:" + name + "】 successfully connected to 【ChargerCable:" + p_cable.name + "】.");elseConsole.WriteLine("The outputCurrent of 【Charger:" + name + "】 greater than the transmissionCurrent of 【ChargerCable:" + p_cable.name + "】.");}else Console.WriteLine("The chargerInterfaceType of 【Charger:" + name + "】 is different from chargerInterfaceType of 【ChargerCable:" + p_cable.name + "】.");// p_cable.Print();return p_cable;}public override Socket ConnectSocket(Socket p_socket){if (inputCurrent <= p_socket.socketCurrent){if (inputCurrent <= p_socket.socketCurrent){if (inputVoltage >= (p_socket.ratedPower - 300f) / 10)Console.WriteLine("【Charger:" + name + "】 successfully connected to 【Socket:" + p_socket.name + "】.");else Console.WriteLine("The inputVoltage of 【Charger:" + name + "】 less than outputVoltage of 【Socket:" + p_socket.name + "】.");}else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");}else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");// p_socket.Print();return p_socket;}//构建器public class Builder{private AndroidCharger c = new AndroidCharger();public Builder BaseInfo(string p_name, string p_model, InterfaceType p_chargerInterfaceType){c.name = p_name;c.model = p_model;c.chargerInterfaceType = p_chargerInterfaceType;return this;}public Builder Current(float p_inputCurrent, float p_outputCurrent){c.inputCurrent = p_inputCurrent;c.outputCurrent = p_outputCurrent;return this;}public Builder Voltage(float p_inputVoltage, float p_outputVoltage){c.inputVoltage = p_inputVoltage;c.outputVoltage = p_outputVoltage;return this;}public AndroidCharger Build(){return c;}}}//苹果电源适配器public class AppleCharger : Charger{private AppleCharger() { }public override ChargerCable ConnectCable(ChargerCable p_cable){if (chargerInterfaceType.Equals(p_cable.chargerInterfaceType)){if (outputCurrent <= p_cable.transmissionCurrent)Console.WriteLine("【Charger:" + name + "】 successfully connected to 【ChargerCable:" + p_cable.name + "】.");elseConsole.WriteLine("The outputCurrent of 【Charger:" + name + "】 greater than the transmissionCurrent of 【ChargerCable:" + p_cable.name + "】.");}else Console.WriteLine("The chargerInterfaceType of 【Charger:" + name + "】 is different from chargerInterfaceType of 【ChargerCable:" + p_cable.name + "】.");// p_cable.Print();return p_cable;}public override Socket ConnectSocket(Socket p_socket){if (inputCurrent <= p_socket.socketCurrent){if (inputCurrent <= p_socket.socketCurrent){if (inputVoltage >= (p_socket.ratedPower - 300f) / 10)Console.WriteLine("【Charger:" + name + "】 successfully connected to 【Socket:" + p_socket.name + "】.");else Console.WriteLine("The inputVoltage of 【Charger:" + name + "】 less than outputVoltage of 【Socket:" + p_socket.name + "】.");}else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");}else Console.WriteLine("The inputCurrent of 【Charger:" + name + "】 greater than socketCurrent of 【Socket:" + p_socket.name + "】.");// p_socket.Print();return p_socket;}//构建器public class Builder{private AppleCharger c = new AppleCharger();public Builder BaseInfo(string p_name, string p_model, InterfaceType p_chargerInterfaceType){c.name = p_name;c.model = p_model;c.chargerInterfaceType = p_chargerInterfaceType;return this;}public Builder Current(float p_inputCurrent, float p_outputCurrent){c.inputCurrent = p_inputCurrent;c.outputCurrent = p_outputCurrent;return this;}public Builder Voltage(float p_inputVoltage, float p_outputVoltage){c.inputVoltage = p_inputVoltage;c.outputVoltage = p_outputVoltage;return this;}public AppleCharger Build(){return c;}}}//电源适配器基类,所有电源适配器继承该类public abstract class Charger{public string name;public string model;public InterfaceType chargerInterfaceType;public float outputCurrent;public float inputCurrent;public float outputVoltage;public float inputVoltage;public void Print(){Console.WriteLine("【Charger:" + name + "】");Console.WriteLine("-----------------------------------");Console.WriteLine("[model]:" + model);Console.WriteLine("[chargerInterfaceType]:" + chargerInterfaceType);Console.WriteLine("[outputCurrent]:" + outputCurrent);Console.WriteLine("[inputCurrent]:" + inputCurrent);Console.WriteLine("[outputVoltage]:" + outputVoltage);Console.WriteLine("[inputVoltage]:" + inputVoltage);Console.WriteLine("-----------------------------------");}public abstract ChargerCable ConnectCable(ChargerCable p_cable);public abstract Socket ConnectSocket(Socket p_socket);}//充电线基类,所有充电线继承该类public abstract class ChargerCable{public string name;public InterfaceType chargerInterfaceType;public InterfaceType phoneInterfaceType;public string material;public float length;public float transmissionCurrent;public void Print(){Console.WriteLine("【ChargerCable:" + name + "】");Console.WriteLine("-----------------------------------");Console.WriteLine("[chargerInterfaceType]:" + chargerInterfaceType.ToString());Console.WriteLine("[phoneInterfaceType]:" + phoneInterfaceType.ToString());Console.WriteLine("[material]:" + material);Console.WriteLine("[length]:" + length);Console.WriteLine("[transmissionCurrent]:" + transmissionCurrent);Console.WriteLine("-----------------------------------");}public abstract void ConnectPhone(Phone p_phone);}//手机基类,所有手机继承该类public abstract class Phone{public string name;public InterfaceType phoneInterfaceType;public float inputCurrent;public float inputVoltage;public Phone(string p_name, InterfaceType p_phoneInterfaceType, float p_inputCurrent, float p_inputVoltage){name = p_name;phoneInterfaceType = p_phoneInterfaceType;inputCurrent = p_inputCurrent;inputVoltage = p_inputVoltage;}public void Print(){Console.WriteLine("【Phone:" + name + "】");Console.WriteLine("-----------------------------------");Console.WriteLine("[phoneInterfaceType]:" + phoneInterfaceType.ToString());Console.WriteLine("[inputCurrent]:" + inputCurrent);Console.WriteLine("[inputVoltage]:" + inputVoltage);Console.WriteLine("-----------------------------------");}}//插座类,所有插座继承该类public abstract class Socket{public string name;public int socketNumber;public float length;public float socketCurrent;public float ratedPower;public Socket(string p_name, int p_socketNumber, float p_length, float p_socketCurrent, float p_ratedPower){name = p_name;socketNumber = p_socketNumber;length = p_length;socketCurrent = p_socketCurrent;ratedPower = p_ratedPower;}public void Print(){Console.WriteLine("【Socket:" + name + "】");Console.WriteLine("-----------------------------------");Console.WriteLine("[socketNumber]:" + socketNumber);Console.WriteLine("[length]:" + length);Console.WriteLine("[socketCurrent]:" + socketCurrent);Console.WriteLine("[ratedPower]:" + ratedPower);Console.WriteLine("-----------------------------------");}public abstract void ConnectPower(int p_voltage);}//测试代码public void Test(){AndroidPhone mi12 = new AndroidPhone("小米 12", InterfaceType.USB_TypeC, 13.4f, 5);ApplePhone apple13 = new ApplePhone("Apple 13", InterfaceType.Lightning, 1, 5);AndroidChargerCable romosChargerCable = new AndroidChargerCable.Builder().BaseInfo("罗马仕CB051-72-113", "TPE", 1.5f, 3).InterfaceType(InterfaceType.USB, InterfaceType.Micro_USB).Build();AndroidChargerCable miChargerCable = new AndroidChargerCable.Builder().BaseInfo("小米原装USB-C数据线", "TPE", 1, 6).InterfaceType(InterfaceType.USB, InterfaceType.USB_TypeC).Build();AppleChargerCable appleChargerCable = new AppleChargerCable.Builder().BaseInfo("AppleA2244", "TPE", 1, 1).InterfaceType(InterfaceType.USB_TypeC, InterfaceType.Lightning).Build();BullSocket bullSocket = new BullSocket("公牛GN-B3440", 8, 1.6f, 10, 2500);DelixiSocket delixiSocket = new DelixiSocket("德力西CD98J-LK4X1.8", 4, 1.6f, 10, 2500);AndroidCharger miCharger = new AndroidCharger.Builder().BaseInfo("小米MDY-13-ES", "MDY-13-ES", InterfaceType.USB).Current(6.1f, 1.7f).Voltage(220, 11).Build();AppleCharger appleCharger = new AppleCharger.Builder().BaseInfo("AppleA2244", "A2244", InterfaceType.USB_TypeC).Current(2.22f, 0.5f).Voltage(240, 9).Build();//电源适配器连接充电线,充电线连接手机miCharger.ConnectCable(romosChargerCable).ConnectPhone(mi12);//电源适配器连接插座,插座连接家用电源miCharger.ConnectSocket(delixiSocket).ConnectPower(220);}

代码解说

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

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


http://www.ppmy.cn/news/195562.html

相关文章

互联网日报 | 8月9日 星期一 | 字节跳动否认重启上市计划;TikTok全球下载量去年居首;中国代表团38金32银18铜收官...

今日看点 ✦ 东京奥运会闭幕&#xff0c;中国代表团38金32银18铜圆满收官 ✦ 高德地图公交出行新增同城火车路线&#xff0c;首批上线30多城 ✦ 最快4小时出结果&#xff0c;百度健康核酸检测覆盖全国百余城市 ✦ 特斯拉西宁首个充电站开放&#xff0c;实现中国32个省会城市充电…

国家知识产权局宣告易百珑自发电专利权有效

2021年11月29日和30日&#xff0c;中国国家知识产权局专利复审委员会先后宣告易百珑的两项基础专利的专利权维持有效&#xff0c;这奠定了易百珑科技在微能量收集与自发电技术领域的翘楚地位。 被维持有效的专利故事还得从十多年前开始讲述。2009年3月易百珑成立电工实验室&…

赛微微电启动上市发行:生产依赖外协加工厂商,市盈率偏高

4月13日&#xff0c;模拟集成电路供应商——广东赛微微电子股份有限公司&#xff08;下称“赛微微电”&#xff0c;SH:688325&#xff09;启动在上交所科创板上市发行申购。这意味着&#xff0c;赛微微电将在不久后实现在科创板挂牌上市。 据贝多财经了解&#xff0c;赛微微电…

结构型设计模式

结构型设计模式目的是将现有的类或结构组合到一起形成更强大的结构。结构型设计模式共7种分别为适配器模式、桥接模式、组合模式、装饰模式、外观模式、享元模式、代理模式。 一、适配器模式 顾名思义适配器模式是将原本不匹配的两个东西适配到一起使用&#xff0c;以三孔插座…

什么牌子的台灯对孩子的视力好?618好用性价比高的护眼台灯

孩子的视力一直都是困扰家长的大问题了&#xff0c;随着科技发展速度&#xff0c;现在的儿童玩电子产品的时间增多了不少&#xff0c;家长应该合理的安排好时间&#xff0c;不然再好的的台灯也不起作用的&#xff0c;而适合孩子的台灯&#xff0c;还是护眼台灯最有效了&#xf…

贝叶斯分类器(贝叶斯决策论,极大似然估计,朴素贝叶斯分类器,半朴素贝叶斯分类器,贝叶斯网)学习笔记

贝叶斯分类器&#xff08;贝叶斯决策论&#xff0c;极大似然估计&#xff0c;朴素贝叶斯分类器&#xff0c;半朴素贝叶斯分类器&#xff0c;贝叶斯网&#xff09;学习笔记 一、条件概率&#xff0c;全概率公式&#xff0c;贝叶斯公式 二、贝叶斯决策论 贝叶斯决策论是概率框架…

加速户外电源快充普及!6大厂商率先推出双向快充芯片

户外电源、便携式储能电源作为一种典型的清洁能源系统&#xff0c;近年来在欧美国家实现了大范围普及&#xff0c;并融入到了人们日常生活当中。国内方面&#xff0c;虽然目前户外电源的普及程度不如国外&#xff0c;但当下国家正在大力倡导新能源产业&#xff0c;作为绿色能源…

设计模式之适配器模式(Adapter)

设计模式之适配器模式&#xff08;Adapter&#xff09; 我想要是说适配器模式&#xff0c;提一下“电压”、“耳机”、“充电器”和 "USB” 的例子应该是比较恰当的了。就说说自己的亲身经历&#xff0c;我原来的手机是NOKIA5730。后来也不知道有没有国行&#xff0c;但是…