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

創建型設計模式--抽象工廠

系統 2403 0

抽象工廠模式 (AbstractFactory)
???

??? 每一個模式都是針對一定問題的解決方案,工廠方法模式針對的是一個產品等級結構;而抽象工廠模式針對的是多個產品等級結果。


??? 產品族: 是指位于不同產品等級結構中,功能相關聯的產品組成的家族。一般是位于不同的等級結構中的相同位置上。
每一個產品等級結構中有多少個具體的產品,就有多少個產品族,也就會在工廠等級結構中發現多少個具體工廠。

??? 一般而言,有多少個產品等級結構,就會在工廠角色中發現多少個工廠方法。


??? 具體工廠(工廠類)中有工廠方法(工廠類中的方法)


創建型設計模式--抽象工廠

    // 兩種抽象產品(兩個結構等級):水果、蔬菜
interface Fruit {
}

interface Veggie {
}

// 四種具體產品:北方水果,熱帶水果,北方蔬菜,熱帶蔬菜
// Northern Fruit
class NorthernFruit implements Fruit {
	private String name;

	NorthernFruit(String name) {
	}

	String getName() {
		return name;
	}

	void setName(String name) {
		this.name = name;
	}
}

// TropicalFruit
class TropicalFruit implements Fruit {
	private String name;

	TropicalFruit(String name) {
	}

	String getName() {
		return name;
	}

	void setName(String name) {
		this.name = name;
	}
}

// NorthernVeggie
class NorthernVeggie implements Veggie {
	private String name;

	NorthernVeggie(String name) {
	}

	String getName() {
		return name;
	}

	void setName(String name) {
		this.name = name;
	}
}

// TropicalVeggie
class TropicalVeggie implements Veggie {
	private String name;

	TropicalVeggie(String name) {
	}

	String getName() {
		return name;
	}

	void setName(String name) {
		this.name = name;
	}
}

// 抽象工廠角色
public interface Gardener {
	public Fruit createFruit(String name);

	public Veggie createVeggie(String name);
}

// 具體工廠角色:北方工廠,熱帶角色
class NorthernGardener implements Gardener {
	Fruit createFruit(String name) {
		return new NorthernFruit(name);
	}

	Veggie createVeggie(String name) {
		return new NorthernVeggie(name);
	}
}

class TropicalGardener implements Gardener {
	Fruit createFruit(String name) {
		return new TropicalFruit(name);
	}

	Veggie createVeggie(String name) {
		return new TropicalVeggie(name);
	}
}

class TestApp {
	private void test() {
		Veggie tv, nv;
		Fruit tf, nf;
		TropicalGardener tg = new TropicalGardener();
		NorthernGardener ng = new NorthernGardener();
		tv = tg.createVeggie("熱帶菜葉");
		nv = ng.createVeggie("東北甜菜");
		tf = tg.createFruit("海南椰子");
		nf = ng.createFruit("雪梨");
	}

	public static void main(String args[]) {
		TestApp test = new TestApp();
		test.test();
	}
}
  

?

創建型設計模式--抽象工廠


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 泸定县| 东辽县| 神木县| 新巴尔虎左旗| 霸州市| 达州市| 乃东县| 阜城县| 保亭| 寿阳县| 临安市| 湛江市| 三亚市| 同心县| 池州市| 蓝山县| 西畴县| 朝阳区| 乌什县| 松江区| 临夏县| 雷州市| 和硕县| 旌德县| 合山市| 哈密市| 和龙市| 聂荣县| 巴林左旗| 安陆市| 高州市| 红安县| 武冈市| 筠连县| 温泉县| 北海市| 北川| 翁牛特旗| 台湾省| 平阳县| 高邮市|