【设计模式】装饰者模式

装饰者模式

角色和buff进行解释

步骤

  1. 角色和Buff共有一个Component,理解为有同一个操作,给予Buff里面可以填充角色。
  2. 角色有一个基类,Buff有一个基类,因为有多种Buff

理解

不是常规理解上的给角色填装Buff,角色作为主体,而是把角色放进Buff里,这里要做区分。

代码

csharp 复制代码
public class Role : Component
{
    public override void Opration()
    {
        Debug.LogError("我是某个角色");
    }
}

public class Buff : Component
{
    private Component role;

    public void SetComponent(Component component)
    {
        role = component;
    }

    public override void Opration()
    {
        if (role != null)
        {
            role.Opration();
        }
    }
}

public class ZengShang : Buff
{
    public override void Opration()
    {
        base.Opration();
        Debug.LogError("增加了伤害");
    }
}

public class JianShang : Buff
{
    public override void Opration()
    {
        base.Opration();
        Debug.LogError("减少了伤害");
    }
}


//调用

   Role role = new Role();
   ZengShang zengShang = new ZengShang();
   JianShang jianShang = new JianShang();

   zengShang.SetComponent(role);
   jianShang.SetComponent(zengShang);
   jianShang.Opration();
相关推荐
_哆啦A梦19 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530142 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools3 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的3 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
lindexi4 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
qq_454245034 天前
基于组件与行为的树状节点系统
数据结构·c#
bugcome_com4 天前
C# 类的基础与进阶概念详解
c#
雪人不是菜鸡4 天前
简单工厂模式
开发语言·算法·c#