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

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

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 场景问题# 1.1 商品类别树## 考虑这样一个实际的应用:管理商品类别树。 在实现跟商品有关的应用系统的时候...
    七寸知架构阅读 6,075评论 10 59
  • 设计模式基本原则 开放-封闭原则(OCP),是说软件实体(类、模块、函数等等)应该可以拓展,但是不可修改。开-闭原...
    西山薄凉阅读 3,880评论 3 14
  • 好的软件设计是多用代码复用,但实际设计中却要遇到许多变化,而变化是复用的天敌。为了能够尽量减少变化所带来的设计复杂...
    CharlesW阅读 585评论 0 0
  • 创建型模式 1、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不...
    珈谊阅读 357评论 0 0
  • 前情回顾 第二天早上我们还没有起床的时候,赵婧带着礼物来了,一脸抱歉的样子,我和方雯婷安慰了她几句,说又不是她的错...
    狗一样的污姐阅读 281评论 0 0