前段时间在忙一个新项目

博客好久没有更新了

GoF中定义:

“将对象以树状结构组合,用以表现部分-全体的层次关系。组合模式让客户端在操作各个对象或组合时是一致的。”

是一致的意思就是:能够对根节点调用的操作,同样能够在叶节点上使用

“分层式管理结构”一般也称为“树状结构”

Unity中对于游戏对象的管理,也应用了树形结构的概念

让游戏对象之间可以被当成子对象或设置为父对象的方式来连接两个对象

public abstract class IComponent {
protected string m_Value; public abstract void Operation(); public virtual void Add(IComponent theComponent) {}
public virtual void Remove(IComponent theComponent) { }
public virtual IComponent GetChild(int index) {
return null;
}
}
//节点类
public class Composite : IComponent {
List<IComponent> m_Childs = new List<IComponent>(); public Composite(string Value) {
m_Value = Value;
} public override void Operation()
{
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];
}
}
//叶子类
public class Leaf : IComponent {
public Leaf(string Value) {
m_Value = Value;
} public override void Operation(){}
}
//测试类
public class TestComposite {
void UnitTest() {
IComponent theRoot = new Composite("Root"); theRoot.Add(new Leaf("Leaf1"));
theRoot.Add(new Leaf("Leaf2")); IComponent theChild1 = new Composite("Child1");
theChild1.Add(new Leaf("Child1.Leaf1"));
theChild1.Add(new Leaf("Child1.Leaf2"));
theRoot.Add(theChild1); 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();
}
}

组合模式优点:

界面与功能分离

工作切分更容易

界面更改不影响项目

缺点:

组件名称重复

组件更名不易

文章整理自书籍《设计模式与游戏完美开发》 菜升达 著

【Unity3D与23种设计模式】组合模式(Composite)的更多相关文章

  1. 23种设计模式 - 数据结构(Composite - iterator - Chain of Responsibility)

    其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 数据结构 Composite 动机(Motivation) 软件在某些情况下,客户代码过多依赖于对象容器复 ...

  2. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  3. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

  4. 24种设计模式--组合模式【Composite Pattern】

    大家在上学的时候应该都学过“数据结构”这门课程吧,还记得其中有一节叫“二叉树”吧,我们上学那会儿这一章节是必考内容,左子树,右子树,什么先序遍历后序遍历什么,重点就是二叉树的的遍历,我还记得当时老师就 ...

  5. 设计模式组合模式(Composite)精华

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...

  6. C#设计模式——组合模式(Composite Pattern)

    一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...

  7. 设计模式 -- 组合模式 (Composite Pattern)

    定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...

  8. 设计模式-组合模式(Composite)

    一.概念 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.模式动机 组合模式,通过设计一个抽像的组件类,使它既代表叶子对象,又代表组合对 ...

  9. 设计模式--组合模式Composite(结构型)

    一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...

随机推荐

  1. EmguCV中图像类型进行转换

    1.       Bitmap:类型不在 Emgucv命名空间中 2.       Image<TColor, TDepth> 3.       Mat: 4.       UMat: 高 ...

  2. hihoCoder1310 岛屿 (dfs)

    思路:首先dfs求得所有联通块,再搜索的同时把每个联通块的坐标都保存下来,然后把每个联通块处理一下–首先得到某个联通块的最小横坐标和纵坐标,然后让每个坐标去减去这个横坐标和纵坐标.相当于使得所有联通块 ...

  3. Spring Data JPA 入门Demo

    什么是JPA呢? 其实JPA可以说是一种规范,是java5.0之后提出来的用于持久化的一套规范:它不是任何一种ORM框架,在我看来,是现有ORM框架在这个规范下去实现持久层. 它的出现是为了简化现有的 ...

  4. string (KMP+期望DP)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description  给定一个由且仅由字符 'H' , 'T' 构成的字符串$S$. 给定一个最初为空的字符串 ...

  5. Socket 参数笔记

    //服务端@RunWith(JUnit4.class) public class ServerSocketTest { @Test public void testServer() throws IO ...

  6. spider RPC更新至2.0.0-RELEASE

    spider使用java语言开发,使用Spring作为IoC容器,采用TCP/IP协议,在此基础上,结合SaaS金融交易系统的特性进行针对性和重点设计,以更加灵活和高效的满足金融交易系统多租户.高可用 ...

  7. 【前端】Vue2全家桶案例《看漫画》之六、图片阅读页

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_6.html 项目github地址:https://github.com/shamoyuu/ ...

  8. 使用poi和jfreechart生成excel图表图片

    最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现. 除此之外,也有一些功能只需要生成对应的 ...

  9. jQuery中的$.ajax()方法

    jQuery中的$.ajax()方法 $.ajax({ type:"POST", url:"../page/user.action?userId=" + use ...

  10. Eviews 9.0新版本新功能——预测(Auto-ARIMA预测、VAR预测)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 9.预测功能 新增需要方法的预测功能:Auto ...