设计模式---装饰模式(DesignPattern_Decorator)

简介: 摘录自:设计模式与游戏完美开发十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity T...

摘录自:设计模式与游戏完美开发

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity Technologies——Kelvin Lo、信仁软件设计创办人—— 赖信仁、资深3D游戏美术——刘明恺 联合推荐全书采用了整合式的项目教学,即以一个游戏的范例来应用23种设计模式的实现贯穿全书,让读者学习到整个游戏开发的全过程和作者想要传承的经验,并以浅显易懂的比喻来解析难以理解的设计模式,让想深入了解此领域的读者更加容易上手。


工程GitHub

DECORATOR—Mary过完轮到Sarly过生日,还是不要叫她自己挑了,不然这个月伙食费肯定玩完,拿出我去年在华山顶上照的照片,在背面写上“最好的的礼物,就是爱你的Fita”,再到街上礼品店买了个像框,再找隔壁搞美术设计的Mike设计了一个漂亮的盒子装起来……,我们都是Decorator,最终都在修饰我这个人呀,怎么样,看懂了吗?

装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。

using UnityEngine;
using System.Collections;

namespace DesignPattern_Decorator
{
    // 动态增加组建的基类
    public abstract class Component
    {
        public abstract void Operator();
    }

    //实例组件
    public class ConcreteComponent : Component
    {
        public override void Operator()
        {
            Debug.Log("ConcreteComponent.Operator");
        }
    }

    // 持有指向Component的reference,須符合Component的介面
    public class Decorator : Component
    {
        Component m_Component;
        public Decorator(Component theComponent)
        {
            m_Component = theComponent;
        }

        public override void Operator()
        {
            m_Component.Operator();
        }
    }

    // 增加额外的功能A
    public class ConcreteDecoratorA : Decorator
    {
        Component m_Component;
        public ConcreteDecoratorA(Component theComponent) : base(theComponent)
        {
        }

        public override void Operator()
        {
            base.Operator();
            AddBehaivor();
        }

        private void AddBehaivor()
        {
            Debug.Log("ConcreteDecoratorA.AddBehaivor");
        }
    }

    // 增加额外的功能B
    public class ConcreteDecoratorB : Decorator
    {
        Component m_Component;
        public ConcreteDecoratorB(Component theComponent) : base(theComponent)
        {
        }

        public override void Operator()
        {
            base.Operator();
            AddBehaivor();
        }

        private void AddBehaivor()
        {
            Debug.Log("ConcreteDecoratorB.AddBehaivor");
        }
    }

}
using UnityEngine;
using System.Collections;
using DesignPattern_Decorator;
using DesignPattern_ShapeDecorator;

public class DecoratorTest : MonoBehaviour
{

    void Start()
    {
        //UnitTest_Shape();
        UnitTest();
    }

    // 
    void UnitTest()
    {

        // 物件
        ConcreteComponent theComponent = new ConcreteComponent();

        // 增加Decorator
        ConcreteDecoratorA theDecoratorA = new ConcreteDecoratorA(theComponent);
        theDecoratorA.Operator();

        ConcreteDecoratorB theDecoratorB = new ConcreteDecoratorB(theComponent);
        theDecoratorB.Operator();

        // 再增加一层
        ConcreteDecoratorB theDecoratorB2 = new ConcreteDecoratorB(theDecoratorA);
        theDecoratorB2.Operator();
    }
}
相关文章
|
29天前
|
设计模式 PHP
php设计模式--装饰模式(七)装饰模式完成文章编辑
php设计模式--装饰模式(七)装饰模式完成文章编辑
12 0
|
4月前
|
设计模式 中间件 PHP
设计模式 | 装饰模式
设计模式 | 装饰模式
15 0
|
6月前
|
设计模式
设计模式系列教程(12) - 装饰模式
设计模式系列教程(12) - 装饰模式
16 0
|
7月前
|
设计模式 算法 uml
结构型设计模式01-装饰模式
结构型设计模式01-装饰模式
16 0
|
7月前
|
设计模式
设计模式13 - 装饰模式【Decorator Pattern】
设计模式13 - 装饰模式【Decorator Pattern】
15 0
|
14天前
|
设计模式 Go
[设计模式 Go实现] 结构型~装饰模式
[设计模式 Go实现] 结构型~装饰模式
|
10月前
|
设计模式
设计模式——装饰模式
设计模式——装饰模式
43 0
|
5月前
|
设计模式 容器
设计模式之装饰模式(2)--有意思的想法
设计模式之装饰模式(2)--有意思的想法
|
5月前
|
设计模式
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
|
7月前
|
设计模式 Java 数据库连接
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能