java设计模式2--抽象工厂模式(Abstract Factory)
本文地址:http://www.cnblogs.com/archimedes/p/java-abstract-factory-pattern.html,轉載請注明源地址。
抽象工廠模式(別名:配套)
提供一個創建一系列(相互依賴)對象的接口,而無需指定它們具體的類。
概述
當系統準備為用戶提供一系列相關的對象,又不想讓用戶代碼和創建這些對象的類形成耦合時,就可以使用抽象工廠方法模式來設計系統。抽象工廠模式的關鍵是在一個抽象類或接口中定義若干個抽象方法,這些抽象方法分別返回某個類的實例,該抽象類或接口讓其子類或實現該接口的類重寫這些抽象方法,為用戶提供一系列相關的對象。
適用性
1.一個系統要獨立于它的產品的創建、組合和表示時。
2.一個系統要由多個產品系列中的一個來配置時。
3.當你要強調一系列相關的產品對象的設計以便進行聯合使用時。
4.當你提供一個產品類庫,而只想顯示它們的接口而不是實現時。
參與者
1.AbstractFactory 聲明一個創建抽象產品對象的操作接口。
2.ConcreteFactory 實現創建具體產品對象的操作。
3.AbstractProduct 為一類產品對象聲明一個接口。
4.ConcreteProduct 定義一個將被相應的具體工廠創建的產品對象。 實現AbstractProduct接口。
5.Client 僅使用由AbstractFactory和AbstractProduct類聲明的接口
抽象工廠模式的結構與使用
模式的結構中包括四種角色:
?抽象產品(Prodcut)
?具體產品(ConcreteProduct)
?抽象工廠(AbstractFactory)
?具體工廠(ConcreteFactory)?
模式的UML類圖
實戰部分
【例1】:建立一個系統,該系統可以為用戶提供西服套裝(上衣+褲子)和牛仔套裝(上衣+褲子)。
模式的結構的描述與使用?
1.抽象產品(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.具體產品(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.具體產品(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.具體產品(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.具體產品(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.應用_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.應用_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);} }抽象工廠模式的優點
?抽象工廠模式可以為用戶創建一系列相關的對象,使得用戶和創建這些對象的類脫耦。
?使用抽象工廠模式可以方便的為用戶配置一系列對象。用戶使用不同的具體工廠就能得到一組相關的對象,同時也能避免用戶混用不同系列中的對象。
?在抽象工廠模式中,可以隨時增加“具體工廠”為用戶提供一組相關的對象。
總結
以上是生活随笔為你收集整理的java设计模式2--抽象工厂模式(Abstract Factory)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java设计模式1--工厂方法模式(Fa
- 下一篇: java设计模式3--单例模式(Sing