天天看點

設計模式【裝飾模式Decorator Pattern】 裝飾器模式

裝飾器模式

裝飾器模式(Decorator Pattern)允許向一個現有的對象添加新的功能,同時又不改變其結構。這種類型的設計模式屬于結構型模式,它是作為現有的類的一個包裝。

這種模式建立了一個裝飾類,用來包裝原有的類,并在保持類方法簽名完整性的前提下,提供了額外的功能。

我們通過下面的執行個體來示範裝飾器模式的用法。其中,我們将把一個形狀裝飾上不同的顔色,同時又不改變形狀類。

介紹

意圖:動态地給一個對象添加一些額外的職責。就增加功能來說,裝飾器模式相比生成子類更為靈活。

主要解決:一般的,我們為了擴充一個類經常使用繼承方式實作,由于繼承為類引入靜态特征,并且随着擴充功能的增多,子類會很膨脹。

何時使用:在不想增加很多子類的情況下擴充類。

如何解決:将具體功能職責劃分,同時繼承裝飾者模式。

關鍵代碼: 1、Component 類充當抽象角色,不應該具體實作。 2、修飾類引用和繼承 Component 類,具體擴充類重寫父類方法。

應用執行個體: 1、孫悟空有 72 變,當他變成"廟宇"後,他的根本還是一隻猴子,但是他又有了廟宇的功能。 2、不論一幅畫有沒有畫框都可以挂在牆上,但是通常都是有畫框的,并且實際上是畫框被挂在牆上。在挂在牆上之前,畫可以被蒙上玻璃,裝到框子裡;這時畫、玻璃和畫框形成了一個物體。

優點:裝飾類和被裝飾類可以獨立發展,不會互相耦合,裝飾模式是繼承的一個替代模式,裝飾模式可以動态擴充一個實作類的功能。

缺點:多層裝飾比較複雜。

使用場景: 1、擴充一個類的功能。 2、動态增加功能,動态撤銷。

注意事項:可代替繼承。

實作

我們将建立一個 Shape 接口和實作了 Shape 接口的實體類。然後我們建立一個實作了 Shape 接口的抽象裝飾類 ShapeDecorator,并把 Shape 對象作為它的執行個體變量。

RedShapeDecorator 是實作了 ShapeDecorator 的實體類。

DecoratorPatternDemo,我們的示範類使用 RedShapeDecorator 來裝飾 Shape 對象。

設計模式【裝飾模式Decorator Pattern】 裝飾器模式

步驟 1

建立一個接口。

Shape.cs

namespace Decorator_Pattern
{
    interface Shape
    {
        void draw();
    }
}
           

步驟 2

建立實作接口的實體類。

Rectangle.cs

using System;
namespace Decorator_Pattern
{
    class Rectangle : Shape
    {
        public void draw()
        {
            Console.WriteLine("Shape: Rectangle");
        }
    }
}
           

Circle.cs

using System;
namespace Decorator_Pattern
{
    class Circle : Shape
    {
        public void draw()
        {
            Console.WriteLine("Shape: Circle");
        }
    }
}
           

步驟 3

建立實作了 Shape 接口的抽象裝飾類。

ShapeDecorator.cs

namespace Decorator_Pattern
{
    //裝飾類
    abstract class ShapeDecorator : Shape
    {
        protected Shape decoratedShape;
        public ShapeDecorator(Shape decoratedShape)
        {
            this.decoratedShape = decoratedShape;
        }

        public abstract void draw();
     
    }
}
           

步驟 4

建立擴充了 ShapeDecorator 類的實體裝飾類。

RedShapeDecorator.cs

using System;
namespace Decorator_Pattern
{
    class RedShapeDecorator : ShapeDecorator
    {
        public RedShapeDecorator(Shape decoratedShape)
            : base(decoratedShape)
        {

        }

        public override void draw()
        {
            decoratedShape.draw();
            SetRedBorder(decoratedShape);  //給Shape拓展紅框的功能
        }

