標籤:
本文地址:http://www.cnblogs.com/archimedes/p/java-abstract-factory-pattern.html,轉載請註明源地址。
抽象原廠模式(別名:配套)
提供一個建立一系列(相互依賴)對象的介面,而無需指定它們具體的類。
概述
當系統準備為使用者提供一系列相關的對象,又不想讓使用者代碼和建立這些對象的類形成耦合時,就可以使用抽象Factory 方法模式來設計系統。抽象原廠模式的關鍵是在一個抽象類別或介面中定義若干個抽象方法,這些抽象方法分別返回某個類的執行個體,該抽象類別或介面讓其子類或實現該介面的類重寫這些抽象方法,為使用者提供一系列相關的對象。
適用性
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)