当前位置: 首页 > article >正文

23种设计模式的定义和应用场景-02-结构型模式-C#代码

23种设计模式的定义和应用场景:

1. 创建型模式(共5种):

        单例模式(Singleton)、工厂方法模式(Factory Method)、抽象工厂模式(Abstract Factory)、建造者模式(Builder)、原型模式(Prototype)

2. 结构型模式(共7种):

        适配器模式(Adapter)、桥接模式(Bridge)、组合模式(Composite)、装饰器模式(Decorator)、外观模式(Facade)、享元模式(Flyweight)、代理模式(Proxy)

3. 行为型模式(共11种):

        责任链模式(Chain of Resposibility)、命令模式(Command)、解释器模式(Interpreter)、迭代器模式(Iterator)、中介者模式(Mediator)、备忘录模式(Mementor)、观察者模式(Oberver)、状态模式(State)、策略模式(Strategy)、模板方法模式(Template Method)、访问者模式 (Visitor)

2-结构型模式-C#代码

2. 结构型模式

2.1 适配器模式 (Adapter)

定义: 将一个类的接口转换成客户希望的另一个接口。
应用场景: 集成第三方库、旧系统升级。

public class Adaptee
{
    public void SpecificRequest() => Console.WriteLine("Specific Request");
}

public interface ITarget
{
    void Request();
}

public class Adapter : ITarget
{
    private Adaptee _adaptee = new Adaptee();

    public void Request() => _adaptee.SpecificRequest();
}
2.2 桥接模式 (Bridge)

定义: 将抽象部分与实现部分分离,使它们可以独立变化。
应用场景: 跨平台应用、图形绘制。

public interface IImplementor
{
    void OperationImpl();
}

public class ConcreteImplementorA : IImplementor
{
    public void OperationImpl() => Console.WriteLine("ConcreteImplementorA Operation");
}

public abstract class Abstraction
{
    protected IImplementor _implementor;

    public Abstraction(IImplementor implementor) => _implementor = implementor;

    public abstract void Operation();
}

public class RefinedAbstraction : Abstraction
{
    public RefinedAbstraction(IImplementor implementor) : base(implementor) { }

    public override void Operation() => _implementor.OperationImpl();
}
2.3 组合模式 (Composite)

定义: 将对象组合成树形结构以表示“部分-整体”的层次结构。
应用场景: 文件系统、UI组件。

public abstract class Component
{
    public abstract void Operation();
    public virtual void Add(Component component) => throw new NotImplementedException();
    public virtual void Remove(Component component) => throw new NotImplementedException();
}

public class Leaf : Component
{
    public override void Operation() => Console.WriteLine("Leaf Operation");
}

public class Composite : Component
{
    private List<Component> _children = new List<Component>();

    public override void Operation() => _children.ForEach(c => c.Operation());
    public override void Add(Component component) => _children.Add(component);
    public override void Remove(Component component) => _children.Remove(component);
}
2.4 装饰器模式 (Decorator)

定义: 动态地给对象添加额外的职责。
应用场景: 日志记录、权限检查。

public abstract class Component
{
    public abstract void Operation();
}

public class ConcreteComponent : Component
{
    public override void Operation() => Console.WriteLine("ConcreteComponent Operation");
}

public abstract class Decorator : Component
{
    protected Component _component;

    public Decorator(Component component) => _component = component;

    public override void Operation() => _component.Operation();
}

public class ConcreteDecoratorA : Decorator
{
    public ConcreteDecoratorA(Component component) : base(component) { }

    public override void Operation()
    {
        base.Operation();
        Console.WriteLine("ConcreteDecoratorA Operation");
    }
}
2.5 外观模式 (Facade)

定义: 提供一个统一的接口,用来访问子系统中的一群接口。
应用场景: 简化复杂系统的使用。

public class SubsystemA
{
    public void OperationA() => Console.WriteLine("SubsystemA Operation");
}

public class SubsystemB
{
    public void OperationB() => Console.WriteLine("SubsystemB Operation");
}

public class Facade
{
    private SubsystemA _subsystemA = new SubsystemA();
    private SubsystemB _subsystemB = new SubsystemB();

    public void Operation()
    {
        _subsystemA.OperationA();
        _subsystemB.OperationB();
    }
}
2.6 享元模式 (Flyweight)

定义: 通过共享技术有效地支持大量细粒度的对象。
应用场景: 文本编辑器中的字符对象。

public class Flyweight
{
    private string _intrinsicState;

    public Flyweight(string intrinsicState) => _intrinsicState = intrinsicState;

    public void Operation(string extrinsicState) => Console.WriteLine($"Intrinsic: {_intrinsicState}, Extrinsic: {extrinsicState}");
}

public class FlyweightFactory
{
    private Dictionary<string, Flyweight> _flyweights = new Dictionary<string, Flyweight>();

    public Flyweight GetFlyweight(string key)
    {
        if (!_flyweights.ContainsKey(key))
        {
            _flyweights[key] = new Flyweight(key);
        }
        return _flyweights[key];
    }
}
2.7 代理模式 (Proxy)

定义: 为其他对象提供一个代理以控制对这个对象的访问。
应用场景: 远程代理、虚拟代理、保护代理。

public interface ISubject
{
    void Request();
}

public class RealSubject : ISubject
{
    public void Request() => Console.WriteLine("RealSubject Request");
}

public class Proxy : ISubject
{
    private RealSubject _realSubject;

    public void Request()
    {
        if (_realSubject == null)
        {
            _realSubject = new RealSubject();
        }
        _realSubject.Request();
    }
}


http://www.kler.cn/a/542344.html

相关文章:

  • [7] 游戏机项目说明
  • 本地部署DeepSeek R1 + 界面可视化open-webui
  • STM32系统架构介绍
  • C++ Primer sizeof运算符
  • 【玩转 Postman 接口测试与开发2_019】第15章:利用 Postman 初探 API 性能测试(含实战截图)
  • android apk反编译
  • 青少年编程与数学 02-009 Django 5 Web 编程 05课题、数据库与ORM
  • PG高可用学习@2
  • 大模型基本原理(二)——ChatGPT的工作原理
  • BUU35 [DASCTF X GFCTF 2024|四月开启第一局]EasySignin 100 【gopher打mysql】
  • 飞牛 使用docker部署MoviePilot V2自动化影视管理平台教程
  • 剪辑如何剪辑制作视频短视频剪辑学习怎么学,难吗?
  • 安川伺服控制器MP系列优势特点及行业应用
  • Oracle入门精读03_Oracle11g安装目录及子目录的结构简介
  • 计算机毕业设计Tensorflow+LSTM空气质量监测及预测系统 天气预测系统 Spark Hadoop 深度学习 机器学习 人工智能
  • 认识一下redis的分布式锁
  • aspectFill(填充目标区域的同时保持图像的原有宽高比 (aspect ratio)图像不会被拉伸或压缩变形
  • 2.11 sqlite3数据库【数据库的相关操作指令、函数】
  • Java 使用腾讯翻译 API 实现含 HTML 标签文本,json值,精准翻译工具
  • 机器学习怎么学习,还有算法基本的源代码
  • 青少年编程与数学 02-009 Django 5 Web 编程 06课题、模型定义
  • 深度剖析责任链模式
  • 社区版IDEA中配置TomCat(详细版)
  • 【强化学习入门笔记】3.2 策略梯度法:REINFORCE
  • 什么是矩阵账号?如何做矩阵账号运营?
  • HarmonyOS NEXT - picker 选择器( 包含 单列、多列、底部选择器)