日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

合成(Composite)模式

系統(tǒng) 1886 0

合成模式有時又叫做部分-整體模式(Part-Whole)。合成模式將對象組織到樹結構中,可以用來描述整體與部分的關系。合成模式可以使客戶端將單純元素與復合元素同等看待。

合成模式的實現(xiàn)根據(jù)所實現(xiàn)接口的區(qū)別分為兩種形式,分別稱為安全模式和透明模式。

透明方式

作為第一種選擇,在Component里面聲明所有的用來管理子類對象的方法,包括add()、remove(),以及getChild()方法。這樣做的好處是所有的構件類都有相同的接口。在客戶端看來,樹葉類對象與合成類對象的區(qū)別起碼在接口層次上消失了,客戶端可以同等同的對待所有的對象。這就是透明形式的合成模式。

這個選擇的缺點是不夠安全,因為樹葉類對象和合成類對象在本質上是有區(qū)別的。樹葉類對象不可能有下一個層次的對象,因此add()、remove()以及getChild()方法沒有意義,是在編譯時期不會出錯,而只會在運行時期才會出錯。

安全方式

第二種選擇是在Composite類里面聲明所有的用來管理子類對象的方法。這樣的做法是安全的做法,因為樹葉類型的對象根本就沒有管理子類對象的方法,因此,如果客戶端對樹葉類對象使用這些方法時,程序會在編譯時期出錯。

這個選擇的缺點是不夠透明,因為樹葉類和合成類將具有不同的接口。

這兩個形式各有優(yōu)缺點,需要根據(jù)軟件的具體情況做出取舍決定。

?

一,安全式結構

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

抽象構件(Component)角色: 這是一個抽象角色,它給參加組合的對象定義出公共的接口及其默認行為,可以用來管理所有的子對象。在安全式的合成模式里,構件角色并不是定義出管理子對象的方法,這一定義由樹枝構件對象給出。

樹葉構件(Leaf)角色: 樹葉對象是沒有下級子對象的對象,定義出參加組合的原始對象的行為。

樹枝構件(Composite)角色: 代表參加組合的有下級子對象的對象。樹枝對象給出所有的管理子對象的方法,如add()、remove()、getChild()等。

二,安全式示例代碼

    import java.util.ArrayList;

/**
 * 抽象構件
 * @author Salmon
 *
 */
public abstract class Component {
	protected String name;

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

	public abstract void display(int depth);
}

/**
 * 樹枝構件
 * @author Salmon
 *
 */
public class Composite extends Component {

	private ArrayList<Component> children = new ArrayList<Component>();

	public Composite(String name) {
		super(name);
	}

	public void add(Component component) {
		children.add(component);
	}

	public void remove(Component component) {
		children.remove(component);
	}

	public void display(int depth) {
		for (Component component : children) {
			component.display(depth + 2);
		}
	}
}

/**
 * 樹葉構件
 * @author Salmon
 *
 */
public class Leaf extends Component {
	public Leaf(String name) {
		super(name);
	}

	public void display(int depth) {
		System.out.println("");
	}
}

/**
 * 客戶端代碼
 * @author Salmon
 *
 */
public class Client {
	public void main() {
		Composite root = new Composite("root");
		root.add(new Leaf("Leaf A"));
		root.add(new Leaf("Leaf B"));
		Composite comp = new Composite("Composite X");

		comp.add(new Leaf("Leaf XA"));
		comp.add(new Leaf("Leaf XB"));
		root.add(comp);

		root.add(new Leaf("Leaf C"));

		// Add and remove a leaf
		Leaf l = new Leaf("Leaf D");
		root.add(l);
		root.remove(l);

		// Recursively display nodes
		root.display(1);
	}
}

  

?

三,透明式結構

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

抽象構件(Component)角色: 這是一個抽象角色,它給參加組合的對象規(guī)定一個接口,規(guī)范共有的接口及默認行為。

樹葉構件(Leaf)角色: 代表參加組合的樹葉對象,定義出參加組合的原始對象的行為。樹葉類會給出add()、remove()以及getChild()之類的用來管理子類對對象的方法的平庸實現(xiàn)。

樹枝構件(Composite)角色: 代表參加組合的有子對象的對象,定義出這樣的對象的行為。

四,透明式示例代碼

    import java.util.ArrayList;

/**
 * 抽象構件
 * @author Salmon
 *
 */
public abstract class Component {
	protected String name;

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

	public abstract void add(Component c);

	public abstract void remove(Component c);

	public abstract void display(int depth);
}

/**
 * 樹枝構件
 * @author Salmon
 *
 */
public class Composite extends Component {
	private ArrayList<Component> children = new ArrayList<Component>();

	public Composite(String name) {
		super(name);
	}

	public void add(Component component) {
		children.add(component);
	}

	public void remove(Component component) {
		children.remove(component);
	}

	public void display(int depth) {
		System.out.println("");
		;

		// Display each of the node's children
		for (Component component : children)
			component.display(depth + 2);
	}
}

/**
 * 樹葉構件
 * @author Salmon
 *
 */
class Leaf extends Component {
	public Leaf(String name) {
		super(name);
	}

	public void add(Component c) {
		System.out.println("Cannot add to a leaf");
	}

	public void remove(Component c) {
		System.out.println("Cannot remove from a leaf");
	}

	public void display(int depth) {
		System.out.println("");
	}
}

/**
 * 客戶端代碼
 * @author Salmon
 *
 */
public class Client {
	public static void main(String[] args) {
		// Create a tree structure
		Composite root = new Composite("root");
		root.add(new Leaf("Leaf A"));
		root.add(new Leaf("Leaf B"));
		Composite comp = new Composite("Composite X");
		comp.add(new Leaf("Leaf XA"));
		comp.add(new Leaf("Leaf XB"));
		root.add(comp);
		root.add(new Leaf("Leaf C"));
		Leaf l = new Leaf("Leaf D");
		root.add(l);
		root.remove(l);
		root.display(1);
	}
}
  

?

?

合成(Composite)模式


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 济宁市| 肇东市| 清涧县| 利川市| 获嘉县| 滁州市| 锡林浩特市| 伊宁市| 石城县| 天津市| 广东省| 陆良县| 大理市| 石台县| 高密市| 宜兰县| 华坪县| 瑞昌市| 道孚县| 德江县| 涿鹿县| 巴楚县| 临洮县| 长治县| 昌图县| 扎兰屯市| 黄大仙区| 航空| 永福县| 尉犁县| 娱乐| 无极县| 奈曼旗| 陆良县| 巴彦淖尔市| 嘉鱼县| 兴海县| 赤城县| 沙雅县| 综艺| 云梦县|