【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(对象接口),定义 ...
随机推荐
- 转:【web前端开发】浏览器兼容性处理大全
解决思路: ①.写代码的时候遵循W3C标准,按照最新稳定版本的IE或WebKit内核浏览器进行编码 ②.遇到部分无法全面解决浏览器兼容的时候,采取CSS的hack手段进行针对性微调.简单的说,CSS ...
- WPF 照片墙的实现
主要参照了DevExpress的PhotoGallery实例的实现. 效果如下: 照片墙核心代码如下: PhotoGallery.xaml <local:CarouselDemoModule x ...
- Pandoc将markdown转换为word
markdown转换为word的指令 直接将markdown转换为word pandoc -f markdown -t docx ./test.md -o test.docx 关于markdown转为 ...
- Linux中文件夹的文件按照时间倒序或者升序排列
1.按照时间升序 命令:ls -lrt 详细解释: -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by modification time ...
- LinkedHashMap概述
1. LinkedHashMap概述: LinkedHashMap是HashMap的一个子类,它保留插入的顺序,如果需要输出的顺序和输入时的相同,那么就选用LinkedHashMap. LinkedH ...
- ORACLE ASMM与AMM的总结
概念对比介绍 相信有些人会对ORACLE当中的AMM(Automatic Memory Management)与ASMM(Automatic Shared Memory Management)有些 ...
- 实战DeviceIoControl 之一:通过API访问设备驱动程序
Q 在NT/2000/XP中,我想用VC编写应用程序访问硬件设备,如获取磁盘参数.读写绝对扇区数据.测试光驱实际速度等,该从哪里入手呢? A 在NT/2000/XP中,应用程序可以通过API函数Dev ...
- java线程池的原理及实现
1.线程池简介: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 ...
- [linux]device eth0 does not seem to be present, delaying initialization
mlite虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟硬盘使用之前的,启动系统后不能上网,通过ifconfig查看网卡没启动,遂启动网卡服务,但是出错,就是:device eth0 does ...
- HighCharts中的Ajax请求的2D折线图
HighCharts中的Ajax请求的2D折线图 设计源码: <!DOCTYPE html> <html> <head> <meta charset=&quo ...