zoukankan      html  css  js  c++  java
  • 设计模式——装饰模式(Decorator Pattern)

    1. 作用

    装饰模式的作用:提供一种向某个对象动态添加状态和行为的方式。对象不知道它已经“被装饰”了,这使得它成为一种对系统设计非常有用的模式。

    装饰模式实现的关键点在于:装饰既继承了原始类,也包含装饰的实例。

    装饰模式的优点:

      1. 原始类不知道自己被装饰了。

      2. 不会一个大的,功能丰富的类会包含所有的功能。

      3. 装饰之间相互独立。

      4. 装饰可以混合搭配使用。

    2. 设计

    UML图:

      

    说明

      1. Component 和 Decorator 均实现了 IComponent 接口。

      2. Decorator 含有一个或者多个 IComponent 接口的对象。

      3. Client 含有一个或者多个 Component 和 Decorator 对象。

    3. 实现

    装饰模式的关键特性在于它不依赖于继承来实现扩展行为,相对于继承方式更加灵活:

      1. 实现接口的方法,改变Component的原有行为。

      2. 添加新的状态和行为

      3. 通过构造行为传递的对象,访问任何公开的成员。

    实现代码:

     1 using System;
     2 
     3 namespace DesignPatternConsoleDemo.DecoratorPattern
     4 {
     5     public interface IComponent
     6     {
     7         string Operation();
     8     }
     9     public class Component : IComponent
    10     {
    11         public string Operation()
    12         {
    13             return "I am walking ";
    14         }
    15     }
    16     public class DecoratorA : IComponent
    17     {
    18         IComponent component;
    19         public DecoratorA(IComponent c)
    20         {
    21             component = c;
    22         }
    23         public string Operation()
    24         {
    25             return component.Operation() + "and listening to Classic FM ";
    26         }
    27     }
    28     public class DecoratorB : IComponent
    29     {
    30         IComponent component;
    31         public string addedState = "past the Coffee Shop ";
    32         public DecoratorB(IComponent c)
    33         {
    34             component = c;
    35         }
    36         public string Operation()
    37         {
    38             return component.Operation() + "to school ";
    39         }
    40         public string AddedBehavior()
    41         {
    42             return "and I bought a cappuccino ";
    43         }
    44     }
    45 
    46     public class Client
    47     {
    48         private static void Display(string s, IComponent c)
    49         {
    50             Console.WriteLine(s + c.Operation());
    51         }
    52 
    53         public static void Demo()
    54         {
    55             Console.WriteLine("Decorator Pattern");
    56 
    57             IComponent component = new Component();
    58             Display("1. Basic component: ", component);
    59             Display("2. A-decorated: ", new DecoratorA(component));
    60             Display("3. B-decorated: ", new DecoratorB(component));
    61             Display("4. B-A-decorated: ", new DecoratorB(new DecoratorA(component)));
    62             DecoratorB b = new DecoratorB(component);
    63 
    64             Display("5. A-B-decorated: ", new DecoratorA(b));
    65             Console.WriteLine("			" + b.addedState + b.AddedBehavior());
    66 
    67         }
    68     }
    69 }

    调用:

     1 namespace DesignPatternConsoleDemo
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             DesignPatternConsoleDemo.DecoratorPattern.Client.Demo();
     8         }
     9     }
    10 }

    结果:

    Decorator Pattern
    1. Basic component: I am walking
    2. A-decorated: I am walking and listening to Classic FM
    3. B-decorated: I am walking to school
    4. B-A-decorated: I am walking and listening to Classic FM to school
    5. A-B-decorated: I am walking to school and listening to Classic FM
                            past the Coffee Shop and I bought a cappuccino

    4. 应用场景

    应用情况:

      1. 装饰模式非常适合图形处理,当然在视频和声音方面也是一样。例如:图形添加不同提示信息或者是不同颜色的边框,视频流被压缩成不同比特率,声音被传入不同的翻译服务。

      2. 装饰模式出现在I/O API操作时的概率比较高。

      3. 移动设备,浏览器以及其他手机程序开始更多应用装饰模式。例如:它可以创建适合更小屏幕的显示对象,包含滚动条并且不包含在桌面浏览器中显示的横幅。

      4. 装饰模式越来越有用,以至于在.NET 3.0中出现了Decorator类。例如:System.Windows.Controls。

    适用性:

      现有:

        1. 一个不能被继承的类A。

      目标:

        1. 向类A中动态的添加状态或者行为。

        2. 在一个类中改变对象的行为并且不要影响其他的。

        3. 因为产生了大量的类,所以避免使用子类。

      替换:

        1. 适配器模式:在不同类之间建立接口。

        2. 组合模式:聚合一个对象,但是不实现它的接口。

        3. 代理模式:明确对象的访问权限。

        4. 策略模式:改变原有对象,而不是包装它。

  • 相关阅读:
    MySQL Connection排查未授权用户(unauthenticated user)
    Git常用命令
    abc
    更新显卡驱动
    财务模块 采购、接收、应付会计分录和功能认识
    ebs界面颜色改变
    获取科目的描述 创建科目
    EBS总账(GL)模块常用表
    收款 付款 到总帐
    PO收料到入库的分析
  • 原文地址:https://www.cnblogs.com/zhuhc/p/3458379.html
Copyright © 2011-2022 走看看