当前位置: 首页 > news >正文

设计模式》》门面模式 适配器模式 区别

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

// 复杂子系统
class CPU {start() { console.log("CPU启动"); }
}
class Memory {load() { console.log("内存加载"); }
}
class HardDrive {read() { console.log("硬盘读取"); }
}// 门面
class ComputerFacade {constructor() {this.cpu = new CPU();this.memory = new Memory();this.hardDrive = new HardDrive();}start() {this.cpu.start();this.memory.load();this.hardDrive.read();console.log("电脑启动完成");}
}// 客户端代码
const computer = new ComputerFacade();
computer.start(); // 只需调用一个简单方法
// 复杂子系统
public class CPU
{public void Start() => Console.WriteLine("CPU启动");
}public class Memory
{public void Load() => Console.WriteLine("内存加载");
}public class HardDrive
{public void Read() => Console.WriteLine("硬盘读取");
}// 门面类
public class ComputerFacade
{private readonly CPU _cpu;private readonly Memory _memory;private readonly HardDrive _hardDrive;public ComputerFacade(){_cpu = new CPU();_memory = new Memory();_hardDrive = new HardDrive();}public void Start(){_cpu.Start();_memory.Load();_hardDrive.Read();Console.WriteLine("电脑启动完成");}
}// 使用
var computer = new ComputerFacade();
computer.Start();

在这里插入图片描述

// 不兼容的旧接口
public interface IOldCalculator
{int Calculate(int a, int b, string operation);
}public class OldCalculator : IOldCalculator
{public int Calculate(int a, int b, string operation){return operation switch{"add" => a + b,"sub" => a - b,_ => throw new NotSupportedException()};}
}// 新接口(期望的)
public interface INewCalculator
{int Add(int a, int b);int Subtract(int a, int b);
}public class NewCalculator : INewCalculator
{public int Add(int a, int b) => a + b;public int Subtract(int a, int b) => a - b;
}// 适配器
public class CalculatorAdapter : IOldCalculator
{private readonly INewCalculator _newCalculator;public CalculatorAdapter(INewCalculator newCalculator){_newCalculator = newCalculator;}public int Calculate(int a, int b, string operation){return operation switch{"add" => _newCalculator.Add(a, b),"sub" => _newCalculator.Subtract(a, b),_ => throw new NotSupportedException()};}
}// 使用
IOldCalculator oldCalc = new OldCalculator();
Console.WriteLine(oldCalc.Calculate(10, 5, "add")); // 15var adapter = new CalculatorAdapter(new NewCalculator());
Console.WriteLine(adapter.Calculate(10, 5, "add")); // 15 (使用新实现但保持旧接口)

在这里插入图片描述

http://www.dtcms.com/a/279605.html

相关文章:

  • 基于Android的
  • 数据可视化全流程设计指南
  • hi3519dv500开发环境搭建及SDK编译和烧录:
  • Linux从零到一的学习
  • 【DOCKER】-6 docker的资源限制与监控
  • Datawhale AI夏令营——用户新增预测挑战赛
  • 营销创意可以从哪些角度挖掘?
  • HNSW(分层导航最小世界)算法:高维向量检索的导航革命
  • 龙虎榜——20250714
  • 手滑误操作? vue + Element UI 封装二次确认框 | 附源码
  • 基于SpringBoot+Vue的体育馆预约管理系统(支付宝沙盒支付、腾讯地图API、协同过滤算法、可视化配置、可视化预约)
  • JAVA并发——volatile关键字的作用是什么
  • 高并发点赞场景Synchronized、AtomicLong、LongAdder 和 LongAccumulator性能分析
  • Linux 系统管理基础教程
  • MyBatis 在执行 SQL 时找不到名为 name 的参数
  • PO类与分层架构
  • UI前端大数据可视化新实践:如何利用数据动画讲述数据背后的故事?
  • Redis高可用集群一主从复制概述
  • SSH 登录失败,封禁IP脚本
  • 理解Grafana中`X-Scope-OrgID`的作用与配置
  • JavaWeb与HTTP协议
  • 【FPGA】AXI总线协议
  • 李宏毅(deep-leraning)-四---梯度下降batch size
  • 品质童装好而不贵!百胜中台助力久岁伴稳步发展
  • 今日行情明日机会——20250714
  • openEuler系统串口文件手法压力测试及脚本使用说明
  • 破解 VMware 迁移难题:跨平台迁移常见问题及自动化解决方案
  • 我的第一个开源项目:SpringCloud电商前端Vue实战
  • AI驱动编程范式革命:传统开发与智能开发的全维度对比分析
  • 《解锁音频处理新姿势:探索Librosa的无限可能》