zoukankan      html  css  js  c++  java
  • 装饰(Decorator)模式

    1、装饰(Decorator)模式
        动态给一个对象添加一些额外的职责。就增加功能来说,装饰模式比生成子类更为灵活。
    Component是定义一个对象接口、可以给这些对象动态地添加职责。ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。
    2、结构图
      
    Component 类:
        abstract class Component
        {
            public abstract void Operation();
        }
    ConcreteComponent类:
        class ConcreteComponent : Component
        {
            public voerride void Operation()
            {
                Console.WriteLine("具体对象的操作");
            }
        }
    Decorator类:
    abstract class Decorator:Component
    {
        protected Component component;
        public void SetComponent(Component component)//设置Component
        {
            this.component = component;
        }
        public voerride void Operation()
        {
            if(component != null)
            {
                component.Operation();
            }
        }
    }
    ConcreteDecoratorA 类:
    class ConcreteDecoratorA : Decorator
    {
        private string addedState;//本类的独有功能,以区别于ConcreteDecoratorB
        public voerride void Operation()
        {
            base.Operation();
            addedState = "New State";
            Console.WriteLine("具体装饰对象A的操作");
        }
    }
    ConcreteDecoratorB 类:
    class ConcreteDecoratorB : Decorator
    {
        private string addedState;//本类的独有功能,以区别于ConcreteDecoratorB
        public voerride void Operation()
        {
            base.Operation();//首先运行原Component的Operation(),再执行本类的功能,如AddedBehavior(),相当于对原Component进行了装饰
            addedBehavior();
            Console.WriteLine("具体装饰对象B的操作");
        }
        
        private void addedBehavior()//本类独有的方法以区别于ConcreteDecoratorA
        {
            
        }
    }

    客户端代码:
    static void Main(string[] args)
    {
        ConcreteComponent c = new ConcreteComponent();
        ConcreteDecoratorA da = new ConcreteDecoratorA();
        ConcreteDecoratorB db = new ConcreteDecoratorB();
        da.SetComponent(c);
        db.SetComponent(da);
        db.Operation();
        Console.Read();    
    }
          装饰模式是利用SetCompnent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。
          如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
    3、总结
          装饰模式是为已有功能动态地添加更多功能的一种方式。当系统需要新功能的时候,是向旧的类中添加新的代码。这些新添加的代码通常装饰了原有类的核心职责或主要行为,他们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功放在一个单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象。
          装饰模式优点是把类中的装饰功能从类中搬移去除,这样可以简化原有的类,有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。

  • 相关阅读:
    个人网址收集
    使用 TListView 控件(2)
    C# 语法练习(14): 类[六] 事件
    如何在 "万一的 Delphi 博客" 回复自动格式化的着色代码?
    使用 TListView 控件(1)
    C# 语法练习(13): 类[五] 索引器
    使用 TListView 控件(4)
    C# 语法练习(11): 类[三] 构造函数、析构函数、base、this
    C# 语法练习(12): 类[四] 抽象类与抽象成员、密封类与密封成员
    使用 TListView 控件(3)
  • 原文地址:https://www.cnblogs.com/robertsun/p/5224324.html
Copyright © 2011-2022 走看看