前言:【模式总览】——————————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的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 说说设计模式~组合模式(Composite)

    返回目录 何时能用到它? 组合模式又叫部分-整体模式,在树型结构中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦.对于今天这个 ...

  9. 大话设计模式--组合模式 Composite -- C++实现实例

    1. 组合模式: 将对象组合成树形结构以表示"部分--整体"的层次结构,组合模式使用户对单个对象和组合对象的使用具有一致性. 需求中是体现部分与整体层次的结构时,希望用户可以忽略组 ...

  10. 设计模式(七)组合模式Composite(结构型)

    设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...

随机推荐

  1. css 常用单位

    em: 相对于应用在当前元素的字体尺寸,1em 等于当前的字体尺寸,2em 等于当前字体尺寸的两倍,一般浏览器字体大小默认为16px,则2em == 32px: W3原文:font size of t ...

  2. Leetcode——338. 比特位计数

    题目描述:题目链接 对于求解一个十进制数转化为二进制时里面1的个数,可以先看一下概况: 十进制数 二进制数 1的个数 1 1    1 2 10 1 3 11   2 4 100 1 5 101 2 ...

  3. P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  4. php连接mysql...mysqli和mysql

    mysql_connect()这一系列函数已经不推荐使用了,不安全. <?php $con = mysql_connect('localhost','root','');// 选择连接数据库系统 ...

  5. Exp4 恶意代码分析 20155223

    Exp4 恶意代码分析 20155223 使用原生命令schtasks监视系统运行 在系统盘目录下建立脚本文件netstatlog.bat,包含以下命令: date /t >> c:\ne ...

  6. 20155226 《网络攻防》 Exp1 PC平台逆向破解(5)M

    20155226 <网络攻防> Exp1 PC平台逆向破解(5)M 实践目标 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串 该程序同时包含另一个代 ...

  7. 20155310 《网络对抗》Exp 8 Web基础

    20155310 <网络对抗>Exp 8 Web基础 基础问题回答 (1)什么是表单 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等 ...

  8. 2017-2018-4 20155317《网络对抗技术》EXP3 免杀原理与实践

    2017-2018-4 20155317<网络对抗技术>EXP3 免杀原理与实践 一.问题回答 (1)杀软是如何检测出恶意代码的?杀软是通过代码特征比对得出的,将检查的代码和自己的特征库的 ...

  9. Command and Query Responsibility分离模式

    CQRS模式,就是命令和查询责任分离模式. CQRS模式通过使用不同的接口来分离读取数据和更新数据的操作.CQRS模式可以最大化性能,扩展性以及安全性,还会为系统的持续演化提供更多的弹性,防止Upda ...

  10. POJ1159

    这竟然是IOI虽然是2000年的,但其实也改变不了它水题的本质 我写了两种方法,这里都讲一下吧 考虑记忆化搜索,用f[i][j]表示当区间的左端为i,右端为j时最少要添加多少字符,所以ans就为f[1 ...