设计模式---合成模式(DesignPattern_Composite)

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

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

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


工程GitHub

COMPOSITE—Mary今天过生日。“我过生日,你要送我一件礼物。”“嗯,好吧,去商店,你自己挑。”“这件T恤挺漂亮,买,这条裙子好看,买,这个包也不错,买。”“喂,买了三件了呀,我只答应送一件礼物的哦。”“什么呀,T恤加裙子加包包,正好配成一套呀,小姐,麻烦你包起来。”“……”,MM都会用Composite模式了,你会了没有?

合成模式:合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。

using UnityEngine;
using System.Collections.Generic;

namespace DesignPattern_Composite
{
    // 含有合成模式的基类
    public abstract class IComponent
    {
        protected string m_Value;
        public abstract void Operation();   // 一般操作
                                            // 加入節點
        public virtual void Add(IComponent theComponent)
        {
            Debug.LogWarning("子類別沒實作");
        }
        // 移除节点
        public virtual void Remove(IComponent theComponent)
        {
            Debug.LogWarning("子類別沒實作");
        }
        // 取得子节点
        public virtual IComponent GetChild(int Index)
        {
            Debug.LogWarning("子類別沒實作");
            return null;
        }
    }

    // 单独节点
    public class Leaf : IComponent
    {
        public Leaf(string Value)
        {
            m_Value = Value;
        }
        public override void Operation()
        {
            Debug.Log("Leaf[" + m_Value + "]执行Operation()");
        }
    }

    // 含有合成结构的节点
    public class Composite : IComponent
    {
        List<IComponent> m_Childs = new List<IComponent>();

        public Composite(string Value)
        {
            m_Value = Value;
        }

        // 一般操作
        public override void Operation()
        {
            Debug.Log("Composite[" + m_Value + "]");
            foreach (IComponent theComponent in m_Childs)
                theComponent.Operation();
        }

        // 加入节点
        public override void Add(IComponent theComponent)
        {
            m_Childs.Add(theComponent);
        }

        // 移除节点
        public override void Remove(IComponent theComponent)
        {
            m_Childs.Remove(theComponent);
        }

        // 取得子节点
        public override IComponent GetChild(int Index)
        {
            return m_Childs[Index];
        }
    }

}

using UnityEngine;
using System.Collections;
using DesignPattern_Composite;

public class CompositeTest : MonoBehaviour
{

    void Start()
    {
        UnitTest();
    }

    // 
    void UnitTest()
    {

        // 根节点
        IComponent theRoot = new Composite("Root");
        // 加入两个单独节点
        theRoot.Add(new Leaf("Leaf1"));
        theRoot.Add(new Leaf("Leaf2"));

        // 子节点1
        IComponent theChild1 = new Composite("Child1");
        // 加入两个单独节点
        theChild1.Add(new Leaf("Child1.Leaf1"));
        theChild1.Add(new Leaf("Child1.Leaf2"));
        theRoot.Add(theChild1);

        // 子节点2
        // 加入3个单独节点
        IComponent theChild2 = new Composite("Child2");
        theChild2.Add(new Leaf("Child2.Leaf1"));
        theChild2.Add(new Leaf("Child2.Leaf2"));
        theChild2.Add(new Leaf("Child2.Leaf3"));
        theRoot.Add(theChild2);

        // 显示
        theRoot.Operation();
    }
}
相关文章
|
11天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
30天前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
3月前
|
设计模式 存储 算法
Java 设计模式最佳实践:三、行为模式
Java 设计模式最佳实践:三、行为模式
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——12中介模式
Golang设计模式——12中介模式
25 0
|
2月前
|
设计模式 前端开发 JavaScript
观察者模式 vs 发布-订阅模式:两种设计模式的对决!
欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚开始学习前端的读者们打造的。无论你是初学者还是有一些基础的开发者,我们都会在这里为你提供一个系统而又亲切的学习平台。我们以问答形式更新,为大家呈现精选的前端知识点和最佳实践。通过深入浅出的解释概念,并提供实际案例和练习,让你逐步建立起一个扎实的基础。无论是HTML、CSS、JavaScript还是最新的前端框架和工具,我们都将为你提供丰富的内容和实用技巧,帮助你更好地理解并运用前端开发中的各种技术。
|
7天前
|
设计模式 Java 数据库
小谈设计模式(2)—简单工厂模式
小谈设计模式(2)—简单工厂模式
|
7天前
|
设计模式 Java
小谈设计模式(9)—工厂方法模式
小谈设计模式(9)—工厂方法模式
|
1月前
|
设计模式 编译器
解析器模式--设计模式
解析器模式--设计模式
17 0
|
1月前
|
设计模式 算法
构建器模式--设计模式
构建器模式--设计模式
17 0
|
1月前
|
设计模式
【设计模式】中介模式
【设计模式】中介模式