结构型设计模式

深入理解结构型设计模式:适配器、代理、装饰器模式(C#实例解析)

结构型设计模式专注于类和对象的组合方式,帮助开发者构建灵活、可扩展的软件架构。本文通过实际场景和C#代码示例,详细解析适配器、代理、装饰器三种常用结构型模式。


一、适配器模式(Adapter Pattern)

核心思想

充当两个不兼容接口之间的桥梁,使原本无法协同工作的类能够协同工作。

应用场景

  • 整合第三方库或遗留代码
  • 统一多个类的不一致接口

C#代码示例

csharp 复制代码
// 目标接口(客户端期望的格式)
public interface ILogger
{
    void Log(string message);
}

// 需要适配的类(不兼容接口)
public class FileLogger
{
    public void WriteToFile(string content)
    {
        File.WriteAllText("log.txt", content);
    }
}

// 适配器类
public class FileLoggerAdapter : ILogger
{
    private readonly FileLogger _fileLogger;

    public FileLoggerAdapter(FileLogger fileLogger)//通过构造方法取出FileLogger的实例
    {
        _fileLogger = fileLogger;
    }

    public void Log(string message)
    {
        _fileLogger.WriteToFile($"[{DateTime.Now}] {message}");
    }
}

// 使用示例
var adaptee = new FileLogger();
ILogger logger = new FileLoggerAdapter(adaptee);
logger.Log("System initialized"); // 统一调用Log方法

模式特点

  • 对象适配器:通过组合实现(推荐)
  • 类适配器:通过多继承实现(C#不支持)

二、代理模式(Proxy Pattern)

核心思想

通过代理对象控制对原始对象的访问,实现权限控制、延迟加载等功能。

应用场景

  • 图片/文件延迟加载
  • 访问权限控制
  • 远程方法调用

C#代码示例

csharp 复制代码
// 服务接口
public interface IImageLoader
{
    void DisplayImage();
}

// 真实服务
public class HighResImage : IImageLoader
{
    private readonly string _filename;

    public HighResImage(string filename)
    {
        // 模拟耗时加载
        Thread.Sleep(2000);
        _filename = filename;
    }

    public void DisplayImage()
    {
        Console.WriteLine($"Displaying {_filename}");
    }
}

// 代理类
public class ImageProxy : IImageLoader
{
    private HighResImage _realImage;
    private readonly string _filename;

    public ImageProxy(string filename)
    {
        _filename = filename;
    }

    public void DisplayImage()
    {
        _realImage ??= new HighResImage(_filename); // 延迟加载
        _realImage.DisplayImage();
    }
}

// 使用示例
IImageLoader image = new ImageProxy("photo.jpg");
// 真实对象尚未创建
image.DisplayImage(); // 此时才加载真实图片

代理类型

  • 虚拟代理:延迟创建开销大的对象
  • 保护代理:控制访问权限
  • 远程代理:本地代表远程对象

三、装饰器模式(Decorator Pattern)

核心思想

动态地为对象添加新功能,相比继承更加灵活。

应用场景

  • 为对象动态添加功能
  • 避免使用子类进行功能扩展

C#代码示例

csharp 复制代码
// 组件接口
public abstract class Coffee
{
    public abstract string GetDescription();
    public abstract double GetCost();
}

// 具体组件
public class SimpleCoffee : Coffee
{
    public override string GetDescription() => "Simple Coffee";
    public override double GetCost() => 1.0;
}

// 装饰器基类
public abstract class CoffeeDecorator : Coffee
{
    protected Coffee _decoratedCoffee;

    protected CoffeeDecorator(Coffee coffee)
    {
        _decoratedCoffee = coffee;
    }
}

// 具体装饰器
public class MilkDecorator : CoffeeDecorator
{
    public MilkDecorator(Coffee coffee) : base(coffee) {}

    public override string GetDescription() => 
        _decoratedCoffee.GetDescription() + ", Milk";

    public override double GetCost() => 
        _decoratedCoffee.GetCost() + 0.5;
}

// 使用示例
Coffee order = new SimpleCoffee();
order = new MilkDecorator(order); // 加牛奶
order = new MilkDecorator(order); // 再加一份牛奶

Console.WriteLine($"{order.GetDescription()} - ${order.GetCost()}");
// 输出:Simple Coffee, Milk, Milk - $2.0

模式特点

  • 保持开放-封闭原则
  • 多层装饰可叠加功能
  • 避免类爆炸问题

模式对比

模式 核心目的 关键区别
适配器 接口转换 解决兼容性问题
代理 访问控制 保持接口一致性
装饰器 功能扩展 动态添加职责

总结

结构型设计模式通过巧妙的组合方式提升系统灵活性:

  • 适配器解决接口不匹配问题
  • 代理优化对象访问控制
  • 装饰器实现动态功能扩展
相关推荐
「QT(C++)开发工程师」24 分钟前
C++设计模式
开发语言·c++·设计模式
茶本无香1 小时前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式
漂洋过海的鱼儿13 小时前
设计模式——EIT构型(三)
java·网络·设计模式
老蒋每日coding21 小时前
AI Agent 设计模式系列(十八)—— 安全模式
人工智能·安全·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十六)—— 资源感知优化设计模式
人工智能·设计模式·langchain
老蒋每日coding1 天前
AI Agent 设计模式系列(十七)—— 推理设计模式
人工智能·设计模式
冷崖1 天前
桥模式-结构型
c++·设计模式
连山齐名1 天前
设计模式之一——堵塞队列
设计模式
会员果汁1 天前
19.设计模式-命令模式
设计模式·命令模式
茶本无香1 天前
设计模式之六—组合模式:构建树形结构的艺术
java·设计模式·组合模式