java设计模式2--抽象工厂模式(Abstract Factory)
本文地址:http://www.cnblogs.com/archimedes/p/java-abstract-factory-pattern.html,轉(zhuǎn)載請(qǐng)注明源地址。
抽象工廠模式(別名:配套)
提供一個(gè)創(chuàng)建一系列(相互依賴(lài))對(duì)象的接口,而無(wú)需指定它們具體的類(lèi)。
概述
當(dāng)系統(tǒng)準(zhǔn)備為用戶提供一系列相關(guān)的對(duì)象,又不想讓用戶代碼和創(chuàng)建這些對(duì)象的類(lèi)形成耦合時(shí),就可以使用抽象工廠方法模式來(lái)設(shè)計(jì)系統(tǒng)。抽象工廠模式的關(guān)鍵是在一個(gè)抽象類(lèi)或接口中定義若干個(gè)抽象方法,這些抽象方法分別返回某個(gè)類(lèi)的實(shí)例,該抽象類(lèi)或接口讓其子類(lèi)或?qū)崿F(xiàn)該接口的類(lèi)重寫(xiě)這些抽象方法,為用戶提供一系列相關(guān)的對(duì)象。
適用性
1.一個(gè)系統(tǒng)要獨(dú)立于它的產(chǎn)品的創(chuàng)建、組合和表示時(shí)。
2.一個(gè)系統(tǒng)要由多個(gè)產(chǎn)品系列中的一個(gè)來(lái)配置時(shí)。
3.當(dāng)你要強(qiáng)調(diào)一系列相關(guān)的產(chǎn)品對(duì)象的設(shè)計(jì)以便進(jìn)行聯(lián)合使用時(shí)。
4.當(dāng)你提供一個(gè)產(chǎn)品類(lèi)庫(kù),而只想顯示它們的接口而不是實(shí)現(xiàn)時(shí)。
參與者
1.AbstractFactory 聲明一個(gè)創(chuàng)建抽象產(chǎn)品對(duì)象的操作接口。
2.ConcreteFactory 實(shí)現(xiàn)創(chuàng)建具體產(chǎn)品對(duì)象的操作。
3.AbstractProduct 為一類(lèi)產(chǎn)品對(duì)象聲明一個(gè)接口。
4.ConcreteProduct 定義一個(gè)將被相應(yīng)的具體工廠創(chuàng)建的產(chǎn)品對(duì)象。 實(shí)現(xiàn)AbstractProduct接口。
5.Client 僅使用由AbstractFactory和AbstractProduct類(lèi)聲明的接口
抽象工廠模式的結(jié)構(gòu)與使用
模式的結(jié)構(gòu)中包括四種角色:
?抽象產(chǎn)品(Prodcut)
?具體產(chǎn)品(ConcreteProduct)
?抽象工廠(AbstractFactory)
?具體工廠(ConcreteFactory)?
模式的UML類(lèi)圖
實(shí)戰(zhàn)部分
【例1】:建立一個(gè)系統(tǒng),該系統(tǒng)可以為用戶提供西服套裝(上衣+褲子)和牛仔套裝(上衣+褲子)。
模式的結(jié)構(gòu)的描述與使用?
1.抽象產(chǎn)品(Product) :
?UpperClothes.java
public abstract class UpperClothes{public abstract int getChestSize();public abstract int getHeight();public abstract String getName(); }Trousers.java
public abstract class Trousers{public abstract int getWaistSize();public abstract int getHeight();public abstract String getName(); }2.具體產(chǎn)品(ConcreteProduct)_1:?WesternUpperClothes.java
public class WesternUpperClothes extends UpperClothes{private int chestSize;private int height;private String name;WesternUpperClothes(String name,int chestSize,int height){this.name=name;this.chestSize=chestSize;this.height=height;}public int getChestSize(){return chestSize;}public int getHeight(){return height;}public String getName(){return name;} }2.具體產(chǎn)品(ConcreteProduct)_2:?CowboyUpperClothes.java
public class CowboyUpperClothes extends UpperClothes{private int chestSize;private int height;private String name;CowboyUpperClothes(String name,int chestSize,int height){this.name=name;this.chestSize=chestSize;this.height=height;}public int getChestSize(){return chestSize;}public int getHeight(){return height;}public String getName(){return name;} }2.具體產(chǎn)品(ConcreteProduct)_3:?WesternTrousers.java
public class WesternTrousers extends Trousers{private int waistSize;private int height;private String name;WesternTrousers(String name,int waistSize,int height){this.name=name;this.waistSize=waistSize;this.height=height;}public int getWaistSize(){return waistSize;}public int getHeight(){return height;}public String getName(){return name;} }2.具體產(chǎn)品(ConcreteProduct)_4:?CowboyTrousers.java
public class CowboyTrousers extends Trousers{private int waistSize;private int height;private String name;CowboyTrousers(String name,int waistSize,int height){this.name=name;this.waistSize=waistSize;this.height=height;}public int getWaistSize(){return waistSize;}public int getHeight(){return height;}public String getName(){return name;} }3.抽象工廠(AbstractFactory):?ClothesFactory.java
public abstract class ClothesFactory{public abstract UpperClothes createUpperClothes(int chestSize,int height);public abstract Trousers createTrousers(int waistSize,int height); }4.具體工廠(ConcreteFactory):?BeijingClothesFactory.java
public class BeijingClothesFactory extends ClothesFactory {public UpperClothes createUpperClothes(int chestSize,int height){return new WesternUpperClothes("北京牌西服上衣",chestSize,height);}public Trousers createTrousers(int waistSize,int height){return new WesternTrousers("北京牌西服褲子",waistSize,height);} }ShanghaiClothesFactory.java
public class ShanghaiClothesFactory extends ClothesFactory {public UpperClothes createUpperClothes(int chestSize,int height){return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);}public Trousers createTrousers(int waistSize,int height){return new WesternTrousers("上海牌牛仔褲",waistSize,height);} }5.應(yīng)用_1:?Shop.java
public class Shop{UpperClothes cloth;Trousers trouser; public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int height){cloth=factory.createUpperClothes(chestSize,height);trouser=factory.createTrousers(waistSize,height);showMess();}private void showMess(){System.out.println("<套裝信息>");System.out.println(cloth.getName()+":");System.out.print("胸圍:"+cloth.getChestSize());System.out.println("身高:"+cloth.getHeight());System.out.println(trouser.getName()+":");System.out.print("腰圍:"+trouser.getWaistSize());System.out.println("身高:"+trouser.getHeight());} }5.應(yīng)用_2:?Application.java
public class Application{public static void main(String args[]){Shop shop=new Shop();ClothesFactory factory=new BeijingClothesFactory(); shop.giveSuit(factory,110,82,170);factory=new ShanghaiClothesFactory(); shop.giveSuit(factory,120,88,180);} }抽象工廠模式的優(yōu)點(diǎn)
?抽象工廠模式可以為用戶創(chuàng)建一系列相關(guān)的對(duì)象,使得用戶和創(chuàng)建這些對(duì)象的類(lèi)脫耦。
?使用抽象工廠模式可以方便的為用戶配置一系列對(duì)象。用戶使用不同的具體工廠就能得到一組相關(guān)的對(duì)象,同時(shí)也能避免用戶混用不同系列中的對(duì)象。
?在抽象工廠模式中,可以隨時(shí)增加“具體工廠”為用戶提供一組相關(guān)的對(duì)象。
總結(jié)
以上是生活随笔為你收集整理的java设计模式2--抽象工厂模式(Abstract Factory)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java设计模式1--工厂方法模式(Fa
- 下一篇: java设计模式3--单例模式(Sing