1. 程式人生 > >設計模式——裝飾模式Decorate

設計模式——裝飾模式Decorate

裝飾模式定義:動態給一個物件新增一些額外的職責,就增加功能來說,裝飾模式比生成子類更為靈活

Component是定義了一個物件介面,可以給這些物件動態新增職責。

ConcreteComponent是定義了一個具體的物件,也可以給這個物件新增一些職責

Decorator裝飾抽象類,繼承了Component從外類來擴充套件Component類的功能,但對於Component來說,是無需知道Decorater的存在。至於ConcreteDecorator,起到給Component新增職責的功能

裝飾模式是為已有功能動態新增功能的方式,當系統需要新功能時,向舊的類新增程式碼。這些新加的程式碼通常裝飾了原有類的核心職責或主要行為。裝飾模式提供了一個非常好的解決方案,把每個要裝飾的功能放在單獨的類中,並讓這個類包裝它所需要裝飾的物件,因此,當需要執行特殊行為時,客戶程式碼就可以在執行根據需要有選擇的、按順序使用裝飾功能包裝物件了

優點:把類的裝飾功能從類中移除,可以簡化原有的類。有效把類的核心職責和裝飾功能區分開,而且可以去除相關類中重複的裝飾邏輯

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

namespace 裝飾模式
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

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

            d2.Operation();

            Console.Read();
        }
    }

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

    class ConcreteComponent : Component
    {
        public override void Operation()
        {
            Console.WriteLine("具體物件的操作");
        }
    }

    abstract class Decorator : Component
    {
        protected Component component;

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

        public override void Operation()
        {
            if (component != null)
            {
                component.Operation();
            }
        }
    }

    class ConcreteDecoratorA : Decorator
    {
        private string addedState;

        public override void Operation()
        {
            base.Operation();
            addedState = "New State";
            Console.WriteLine("具體裝飾物件A的操作");
        }
    }

    class ConcreteDecoratorB : Decorator
    {

        public override void Operation()
        {
            base.Operation();
            AddedBehavior();
            Console.WriteLine("具體裝飾物件B的操作");
        }

        private void AddedBehavior()
        {

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 裝飾模式
{
    class Person
    {
        public Person() { }
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
        public virtual void Show()
        {
            Console.WriteLine("裝扮的{0}", name);
        }
    }
    class Finery:Person
    {
        protected Person component;
        public void Decorate(Person component)
        {
            this.component = component;
        }
        public override void Show()
        {
            if (component != null)
                component.Show();
        }
    }
    class TShirts:Finery
    {
        public override void Show()
        {
            Console.Write("Tshirts ");
            base.Show();
        }
    }
    class BigTrouser:Finery
    {
        public override void Show()
        {
            Console.Write("BigTrouser ");
            base.Show();
        }
    }
    class Sneaks : Finery
    {
        public override void Show()
        {
            Console.Write("Sneaks ");
            base.Show();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person zyj = new Person("zyj");
            Sneaks snk = new Sneaks();
            BigTrouser bt = new BigTrouser();
            TShirts ts = new TShirts();
            snk.Decorate(zyj);
            bt.Decorate(snk);
            ts.Decorate(bt);
            ts.Show();
            Console.Read();
        }
    }
}