设计模式---合成模式(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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值