【设计模式】装饰者模式

装饰者模式

角色和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();
相关推荐
Larcher6 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
Scout-leaf8 小时前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6259 小时前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech15 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
咖啡八杯1 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
2601_962072551 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
:mnong1 天前
学习创建结构行为设计模式
设计模式
m0_547486662 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆2 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
w_t_y_y2 天前
Agent设计模式(四)多模态融合模式(Multi-Modal Fusion)
设计模式