[review]Design Pattern:Decorator

装饰者模式详解
本文深入探讨了装饰者模式的设计理念及其应用场景,强调了其在不修改原有组件的情况下动态添加功能的优势,并通过实例代码展示了如何实现装饰者模式。

Decorator

Add some new features to the object dynamically, it is more flexible than the inheritance, the original component doesn't need to have plenty of functions at the first time, but has only very frequent operations. The decorator is designed to add any other additional operations to the original component if necessary. The decorator pattern makes the high level class simple and decouples the system.

When we use this pattern

  • as i said above, if you want to add new features or operations dynamically
  • if the original component is hard to inherit
  • if the orininal component got lots of combinations in the future, if you use the inheritance mechanism, you have to implement lots of sub-classes. so the decorator is a better option.
  • another situation is if you want to add some new operations to the exsiting class, it is better to use this pattern without change the exsiting class

Roles in this pattern

  • Component: define some interface
  • ConcreteComonent: implement the Component, have the real operation
  • Decorator: maintain a reference of the Component and also implement the Component by inheritance
  • ConcreteDecorator: implement the Decorator, the very class to decorate the Component.

More descriptions

What the decorator really does is to call the Component's interface and after that do some more operation or change some states, that is the reason why we call it decorator.

Dynamic and flexible are the key words for this pattern, it is the client that decides the order of the decoration and decides how to decorate.

The very component does not need to implement all the potential operations but only the very frequent ones. The potential operations can be implenented by the decorator, you can make as many different concrete decorators as you want for different requirement.

what's more , the different decorators can be used together to decorate the component by the orders you want. compared with the inheritance by the sub-classes, you cannot use them together for the different orders, you got to implement the different ones just beacause of the order requirment.

The decorator is used in very certain time and very certain situation, so you can implement it and leave it there, when it is necessary, use it, which simplifies the original Component.

a small demo is designed to demonstate the above, see below:

namespace Decorator
{
public abstract class Component
{
public abstract void Operation();
}

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

public abstract class Decorator : Component
{
protected Component component;

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

public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}

public class ConcreteDecoratorA : Decorator
{
private string addedStatus;

public override void Operation()
{
base.Operation();
addedStatus = "New Status";
Console.WriteLine("ConcreteDecoratorA");
}
}

public class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddOperation();
Console.WriteLine("ConcreteDecoratorB");
}

private void AddOperation()
{
Console.WriteLine("New Operation in ConcretedecoratorB");
}
}

class Program
{
static void Main(string[] args)
{
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB();

d1.SetComponent(c);
d2.SetComponent(d1);

d2.Operation();
}
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值