大话设计模式-装饰模式(6)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    abstract class Component
    {
        public abstract void Operation();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    abstract class Decorator:Component
    {
        protected Component component;

        public void SetComponent(Component component)
        {
            this.component = component;
        }

        public override void Operation()
        {
            if (component != null)
            {
                component.Operation();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class ConcreteComponent:Component
    {
        public override void Operation()
        {
            Console.WriteLine("具体对象的操作");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class ConcreteDecoratorA : Decorator
    {
        private string addedState;

        public override void Operation()
        {
            base.Operation();
            addedState = "New A";
            Console.WriteLine("具体装饰对象 A 的操作:" + addedState);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class ConcreteDecoratorB : Decorator
    {
        public override void Operation()
        {
            base.Operation();
            string str = AddedBehavior();
            Console.WriteLine("具体装饰对象 B 的操作:" + str);
        }

        private string AddedBehavior()
        {
            return "New B";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Person
    {
        public Person() { }

        private string name;

        public Person(string name)
        {
            this.name = name;
        }

        public virtual void Show() 
        {
            Console.WriteLine("装扮的{0}",name);
        } 
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Finery:Person
    {
        protected Person component;

        public void Decorator(Person component)
        {
            this.component = component;
        }

        public override void Show()
        {
            if (component != null)
            {
                component.Show();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Sneakers:Finery
    {
        public override void Show()
        {
            Console.WriteLine("破球鞋");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class BigTrouser:Finery
    {
        public override void Show()
        {
            Console.WriteLine("垮裤");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class TShirts:Finery
    {
        public override void Show()
        {
            Console.WriteLine("大T恤");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class LeatherShoes:Finery
    {
        public override void Show()
        {
            Console.WriteLine("皮鞋");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Tie:Finery
    {
        public override void Show()
        {
            Console.WriteLine("领带");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Suit:Finery
    {
        public override void Show()
        {
            Console.WriteLine("西装");
            base.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecoratorFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 装饰模式
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            d1.SetComponent(c);
            d2.SetComponent(d1);
            d2.Operation();

            Console.ReadLine();
            #endregion

            #region 示例

            Console.WriteLine("第一种装扮");

            Person xc = new Person("小菜");
            Sneakers pqx = new Sneakers();
            BigTrouser kk = new BigTrouser();
            TShirts dtx = new TShirts();

            pqx.Decorator(xc);
            kk.Decorator(pqx);
            dtx.Decorator(kk);
            dtx.Show();

            Console.WriteLine("
第二种装扮");

            LeatherShoes px = new LeatherShoes();
            Tie ld = new Tie();
            Suit xz = new Suit();

            px.Decorator(xc);
            ld.Decorator(px);
            xz.Decorator(ld);
            xz.Show();

            #endregion

            Console.ReadLine();
        }
    }
}

 装饰模式

    装饰模式是为已有功能动态的添加更多功能的一种方式,在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。

     它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

    当系统需要新功能的时候,是向旧的类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为,在主类中加入了新的字段,新的方法和新的逻辑,

  从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要,装饰模式却提供了一个非常好的解决方案,它把每个

  要装饰的功能放在单独的类中,并让这个类包装它所需要的对象,因此,当需要执行特俗行为时,客户代码就可以在运行时根据需要有选择的,按顺序的使用装饰功能包装对象了。

     优点

    1.把类中的装饰功能从类中搬移去除,这样可以简化原有的类,有效的把类的核心职责和装饰功能区分开了。而且去除相关类中重复的装饰逻辑。

    2.通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。

  缺点

     1. 这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。

    2. 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。

设计原则

    1. 多用组合,少用继承。利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。

      然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。

    2. 类应设计的对扩展开放,对修改关闭。

原文地址:https://www.cnblogs.com/rinack/p/5262146.html