.net设计模式装饰者设计

2024-12-03 08:54:02

1、首先你需要建一个控制台应用程序,命名为装饰者设计模式,方便查看,如图所示:

.net设计模式装饰者设计

2、新建一个抽象类Component,添加一个抽象方法Operation,如图所示:

.net设计模式装饰者设计

3、编写两个实现类ConcreteComponent,Decorator继承于Component,然后在其中实现Operation方法。

.net设计模式装饰者设计

4、装饰者模式用到多继承关系,添加两个类ConcreteDecoratorA,ConcreteDecoratorB继承于Decorator,分别写出对应的实现方法Operation,如图所示:

.net设计模式装饰者设计

5、编写实现主方法,如图所示:

.net设计模式装饰者设计

6、运行程序,得到相应的结果值。有没有觉得很巧妙呢。

.net设计模式装饰者设计
猜你喜欢