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


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 (使用新实现但保持旧接口)
相关推荐
nVisual11 小时前
运维新纪元:告别Excel手工规划,实现“零误差”决策
运维·网络·设计模式·自动化
喝拿铁写前端1 天前
Vue 实战:构建灵活可维护的菜单系统
前端·vue.js·设计模式
VisuperviReborn2 天前
打造自己的前端监控---前端流量监控
前端·设计模式·架构
BUG收容所所长2 天前
发布订阅模式 vs 观察者模式:它们真的是一回事吗?
前端·javascript·设计模式
探索为何2 天前
Transformer:从神坛到笑坛的华丽转身
设计模式·程序员·代码规范
AlenLi2 天前
JavaScript - 单例模式的几种简单实现方式
设计模式
心月狐的流火号2 天前
观察者模式解析与Spring事件机制
spring·设计模式
用户6120414922132 天前
C语言做的汽车线路模拟查询系统
c语言·后端·设计模式
#六脉神剑2 天前
接口请求的后台发起确认
低代码·设计模式·产品运营·mybuilder
二闹3 天前
高效开发秘籍:CRUD增强实战
后端·设计模式·性能优化