Java設計模式——Factory 方法模式(factory method)

來源:互聯網
上載者:User

標籤:設計模式   java   Factory 方法模式   

Factory 方法模式是類的建立模式,用意是定義一個建立產品對象的工廠介面,將實際建立工作延遲到子類中。
相信很多人都做過匯入匯出功能,就拿匯出功能來說。有這麼一個需求:某一個系統需要支援對資料庫中的員工薪資進行匯出,並且支援多種格式如:HTML、Excel、PDF等,每種格式匯出的結構有所不同,比如:財務跟其他人對匯出薪資的HTML格式要求可能會不一樣,因為財務可能需要特定的格式方便核算或其他用途。如果使用簡單原廠模式,則工廠類必定過於臃腫。因為簡單原廠模式只有一個工廠類,它需要處理所有的建立的邏輯。假如以上需求暫時只支援3種匯出的格式以及2種匯出的結構,那工廠類則需要6個if else來建立6種不同的類型。如果日後需求不斷增加,則後果不堪設想。
這時候就需要Factory 方法模式來處理以上需求。在Factory 方法模式中,核心的工廠類不再負責所有的對象的建立,而是將具體建立的工作交給子類去做。這個核心類則搖身一變,成為了一個抽象工廠角色,僅負責給出具體工廠子類必須實現的介面,而不接觸哪一個類應當被執行個體化這種細節。
UML設計圖如下:

public interface ExportFactory {    public ExportFile factory(String type);}
public interface ExportFile {    public boolean export(String data);}
public class ExportHtmlFactory implements ExportFactory {    public ExportFile factory(String type) {        if("standard".equals(type))        {            return new ExportStandardHtmlFile();        }        else if("finacial".equals(type))        {            return new ExportFinacialHtmlFile();        }        else        {            throw new RuntimeException("沒有找到對象");        }    }}
public class ExportPdfFactory implements ExportFactory {    public ExportFile factory(String type) {        if("standard".equals(type))        {            return new ExportStandardPdfFile();        }        else if("finacial".equals(type)){            return new ExportFinacialPdfFile();        }        else        {            throw new RuntimeException("沒有找到對象");        }    }}
public class ExportFinacialHtmlFile implements ExportFile {    public boolean export(String data) {        System.out.println("匯出財務HTML模板!");        return true;    }}
public class ExportFinacialPdfFile implements ExportFile {    public boolean export(String data) {        System.out.println("匯出財務PDF模板");        return true;    }}
public class ExportStandardHtmlFile implements ExportFile {    public boolean export(String data) {        System.out.println("匯出標準HTML模板");        return true;    }}
public class ExportStandardPdfFile implements ExportFile {    public boolean export(String data) {        System.out.println("匯出標準PDF模板");        return true;    }}
/**     * @author 付玉偉     * @time 2015-3-17 下午08:57:11     * @param args     */    public static void main(String[] args) {        String data = "";        ExportFactory exportFactory = new ExportHtmlFactory();        ExportFile exportFile = exportFactory.factory("finacial");        exportFile.export(data);    }}

一個應用系統是由多人開發的,匯出的功能是你實現的,但是使用者(調用這個方法的人)可能卻是其他人。這時候你應該設計的足夠靈活並儘可能降低兩者之間的耦合度,當你修改或增加一個新的功能時,使用者不需要修改任何地方。假如你的設計不夠靈活,那麼將無法面對客戶多變的需求。可能一個極小的需求變更,都會使你的代碼結構發生改變,並導致其他使用你所提供的介面的人都要修改他們的代碼。牽一處而動全身,這就使得日後這個系統將難以維護。
簡單原廠模式
簡單原廠模式是由一個工廠對象絕對建立出哪一種產品執行個體
類圖如下:

Java設計模式——Factory 方法模式(factory method)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.