zoukankan      html  css  js  c++  java
  • 设计模式之六(装饰模式)

    前言

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

    装饰模式结构图

     

    Component是定义一个对象接口,可以给这些对象动态添加职责

    ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责

    Decorator装饰抽象类,继承了Component,从外类来扩展Componnt类的功能,但对于Component来说,是无需知道Decorator的存在的

    代码实现

    Component类

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

    ConcreteComponnet类

        public class ConcreteComponent : Component
        {
            public override void Operation()
            {
                Console.WriteLine("具体对象的操作");
            }
        }

    Decorator类

        public abstract class Decorator : Component
        {
            protected Component component;
    
            /// <summary>
            /// 设置Component
            /// </summary>
            /// <param name="component"></param>
            public void SetComponent(Component component)
            {
                this.component = component;
            }
    
            /// <summary>
            /// 重写Operation,实际执行的是Component的Operation
            /// </summary>
            public override void Operation()
            {
                if (component != null)
                {
                    component.Operation();
                }
            }
        }

    ConcreteDecoratorA

        public class ConcreteDecoratorA : Decorator
        {
            /// <summary>
            /// 本类独有功能,以区别于ConcreteDecoratorB
            /// </summary>
            private string AddedState;
    
            public override void Operation()
            {
                base.Operation();
                AddedState = "NewState";
                Console.WriteLine("具体装饰对象A的操作");
            }
        }

    ConcreteDecoratprB

        public class ConcreteDecoratorB : Decorator
        {
            public override void Operation()
            {
                base.Operation();
    
            }
    
            /// <summary>
            /// 本类独有的方法,以区别于ConcreteDecoratorA
            /// </summary>
            private void AddedBehavior()
            { 
                ///
            }
        }


    客户端调用实例

        public class Program
        {
            static void Main(string[] args)
            {
                ConcreteComponent cc = new ConcreteComponent();
                ConcreteDecoratorA ca = new ConcreteDecoratorA();
                ConcreteDecoratorB cb = new ConcreteDecoratorB();
    
                ca.SetComponent(cc);
                cb.SetComponent(ca);
                cb.Operation();
                Console.ReadLine();
            }
        }

    装饰的方式是:首先用ConcreteComponent实例化cc,然后用ConcreteComponentA的实例化对象ca来包装cc,再用ConcreteComponetB的对象cb包装ca,最终执行cb的Operation()

    其实就是利用Setcomponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

    不过也有特殊情况:如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没必要建立单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

    总结

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

    优点可以这样说:把类中的装饰功能从类中搬移去除,这样可以简化原有的类。有效的把类的核心职责和装饰功能区分开了。

  • 相关阅读:
    [省选联考 2020 A 卷] 组合数问题
    [HAOI2018]苹果树
    [集训队作业2013]城市规划
    多项式牛顿迭代
    多项式开方
    分治 FFT 模板的三种过法
    Graham 求静态凸包
    exp 初探
    HAOI2018 染色
    如何关闭wps热点,如何关闭wpscenter,如何关闭我的wps
  • 原文地址:https://www.cnblogs.com/aehyok/p/3107435.html
Copyright © 2011-2022 走看看