黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

合成(Composite)模式

系統(tǒng) 2048 0

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

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

透明方式

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

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

安全方式

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

這個選擇的缺點(diǎn)是不夠透明,因?yàn)闃淙~類和合成類將具有不同的接口。

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

?

一,安全式結(jié)構(gòu)

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

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

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

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

二,安全式示例代碼

    import java.util.ArrayList;

/**
 * 抽象構(gòu)件
 * @author Salmon
 *
 */
public abstract class Component {
	protected String name;

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

	public abstract void display(int depth);
}

/**
 * 樹枝構(gòu)件
 * @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);
		}
	}
}

/**
 * 樹葉構(gòu)件
 * @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);
	}
}

  

?

三,透明式結(jié)構(gòu)

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

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

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

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

四,透明式示例代碼

    import java.util.ArrayList;

/**
 * 抽象構(gòu)件
 * @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);
}

/**
 * 樹枝構(gòu)件
 * @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);
	}
}

/**
 * 樹葉構(gòu)件
 * @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)模式


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論