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


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 (使用新实现但保持旧接口)
相关推荐
Blossom.1184 分钟前
基于Mamba-2的实时销量预测系统:如何用选择性状态空间干掉Transformer的O(n²)噩梦
人工智能·python·深度学习·react.js·机器学习·设计模式·transformer
一个处女座的暖男程序猿20 分钟前
3大类设计模式
设计模式
双木的木25 分钟前
Coggle数据科学 | 并行智能体:洞察复杂系统的 14 种并发设计模式
运维·人工智能·python·设计模式·chatgpt·自动化·音视频
执笔论英雄1 小时前
【RL】Slime异步原理(单例设计模式)3
设计模式
老鼠只爱大米1 小时前
Java设计模式之装饰器模式详解
java·设计模式·装饰器模式·decorator·java设计模式
9***Y481 小时前
Web3预言机设计模式
设计模式·web3
wudl55662 小时前
Agent 设计模式全面分析
设计模式
__万波__2 小时前
二十三种设计模式(四)--原型模式
java·设计模式·原型模式
4***g8942 小时前
Java进阶-SpringCloud设计模式-工厂模式的设计与详解
java·spring cloud·设计模式
__万波__2 小时前
二十三种设计模式(五)--建造者模式
java·设计模式·建造者模式