【设计模式】装饰者模式

装饰者模式

角色和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();
相关推荐
郝学胜-神的一滴11 小时前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
hixiong12313 小时前
TensorRT转换工具分享
人工智能·计算机视觉·ai·c#
饼干哥哥13 小时前
用Claude Code跑通跨境电商的5大场景,顶10个人的团队
人工智能·设计模式·正则表达式
折哥的程序人生 · 物流技术专研14 小时前
第3篇:手写促销策略引擎(满减、打折、立减)
java·设计模式·策略模式·开闭原则·编程实战·扩充系列·促销引擎
莫生灬灬14 小时前
DY联系人聊天Web面板支持获取联系人/聊天记录/发送消息
前端·chrome·websocket·c#
折哥的程序人生 · 物流技术专研16 小时前
第3篇:手写一个饮品制作模板(附代码)
java·设计模式·行为型模式·模版方法模式·钩子方法·代码复用·编程实战
逝水无殇16 小时前
C# 多态性详解
开发语言·后端·c#
逝水无殇16 小时前
C# 继承(Inheritance)详解
开发语言·后端·c#
ttod_qzstudio17 小时前
【软考设计模式】单例模式:唯一实例的管控与线程安全精讲
单例模式·设计模式
折哥的程序人生 · 物流技术专研18 小时前
第4篇:模板方法 vs 策略模式,面试怎么选?
java·设计模式·策略模式·行为型模式·模版方法模式·扩充系列·继承v组合