Amazing .NET.NET

设计模式之装饰模式

2020-08-16  本文已影响0人  天天向上卡索

装饰模式 Decorator

Intro

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

使用场景

装饰模式是为已有功能动态地添加更多功能的一种方式

当系统需要新功能的时候,是向旧的类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为,但是往往会在主类中加入新的字段/方法/逻辑,从而增加了主类的复杂度,
而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要

装饰模式提供了一个很好的方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它要装饰的对象,
当需要执行特殊行为时,就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

装饰模式的优点时把类中的装饰功能从类中搬移去除,这样可以简化原有的类,这样做就有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。

Prototype

internal abstract class Component
{
    public abstract void Operation();
}
internal class ConcreteComponent : Component
{
    public override void Operation()
    {
        Console.WriteLine("Operation executed in ConcreteComponent");
    }
}

internal abstract class Decorator : Component
{
    protected Component Component;

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

    public override void Operation()
    {
        Component?.Operation();
    }
}
internal class DecoratorA : Decorator
{
    private string _state;

    public override void Operation()
    {
        base.Operation();
        _state = "executed";
        Console.WriteLine($"operation in DecoratorA, state:{_state}");
    }
}
internal class DecoratorB : Decorator
{
    public override void Operation()
    {
        base.Operation();
        Console.WriteLine("operation in DecoratorB");
        AddBehavior();
    }

    private void AddBehavior()
    {
        Console.WriteLine("another behavior");
    }
}


Component component = new ConcreteComponent();
Decorator decorator = new DecoratorA();
decorator.SetComponent(component);
decorator.Operation();

Decorator decorator1 = new DecoratorB();
decorator1.SetComponent(decorator);
decorator1.Operation();

More

装饰器模式主要解决继承关系过于复杂的问题,通过组合来替代继承。它主要的作用是给原始类添加增强功能。这也是判断是否该用装饰器模式的一个重要的依据。

Reference

上一篇 下一篇

猜你喜欢

热点阅读