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


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 (使用新实现但保持旧接口)
相关推荐
GISer_Jing1 小时前
AI Agent 人类参与HITL与知识检索RAG
人工智能·设计模式·aigc
Tiny_React6 小时前
Claude Code Skills 自优化架构设计
人工智能·设计模式
胖虎110 小时前
iOS中的设计模式(十)- 中介者模式(从播放器场景理解中介者模式)
设计模式·中介者模式·解耦·ios中的设计模式
Geoking.10 小时前
【设计模式】组合模式(Composite)详解
java·设计模式·组合模式
刀法孜然10 小时前
23种设计模式 3 行为型模式 之3.6 mediator 中介者模式
设计模式·中介者模式
Yu_Lijing11 小时前
基于C++的《Head First设计模式》笔记——单件模式
c++·笔记·设计模式
Geoking.11 小时前
【设计模式】外观模式(Facade)详解
java·设计模式·外观模式
点云SLAM11 小时前
C++设计模式之单例模式(Singleton)以及相关面试问题
c++·设计模式·面试·c++11·单例模式(singleton)
GISer_Jing1 天前
AI Agent 目标设定与异常处理
人工智能·设计模式·aigc
蔺太微1 天前
组合模式(Composite Pattern)
设计模式·组合模式