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


javascript 复制代码
// 复杂子系统
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(); // 只需调用一个简单方法
csharp 复制代码
// 复杂子系统
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();
csharp 复制代码
// 不兼容的旧接口
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")); // 15

var adapter = new CalculatorAdapter(new NewCalculator());
Console.WriteLine(adapter.Calculate(10, 5, "add")); // 15 (使用新实现但保持旧接口)
相关推荐
是2的10次方啊11 小时前
设计模式与生活①:你的脸,为什么不能有第二个?
设计模式
高晶13 小时前
ARM 嵌入式 Linux 命令系统 BusyBox 实战指南
linux·设计模式·程序员
高晶14 小时前
语音合成与变声技术十大业务场景落地实战:从电商直播到规模化生产
设计模式·程序员
博风17 小时前
19、设计模式应用举例:桥接模式:制作电视节目
设计模式·桥接模式
知守观17 小时前
从 4 套 PDF 库的混战,聊聊老项目的依赖管理与“静默失败”
java·后端·设计模式
geovindu2 天前
rust: Mediator Pattern
开发语言·后端·设计模式·rust·中介者模式·行为模式
SENKS_DIGITAL2 天前
金属加工遇上数字化:通快展厅里的工业蜕变-森克思科技
科技·设计模式·展厅设计·展馆设计·科技展厅设计·展览设计·企业展厅设计
flower9803232 天前
C++消息总线软件开发框架AI重构版
c++·设计模式·架构
zzu123zsw3 天前
业务拆分后的治理深水区:解构企业级微服务六大核心设计模式与高可用落真实践
微服务·设计模式·架构·高可用·微服务架构设计
牧瀬クリスだ3 天前
观察者模式:一个对象变化,一群对象自动响应
java·观察者模式·设计模式