设计模式总结-Composite模式

一、Composite模式的定义

将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性.

二、Composite模式的特点

Composite比较容易理解,想到Composite就应该想到树形结构图。组合体内这些对象都有共同接口,当组合体一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。可以用牵一动百来形容。

所以Composite模式使用到Iterator模式,和Chain of Responsibility模式类似。

三、Composite好处

1.使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关系自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。
2.更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。

四、如何使用Composite

首先定义一个接口或抽象类,这是设计模式通用方式了,其他设计模式对接口内部定义限制不多,Composite却有个规定,那就是要在接口内部定义一个用于访问和管理Composite组合体的对象们(或称部件Component).

    ---- 以上内容摘自Jdon网站的《Gof 23种设计模式》系列文章

五、Composite模式的分析

·Composite模式的结构:
基类/接口(构件抽象)
    |
    |--- 原子构件(extends 基类 / implements 接口)
    |
    |--- 组合构件(extends 基类 / implements 接口)
               |--- 原子构件1
               |--- 原子构件2
               |--- 组合构件3
               |--- 原子构件3-1
                        |--- 原子构件3-2

·Composite模式的特点:
   ·Composite模式一般都有一个抽象类或接口来表示最基本的构件。
   ·Composite模式一般都由两类对象构成:表示单个元素的对象(Primitive)和表示多个元素组合的对象(Composite)
   ·Composite模式下Primitive和Composite对象都继承或实现上层接口或父类
   ·Composite模式下每个构件都含有三个基础方法:add(构件)、remove(构件)、iterator()
   ·Composite对象含有一个用来保存其下所有基础元素的的集合,例如:Vector,ArrayList,HashMap
   ·Composite对象的方法被调用时一般都会引起其下所有基础元素相同方法的调用,即递归调用。

·Composite模式中Primitive对象和Composite对象的方法区别:
   ·add(构件):如果是基础对象,则此方法直接返回false,如果是组合对象,则先添加新构件然后返回true
   ·remove(构件):如果是基础对象,则此方法直接返回false,如果是组合对象,则先删除构件然后返回true
   ·iterator():如果是基础对象,则此方法直接返回null,如果是组合对象,则返回一个包含所有对象的集合

·客户端调用Composite模式的代码示例:
   ·创建一个原子构件对象
   ·创建一个组合构件对象
   ·调用组合构件对象的add/remove方法添加/删除对象
   ·调用组合够对象的iteratore方法迭代显示对象
本文摘自:http://www.blogjava.net/pengpenglin/archive/2008/01/21/176675.html
续:

一个二叉树的例子:
1.Component 是抽象组件
Tree 和Leaf 继承Component

private String name; //树或叶子的名称
addChild(Component leftChild,Component rightChild);
//给一个树上加上一个左孩子,一个右孩子
getName(){return name;}
getTreeInfo(){} //得到树或叶子的详细信息
getLength(); //得到树的高度

2.Tree 二叉树,一个左孩子,一个右孩子

3.Leaf 是叶子节点
4.Test 是测试节点

/** Component.java **************/
package binarytree;

public abstract class Component {
 private String name;

 public abstract Component addChild(Component leftChild, Component rightChild);

 public String getName() {
  return name;
 }

 public void getTreeInfo() {
 }

 public abstract int getLength();
}


/** Leaf.java **************/
package binarytree;

public class Leaf extends Component {
 private String name;

 private Component leaf = null;

 public Leaf(String name) {
  this.name = name;
 }

 public Component addChild(Component leftChild, Component rightChild) {
  return this;
 }

 public String getName() {
  return name;
 }

 public int getLength() {
  return 1;
 }

 public static void main(String[] args) {
 }
}

/** Tree.java **************/
package binarytree;

public class Tree extends Component {
 private String name;

 private Component leftChild;

 private Component rightChild;

 public Tree(String name, Component leftChild, Component rightChild) {
  this.name = name;
  this.leftChild = leftChild;
  this.rightChild = rightChild;
 }

 public Tree(String name) {
  this.name = name;
  this.leftChild = null;
  this.rightChild = null;
 }

 public Component addChild(Component leftChild, Component rightChild) {
  this.leftChild = leftChild;
  this.rightChild = rightChild;
  return this;
 }

 public String getName() {
  return name;
 }

 public void getTreeInfo()
 // 得到树或叶子的详细信息
 // 先打印自己的名字,再遍例左孩子,再遍例右孩子
 // 如果左孩子或右孩子是树,递归调用
 {
  System.out.println(" this trees name is " + getName());
  if (this.leftChild instanceof Leaf) {
   System.out.println(getName() + "s left child is "
     + this.leftChild.getName() + ",it is a Leaf");
  }
  if (this.leftChild instanceof Tree) {
   System.out.println(getName() + "s left child is "
     + this.leftChild.getName() + ",it is a Tree");
   this.leftChild.getTreeInfo();
  }
  if (this.leftChild == null) {
   System.out.println(getName() + "s left child is a null");
  }
  if (this.rightChild instanceof Leaf) {
   System.out.println(getName() + "s right child is "
     + this.rightChild.getName() + ",it is a Leaf");
  }
  if (this.rightChild instanceof Tree) {
   System.out.println(getName() + "s right child is "
     + this.rightChild.getName() + ",it is a Tree");
   this.rightChild.getTreeInfo();
  }
  if (this.rightChild == null) {
   System.out.println(getName() + "s right child is a null");
  }
  // System.out.println(getName()+"s 高度 是 "+getLength());
 }

 public int getLength() {
  // 比较左孩子或右孩子的高度,谁大,+1 返回
  // 空孩子的处理
  if (this.leftChild == null) {
   if (this.rightChild == null)
    return 1;
   else
    return this.rightChild.getLength() + 1;
  } else {
   if (this.rightChild == null) {
    return this.leftChild.getLength() + 1;
   } else {
    if ((this.leftChild.getLength()) >= (this.rightChild
      .getLength()))
     return this.leftChild.getLength() + 1;
    else
     return this.rightChild.getLength() + 1;
   }
  }
 }

 public static void main(String[] args) {
 }
}

/** Test.java 测试类 **************/
package binarytree;

public class Test {

 public Test() {
 }

 public static void main(String[] args) {
  Component tree = new Tree("luopeng");
  Component left_child = new Leaf("luopeng1");
  Component right_child = new Leaf("luopeng2");
  tree = tree.addChild(left_child, right_child);
  // tree=tree.addRightChild(right_child);
  tree.getTreeInfo();
  Component tree1 = new Tree("luopeng2");
  tree1.addChild(tree, left_child);
  tree1.getTreeInfo();
  Component tree2 = new Tree("luopeng3");
  tree2.addChild(tree, null);
  tree2.getTreeInfo();
  Component tree4 = new Tree("luopeng4");
  tree4.addChild(null, tree);
  tree4.getTreeInfo();
  System.out.println(tree4.getName() + "的高度是 " + tree4.getLength());
 }
}

posted on 2010-01-18 22:04 飞熊 阅读(245) 评论(0)  编辑  收藏 所属分类: java设计模式


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