【Unity3D与23种设计模式】组合模式(Composite)
前段时间在忙一个新项目
博客好久没有更新了
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)的更多相关文章
- 23种设计模式 - 数据结构(Composite - iterator - Chain of Responsibility)
其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 数据结构 Composite 动机(Motivation) 软件在某些情况下,客户代码过多依赖于对象容器复 ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 24种设计模式--组合模式【Composite Pattern】
大家在上学的时候应该都学过“数据结构”这门课程吧,还记得其中有一节叫“二叉树”吧,我们上学那会儿这一章节是必考内容,左子树,右子树,什么先序遍历后序遍历什么,重点就是二叉树的的遍历,我还记得当时老师就 ...
- 设计模式组合模式(Composite)精华
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...
- C#设计模式——组合模式(Composite Pattern)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
- 设计模式 -- 组合模式 (Composite Pattern)
定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...
- 设计模式-组合模式(Composite)
一.概念 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.模式动机 组合模式,通过设计一个抽像的组件类,使它既代表叶子对象,又代表组合对 ...
- 设计模式--组合模式Composite(结构型)
一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...
随机推荐
- Windows下如何硬盘安装Ubuntu
一般来说,折腾双系统是每一位程序猿都有过的经历,如何在windows下安装双系统ubuntu呢?今天来给大家介绍一下如何直接在windows硬盘安装ubuntu,而不需要使用U盘或者光盘,或外置硬盘. ...
- 《android开发艺术探索》读书笔记(十三)--综合技术
接上篇<android开发艺术探索>读书笔记(十二)--Bitmap的加载和Cache No1: 使用CrashHandler来获取应用的crash信息 No2: 在Android中单个d ...
- 1014. Waiting in Line (模拟)
n个窗口就有n个队列,模拟这n个队列就可以了.需要注意的是,一个人在选择排队窗口的时候,他会选择排队人数最少的窗口,如果存在多个窗口排队的人数相同,那么他会选择编码最小的窗口. Note that s ...
- codeforce-748A
简单判断一下就行. AC代码: #include<cstdio> int main(){ int n,m,k; while(scanf("%d%d%d",&n, ...
- 在SpringBoot中添加Logback日志处理
前言 SpringBoot项目中在官方文档中说明,默认已经依赖了一些日志框架.而其中推荐使用的就是Logback,所以这一次我将在我的模版中加入Logback日志的配置,说明一下,SpringBoot ...
- linux 云计算Openstack搭建
Openstack 由NASA和Reckspace合作研发并发起的项目,以Apache许可证为授权 云计算三大支柱模型 IaaS:基础架构即服务 提供服务器/虚拟主机/网络等设备资源 PaaS:平台即 ...
- R︱Rstudio 1.0版本尝鲜(R notebook、下载链接、sparkR、代码时间测试profile)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 2016年11月1日,RStudio 1.0版 ...
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
- jQuery.extend 函数使用详解
JQuery的extend扩展方法: Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解. 一.Jquery的扩展方 ...
- UniCode 下 CString 转 char* 的方法(转)
转自:http://blog.csdn.net/neverup_/article/details/5664733 今天进行文件操作时,将CString的GetBuffer()后直接倒到char数组后写 ...