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


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 (使用新实现但保持旧接口)
相关推荐
善良勤劳勇敢而又聪明的老杨11 小时前
【AI编程系列】MCP 常用设计模式解读
设计模式·ai编程
geovindu13 小时前
java: Strategy Pattern
java·开发语言·后端·设计模式·策略模式·行为模式
码匠许师傅1 天前
【设计模式精讲】29.行为型模式总结与对比(Behavioral Patterns Summary)
c++·设计模式·uml
geovindu1 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
yuehuaxin012 天前
LP8728A/LP8728B 芯茂微|SOP8L 副边反馈 PWM,18‑30W 适配器恒功率补偿方案解析
嵌入式硬件·智能家居·适配器模式·智能硬件
YHHLAI2 天前
NestJS 后端开发实战:从设计模式到 CRUD 全栈
设计模式
2401_868534782 天前
长远目标 Long-term Goal 老题沿用
设计模式·需求分析
吃饱了得干活2 天前
Java设计模式实战:一个支付模块的重构之旅,层层递进理解设计模式精髓
后端·设计模式·架构
vivo互联网技术3 天前
软件不是从数据开始,而是从现实开始 | KDC 系列 01
设计模式·架构·领域驱动设计
码匠许师傅3 天前
【设计模式精讲】27.模板方法模式(Template Method)
c++·设计模式·uml·模板方法模式