23种设计模式 第二部分 结构模式(6)组合模式
来源:互联网 发布:ubuntu 虚拟终端 编辑:程序博客网 时间:2024/05/29 16:56
理解
组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便。
实现
import java.util.Enumeration;import java.util.Vector;public class TreeNode {private String name;private TreeNode parent;private Vector<TreeNode> children = new Vector<TreeNode>();public TreeNode(String name){this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public TreeNode getParent() {return parent;}public void setParent(TreeNode parent) {this.parent = parent;}public void add(TreeNode node){children.add(node);}public void remove(TreeNode node){children.remove(node);}public Enumeration<TreeNode> getChildren(){return children.elements();}}
public class Tree {TreeNode root = null;public Tree(String name){root = new TreeNode(name);}public static void main(String[] args){Tree tree = new Tree("A");TreeNode nodeB = new TreeNode("B");TreeNode nodeC = new TreeNode("C");nodeB.add(nodeC);tree.root.add(nodeB);System.out.println("build the tree");}}
使用场景:将多个对象组合在一起进行操作,常用于表示树形结构中,例如二叉树,数等。
维基百科
The composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects is to be treated in the same wayas a single instance of an object.
=== Java ===<source lang=java>/** "Component" */interface Graphic { //Prints the graphic. public void print();}/** "Composite" */import java.util.List;import java.util.ArrayList;class CompositeGraphic implements Graphic { //Collection of child graphics. private List<Graphic> childGraphics = new ArrayList<Graphic>(); //Prints the graphic. public void print() { for (Graphic graphic : childGraphics) { graphic.print(); } } //Adds the graphic to the composition. public void add(Graphic graphic) { childGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { childGraphics.remove(graphic); }}/** "Leaf" */class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); }}/** Client */public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); }}</source><span style="color:#3333ff;"></span>
0 0
- 23种设计模式 第二部分 结构模式(6)组合模式
- 23种设计模式 第二部分 结构模式(1) 适配器模式
- 23种设计模式 第二部分 结构模式(2) 装饰器模式
- 23种设计模式 第二部分 结构模式(3) 代理模式
- 23种设计模式 第二部分 结构模式(4) 外观模式
- 23种设计模式 第二部分 结构模式(5) 桥接模式
- 23种设计模式 第二部分 结构模式(7)享元模式
- 设计模式->结构型模式->组合模式(部分-整体模式)
- javascript设计模式_第二部分_组合模式
- 组合模式(结构型设计模式)
- 23种设计模式3--结构型模式(适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享元模式)
- 23种设计模式--结构型模式(适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享元模式)
- 【结构型模式】组合模式(Composite)之23种java设计模式
- 设计模式--结构模式--组合模式--Java
- 设计模式 - 结构型模式 - 组合模式
- C++设计模式 对象结构模式-----组合模式(COMPOSITE)
- 设计模式(二):结构行模式之组合模式
- Java开发中的23个设计模式--第二部分:结构型模式
- 集群环境中使用 EhCache 缓存系统
- python环境导出
- lightoj 1003
- 网络编程
- [LeetCode] 101. Symmetric Tree
- 23种设计模式 第二部分 结构模式(6)组合模式
- html 中 TabIndex 属性 的含义
- linux下文件统计
- JVM性能调优
- Aizu 2677 Breadth-First Search by Foxpower(LCA + BFS)
- iPhone 6 屏幕揭秘
- 局部类
- Ognl标签常用例子 只能在Struts2中使用
- java 拦截器、过滤器、监听器