設計模式總結-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.aygfsteel.com/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 飛熊 閱讀(270) 評論(0) 編輯 收藏 所屬分類: java設計模式