Unity设计模式——装饰模式

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

Component类:

cs 复制代码
abstract class Component : MonoBehaviour
{
    public abstract void Operation();
}

ConcreteComponent类:

cs 复制代码
class ConcreteComponent : Component
{
    public override void Operation()
    {
        Debug.Log("具体对象操作");
    }
}

Decorator类:

cs 复制代码
abstract class Decorator : Component
{
    protected Component component;
    public void SetComponent(Component component)
    {
        this.component = component;
    }
    public override void Operation()
    {
        if(component != null)
        {
            component.Operation();
        }
    }
}

ConcreteDecoratorA类:

cs 复制代码
class ConcreteDecoratorA : Decorator
{
    //此类特有的功能,区别于ConcreteDecoratorB
    private string addState;
    public override void Operation()
    {
        //首先运行元Component的Operation()
        //再执行本类功能,对原Component进行修饰
        base.Operation();
        addState = "new state";
        Debug.Log("具体装饰对象A的操作");
    }
}

ConcreteDecoratorB类:

cs 复制代码
class ConcreteDecoratorB : Decorator
{
    public override void Operation()
    {
        //首先运行元Component的Operation()
        //再执行本类功能,对原Component进行修饰
        base.Operation();
        AddBehavior();
        Debug.Log("具体装饰对象B的操作");
    }

    //本类特有的方法
    private void AddBehavior()
    {
        
    }
}

客户端:

cs 复制代码
class Main : MonoBehaviour
{
    private void Start()
    {
        ConcreteComponent c = new ConcreteComponent();
        ConcreteDecoratorA a = new ConcreteDecoratorA();
        ConcreteDecoratorB b = new ConcreteDecoratorB();

        a.SetComponent(c);
        b.SetComponent(a);
        b.Operation();

    }
}

输出结果:

cs 复制代码
具体装饰对象A的操作 具体装饰对象B的操作 具体对象操作

装饰模式是利用SetComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自已的功能,不需要关心如何被添加到对象链当中。

如果只有一个Concrete Component类而没有抽象的Component类,那么Decorator类可以是Concrete Component的一个子类。同样道理,如果只有一个Concrete Decorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和Concrete Decorator的责任合并成一个类。

起初的设计中,当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为 ,但这种做法的问题在于,它们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式却提供了一个非常好的解决方案 ,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

相关推荐
geovindu11 小时前
go: Strategy Pattern
开发语言·设计模式·golang·策略模式
嵌入式学习_force17 小时前
02_state
设计模式·蓝牙
qcx2320 小时前
Warp源码深度解析(七):Token预算策略——双轨计费、上下文溢出与摘要压缩
人工智能·设计模式·rust·wrap
Cosolar1 天前
提示词工程面试题系列 - Zero-Shot Prompting 和 Few-Shot Prompting 的核心区别是什么?
人工智能·设计模式·架构
geovindu2 天前
go:Template Method Pattern
开发语言·后端·设计模式·golang·模板方法模式
钝挫力PROGRAMER2 天前
贫血模型的改进
java·开发语言·设计模式·架构
qcx232 天前
Warp源码深度解析(二):自研GPU UI框架——WarpUI的ECH模式与渲染管线
人工智能·ui·设计模式·rust
qcx232 天前
Warp源码深度解析(三):Block-Based终端引擎——Grid模型、PTY与Shell Integration
人工智能·设计模式·架构·wrap
mounter6252 天前
Linux Kernel Design Patterns (Part 2):从经典链表到现代 XArray,拆解内核复杂数据结构的设计哲学
linux·数据结构·链表·设计模式·内存管理·kernel
rrr22 天前
【PyQt5】| 多线程设计模式
开发语言·qt·设计模式