`
liuxinglanyue
  • 浏览: 548946 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java设计模式之组合模式

阅读更多

COMPOSITE (Object Structural) 
Purpose 
Facilitates the creation of object hierarchies where each object 
can be treated independently or as a set of nested objects 
through the same interface. 
Use When 
1 Hierarchical representations of objects are needed.. 
2 Objects and compositions of objects should be treated uniformly. 
Example 
Sometimes the information displayed in a shopping cart is the 
product of a single item while other times it is an aggregation 
of multiple items. By implementing items as composites we can 
treat the aggregates and the items in the same way, allowing us 
to simply iterate over the tree and invoke functionality on each 
item. By calling the getCost() method on any given node we 
would get the cost of that item plus the cost of all child items, 
allowing items to be uniformly treated whether they were single 
items or groups of items. 

Java代码 
  1. package javaPattern.composite;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5.   
  6. interface Component{  
  7.     public void operation();  
  8. }  
  9. public class Composite implements Component{  
  10.     private ArrayList<Component> al = new ArrayList<Component>();  
  11.     @Override  
  12.     public void operation() {  
  13.         System.out.println("Composite's operation...");  
  14.           
  15.     }  
  16.     public void add(Component c){  
  17.         al.add(c);  
  18.     }  
  19.     public void remove(Component c){  
  20.         al.remove(c);  
  21.     }  
  22.     public Component getChild(int index){  
  23.         return al.get(index);  
  24.     }  
  25. }  
  26. class Leaf implements Component{  
  27.   
  28.     @Override  
  29.     public void operation() {  
  30.         System.out.println("leaf's operation..");  
  31.           
  32.     }  
  33.       
  34. }  
  35. class Client{  
  36.     public static void main(String[] args) {  
  37.         Composite composite = new Composite();  
  38.         Component component1 = new Leaf();  
  39.         Component component2 = new Leaf();  
  40.         composite.add(component1);  
  41.         composite.add(component2);  
  42.     }  
  43. }  

分享到:
评论

相关推荐

    java设计模式之组合模式.docx

    把部分和整体的关系用树形结构来表示,从而使客户端可以使用统一的方式处理部分对象和整体对象。就像是图书管理员对图书馆的书籍进行整理,既可以对每一部分的书籍进行整理,也可以一次性将它们收集起来然后进行整理...

    java设计模式之组合模式(Composite)

    主要为大家详细介绍了java设计模式之组合模式Composite,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    JAVA设计模式之组合模式原理与用法详解

    主要介绍了JAVA设计模式之组合模式,简单说明了组合模式的原理,并结合实例分析了java组合模式的具体用法,需要的朋友可以参考下

    Java设计模式之组合模式(Composite模式)介绍

    主要介绍了Java设计模式之组合模式(Composite模式)介绍,Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性,需要的朋友可以参考下

    JAVA设计模式chm文档

    设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of ...

    JAVA设计模式之结构模式

    这是JAVA设计模式中属于结构模式的部分,包括Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade (外观模式)的源代码。其中有些模式中...

    java 23种设计模式.zip

    设计模式主要分为三大类: 1.创建型模式:工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式。 2.结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。 4.行为型模式:...

    java设计模式【之】组合模式【源码】【场景:遍历目录树】

    java设计模式【之】组合模式【源码】【场景:遍历目录树】 * 组合模式 * 将对象组合成树结构,表示 “部分与整体” 的关系 * 要求 部分与整体,具备相同的父类 * * 代码实现 * 输出当前目录下,全部目录层级...

    设计模式--组合模式java例子

    设计模式--组合模式java例子

    java设计模式

    目录: 前 言 第一部分 大旗不挥,谁敢冲锋——热身篇 第1章 单一职责原则 1.1 我是“牛”类,我可以担任多职吗 1.2 绝杀技,打破你的传统思维 1.3 我单纯,所以我快乐 1.4 最佳实践 ...附录:23个设计模式

    java常用设计模式-组合模式

    java常用设计模式-组合模式

    Java 23种设计模式12组合模式.pdf

    Java 23种设计模式12组合模式.pdf

    33种JAVA设计模式DEMO

    组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3 行为型模式 这些设计模式特别关注对象之间的通信。 责任...

    java设计模式示例

    java设计模式示例 创建型模式(5种):工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式。 结构型模式(7种):适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模式,享元模式。 行为型...

    计算机后端-Java-图解java设计模式080 组合模式(4).avi

    计算机后端-Java-图解java设计模式080 组合模式(4).avi

    计算机后端-Java-图解java设计模式078 组合模式(2).avi

    计算机后端-Java-图解java设计模式078 组合模式(2).avi

    计算机后端-Java-图解java设计模式077 组合模式(1).avi

    计算机后端-Java-图解java设计模式077 组合模式(1).avi

    Java23种设计模式可直接运行Demo

    设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。 ...

    java常用23中设计模式

    总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元...

Global site tag (gtag.js) - Google Analytics