【设计模式】—— 组合模式Composite
前言:【模式总览】——————————by xingoo
模式意图
使对象组合成树形的结构。使用户对单个对象和组合对象的使用具有一致性。
应用场景
1 表示对象的 部分-整体 层次结构
2 忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象。
模式结构
【安全的组合模式】
这种组合模式,叶子节点,也就是单个对象不具有对象的控制功能。仅仅有简单的业务操作。

package com.xingoo.composite.safe; import java.util.ArrayList;
import java.util.List; interface Component{
Composite getCmposite();
void sampleComposite();
} class Leaf implements Component{ public Composite getCmposite() {
return null;
} public void sampleComposite() {
System.out.println("Leaf operation");
} } class Composite implements Component{ private List<Component> list = new ArrayList(); public void add(Component component){
list.add(component);
} public void remove(Component component){
list.remove(component);
} public Composite getCmposite() {
return this;
} public void sampleComposite() {
System.out.println("Composite operation");
for(Component com : list){
com.sampleComposite();
}
} }
public class Client {
public static void main(String[] args) {
Component leaf1 = new Leaf();
Component leaf2 = new Leaf();
Component composite = new Composite();
composite.getCmposite().add(leaf1);
composite.getCmposite().add(leaf2);
composite.getCmposite().sampleComposite();
}
}
执行结果
Composite operation
Leaf operation
Leaf operation
【透明的组合模式】
这种组合模式,叶子节点与组合对象具有相同的方法,外表看来毫无差异。不过叶子节点的处理方法默认为空。忽略叶子节点,与组合对象的差异性。

package com.xingoo.composite.transparent; import java.util.ArrayList;
import java.util.List; interface Component{
public void SampleOperation();
public void add(Component component);
public void remove(Component component);
public Component getComponent();
} class Leaf implements Component{
public void SampleOperation() {
System.out.println("leaf operation!");
} public void add(Component component) { } public void remove(Component component) { } public Component getComponent(){
return this;
}
} class Composite implements Component{ private List<Component> list = new ArrayList(); public void SampleOperation() {
System.out.println("composite operation!");
for(Component com : list){
com.getComponent().SampleOperation();
}
} public void add(Component component) {
list.add(component);
} public void remove(Component component) {
list.remove(component);
} public Component getComponent(){
return this;
}
}
public class Client {
public static void main(String[] args) {
Component leaf1 = new Leaf();
Component leaf2 = new Leaf();
Component leaf3 = new Leaf();
Component composite1 = new Composite();
Component composite = new Composite(); composite1.add(leaf3); composite.getComponent().add(leaf1);
composite.getComponent().add(leaf2);
composite.getComponent().add(composite1); composite.getComponent().SampleOperation();
}
}
本例中的结构层次

执行结果
composite operation!
leaf operation!
leaf operation!
composite operation!
leaf operation!
【设计模式】—— 组合模式Composite的更多相关文章
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...
- 设计模式组合模式(Composite)精华
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...
- 设计模式 -- 组合模式 (Composite Pattern)
定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...
- 设计模式--组合模式Composite(结构型)
一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...
- C#设计模式——组合模式(Composite Pattern)
一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...
- 设计模式-组合模式(Composite)
一.概念 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.模式动机 组合模式,通过设计一个抽像的组件类,使它既代表叶子对象,又代表组合对 ...
- 说说设计模式~组合模式(Composite)
返回目录 何时能用到它? 组合模式又叫部分-整体模式,在树型结构中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦.对于今天这个 ...
- 大话设计模式--组合模式 Composite -- C++实现实例
1. 组合模式: 将对象组合成树形结构以表示"部分--整体"的层次结构,组合模式使用户对单个对象和组合对象的使用具有一致性. 需求中是体现部分与整体层次的结构时,希望用户可以忽略组 ...
- 设计模式(七)组合模式Composite(结构型)
设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...
随机推荐
- DB2创建function(二)
DB2创建function(一),介绍将function内容作为字段值,或做为一个where条件的情况. DB2创建function(二),介绍返回的内容为一个集合的情况.调用结果集的示例如下: se ...
- SQL_sql语言的学习
关系数据库SQL sql基本功能 SQLde 基本概念 主要知识点 1.外模式包含若干视图和部分基本表 2.模式包含若干基本表 3.内模式包含若干存储文件 4操作对象 基本表:本身独立存在的表,一个关 ...
- 20155238 《JAVA程序设计》实验三(敏捷开发与XP实践)实验报告
实验内容 敏捷开发与XP实践 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实 ...
- 20155318 《网络攻防》Exp2 后门原理与实践
20155318 <网络攻防>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 下载软件前要勾选一些用户协议,其中部分就存在后门进入系统的安全隐患. ...
- controlfile作为RMAN的repository时,对 keep time 的测试
4月2日,首先查看系统状况: SQL> show parameter control NAME TYPE VALUE ...
- libgdx学习记录6——动作Action
libgdx中的Action类能够有效的帮助我们实现位移.旋转.缩放.淡入淡出等效果,对游戏的设计很有用. Action是一个抽象类,本身不可以实例化.一般使用的它的继承类,常用的有 MoveToAc ...
- 从零系列--开发npm包(一)
一.目的 主要是纪录和回顾自己开发的一些步骤以及遇到的一些问题和解决方案 二.准备工作 1.IDE 选择 VS Code 2.安装node 环境 (https://nodejs.org/zh-cn/) ...
- Jenkins报表 代码 指标分析
Jenkins报表 这表现在前面的章节中,也有可用最简单的一种是适用于 JUnit 测试报告的许多报表插件. 在生成后动作进行任何工作,你可以定义要创建的报告. 该构建已经完成,测试结果选项将可进一步 ...
- 1. Python3 环境搭建
Python3 环境搭建 开门见山,其他关于Python发展史.语言类型.优缺点等等 可以自己去百度百度,这里就不多说了.其实基本想要学这门语言的时候,你已经了解差不多了!!! Python的运行环境 ...
- JWT总结
Json web token (JWT) 什么是JWT? Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该toke ...