装饰模式(Decorator)

 

  • 写在定义之前
    • 装饰者和被装饰者有相同的超类型;
    • 可以使用一个或多个装饰者包装(wrap)一个对象;
    • 基于相同的超类型,任何需要原始对象的场合可以使用装饰过的对象代替它;
    • 装饰者可以在所委托被装饰者之前或/与之后,加上自己的行为,以达到特定目的;
    • 对象可以在运行时动态的、不限量的使用任何装饰者装饰;
  • 定义

    • 动态的将责任附加到对象上,若有扩展功能,装饰者提供比继承更有弹性的替代方案。

DecoratorByDP

  • 写在定义之后

    • 利用继承是为到达“类型匹配”(共同的超类型),而不是为扩展行为;
    • 装饰者必须能够取代被装饰者;
    • 行为来自基础组件(抽象类)和装饰者或与其它装饰者之间的组合关系; 
    • 抽象组件+具体组件+抽象装饰者+具体装饰者
    • 组合:装饰者的一个实例变量维护一个被装饰者(基于超类型)的引用,需要使用合适的方式为此引用赋值,比如使用构造方法赋值,同时此引用可以作为一个委托运行时调用被装饰对象的成员;
    • is a”关系是可包装的前提,包装者维护一个被包装者引用(has a),以“对外透明”的方式(内部)使用委托调用被包装者成员。
  • 类图

DecoratorClass

  • 示例代码(C#):

 

 

using  System;

namespace  Decorator
{
    
class MainClass
    
{
        
public static void Main(string[] args)
        
{
            
// Create ConcreteComponent and two Decorators
            
            ConcreteComponent Con 
= new ConcreteComponent();
            ConcreteDecoratorA DeA 
= new ConcreteDecoratorA();
            ConcreteDecoratorB DeB 
= new ConcreteDecoratorB();
            
            
// Link decorators 
            DeA.SetComponent(Con);
            DeB.SetComponent(DeA);
            
            DeB.Operation();
            
            Console.Write(
"Press any key to continue . . . ");
            Console.ReadKey(
true);
        }

        
        
// "Component"
        abstract class Component
        
{
            
public abstract void Operation();
        }

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

        }

        
        
// "Decorator"
        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"
        class ConcreteDecoratorA : Decorator
        
{
            
private string addedState;
            
            
public override void Operation()
            
{
                component.Operation();
                addedState 
= "New State";
                Console.WriteLine(
"ConcreteDecoratorA.Operation()");
            }

        }

        
        
// "ConcreteDecoratorB"
        class ConcreteDecoratorB : Decorator
        
{
            
public override void Operation()
            
{
                component.Operation();
                AddedBehavior();
                Console.WriteLine(
"ConcreteDecoratorB.Operation()");
            }

            
            
void AddedBehavior()
            
{
                
//Add Codeing
            }

        }

        
    }

}

 

 

  • 运行结果:

Concretor.Operation()
ConcreteDecoratorA.Operation()
ConcreteDecoratorB.Operation()
Press any key to continue . . .

  • 生活趣解:
    • DECORATOR—Mary过完轮到Sarly过生日,还是不要叫她自己挑了,不然这个月伙食费肯定玩完,拿出我去年在华山顶上照的照片,在背面写上“最好的的礼物,就是爱你的Fita”,再到街上礼品店买了个像框(卖礼品的MM也很漂亮哦),再找隔壁搞美术设计的Mike设计了一个漂亮的盒子装起来……,我们都是Decorator,最终都在修饰我这个人呀,怎么样,看懂了吗?

  • 参考资料:
    1. 《Head.First.Design.Patterns》中文版 (美)Elisabeth Freeman,Eric Freeman,Bert Bates,Kathy Sierra  2007.9
    2. 以及来自网络零散文章(作者不详)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值