        void SetRedBorder(Shape decoratedShape)
        {
            Console.WriteLine("Border Color: Red");
        }
    }
}
           

步驟 5

使用 RedShapeDecorator 來裝飾 Shape 對象。

DecoratorPatternDemo.cs

using System;
namespace Decorator_Pattern
{
    class DecoratorPatternDemo 
    {
        static void Main(string[] args)
        {
            Shape circle = new Circle();
            Shape redCircle = new RedShapeDecorator(new Circle());
            Shape redRectangle = new RedShapeDecorator(new Rectangle());

            Console.WriteLine("\nCircle with normal border");
            circle.draw();

            Console.WriteLine("\nCircle of red border");
            redCircle.draw();

            Console.WriteLine("\nRectangle of red border");
            redRectangle.draw();

            Console.Read();
        }
    }
}
           

步驟 6

驗證輸出。

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red
           

拓展例子

裝飾模式有很多應用,例如裝飾模式為已有類動态附加額外的功能就像LOL、王者榮耀等類Dota遊戲中,英雄更新一樣。每次英雄更新都會附加一個額外技能點學習技能。具體的英雄就是ConcreteComponent,技能欄就是裝飾器Decorator,每個技能就是ConcreteDecorator;

using System;
namespace LearnSkill
{
    //Component英雄接口
    public interface Hero
    {
        //學習技能
        void learnSkills();
    }

    //ConcreteComponent 具體英雄坦克職業
    public class Tank : Hero
    {
        string name;

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

        public void learnSkills()
        {
            Console.WriteLine(name + "學習了以上技能!");
        }
    }

    //Decorator 技能欄
    public class Skills : Hero
    {
        Hero hero;

        public Skills(Hero hero)
        {
            this.hero = hero;
        }

        public virtual void learnSkills()
        {
            if (hero != null)
                hero.learnSkills();
        }
    }

    //ConcretDecrator 技能: Q
    public class Skill_Q : Skills
    {
        string skillName;

        public Skill_Q(Hero hero, string skillName)
            : base(hero)
        {
            this.skillName = skillName;
        }

        public override void learnSkills()
        {
            Console.WriteLine("學習了技能Q:" + skillName);
            base.learnSkills();
        }
    }

    public class Skill_W : Skills
    {
        string skillName;

        public Skill_W(Hero hero, string skillName)
            : base(hero)
        {
            this.skillName = skillName;
        }

        public override void learnSkills()
        {
            Console.WriteLine("學習了技能W:" + skillName);
            base.learnSkills();
        }
    }

    //ConcretDecorator 技能:E
    public class Skill_E : Skills
    {
        string skillName;

        public Skill_E(Hero hero, string skillName)
            :base(hero)
        {
            this.skillName = skillName;
        }

        public override void learnSkills()
        {
            Console.WriteLine("學習了技能E:" + skillName);
            base.learnSkills();
        }
    }

    //ConcretDecorator 技能:R
    public class Skill_R : Skills
    {
        string skillName;

        public Skill_R(Hero hero, string skillName)
            : base(hero)
        {
            this.skillName = skillName;
        }

        public override void learnSkills()
        {
            Console.WriteLine("學習了技能R:" + skillName);
            base.learnSkills();
        }
    }

    class Player
    {
        static void Main(string[] args)
        {
            Hero hero = new Tank("亞瑟");
            Skills skills = new Skills(hero);
            Skills r = new Skill_R(skills, "聖劍裁決");
            Skills e = new Skill_E(r, "回旋打擊");
            Skills w = new Skill_W(e, "誓約之盾");
            Skills q = new Skill_Q(w, "聖光守護");
            //學習技能
            q.learnSkills();
            Console.Read();
        }
    }
}
           

輸出:

學習了技能Q: 聖光守護
學習了技能W: 誓約之盾
學習了技能E: 回旋打擊
學習了技能R: 聖劍裁決
亞瑟學習了以上技能!
           

繼續閱讀