Java描述設(shè)計(jì)模式(03):工廠方法模式-創(chuàng)新互聯(lián)

本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里

創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元隆回做網(wǎng)站,已為上家服務(wù),為隆回各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

一、工廠方法模式

1、生活場(chǎng)景

系統(tǒng)常見(jiàn)的數(shù)據(jù)導(dǎo)出功能:數(shù)據(jù)導(dǎo)出PDF、WORD等常見(jiàn)格式。

2、工廠方法模式

是類的創(chuàng)建模式,又叫做虛擬構(gòu)造子(Virtual Constructor)模式或者多態(tài)性工廠(Polymorphic Factory)模式。工廠方法模式的用意是定義一個(gè)創(chuàng)建產(chǎn)品對(duì)象的工廠接口,將實(shí)際創(chuàng)建工作推遲到子類中。

3、核心角色

1)、抽象工廠角色
這個(gè)角色的是工廠方法模式的核心,任何在模式中創(chuàng)建對(duì)象的工廠類必須實(shí)現(xiàn)這個(gè)接口。在實(shí)際的系統(tǒng)中,這個(gè)角色也常常使用抽象類實(shí)現(xiàn)。

2)、具體工廠角色
擔(dān)任這個(gè)角色的是實(shí)現(xiàn)了抽象工廠接口的具體JAVA類。具體工廠角色含有與業(yè)務(wù)密切相關(guān)的邏輯,并且受到使用者的調(diào)用以創(chuàng)建導(dǎo)出類。

3)、抽象導(dǎo)出角色
工廠方法模式所創(chuàng)建的對(duì)象的超類,也就是所有導(dǎo)出類的共同父類或共同擁有的接口。在實(shí)際的系統(tǒng)中,這個(gè)角色也常常使用抽象類實(shí)現(xiàn)。

4)、具體導(dǎo)出角色
這個(gè)角色實(shí)現(xiàn)了抽象導(dǎo)出角色所聲明的接口,工廠方法模式所創(chuàng)建的每一個(gè)對(duì)象都是某個(gè)具體導(dǎo)出角色的實(shí)例。

4、代碼UML關(guān)系圖

Java描述設(shè)計(jì)模式(03):工廠方法模式

5、源代碼實(shí)現(xiàn)

// 客戶端角色
public class C01_FactoryMethod {
    public static void main(String[] args) {
        String data = "" ;
        ExportFactory factory = new ExportWordFactory () ;
        ExportFile exportWord = factory.factory("user-word") ;
        exportWord.export(data) ;
        factory = new ExportPdfFactory() ;
        ExportFile exportPdf =factory.factory("log-pdf") ;
        exportPdf.export(data) ;
    }
}
// 抽象工廠角色
interface ExportFactory {
    ExportFile factory (String type) ;
}
// 具體工廠角色
class ExportWordFactory implements ExportFactory {
    @Override
    public ExportFile factory(String type) {
        if ("user-word".equals(type)){
            return new ExportUserWordFile() ;
        } else if ("log-word".equals(type)){
            return new ExportLogWordFile() ;
        } else {
            throw new RuntimeException("沒(méi)有找到對(duì)象") ;
        }
    }
}
class ExportPdfFactory implements ExportFactory {
    @Override
    public ExportFile factory(String type) {
        if ("user-pdf".equals(type)){
            return new ExportUserPdfFile() ;
        } else if ("log-pdf".equals(type)){
            return new ExportLogPdfFile() ;
        } else {
            throw new RuntimeException("沒(méi)有找到對(duì)象") ;
        }
    }
}
// 抽象導(dǎo)出角色
interface ExportFile {
    boolean export (String data) ;
}
// 具體導(dǎo)出角色
class ExportUserWordFile implements ExportFile {
    @Override
    public boolean export(String data) {
        System.out.println("導(dǎo)出用戶Word文件");
        return true;
    }
}
class ExportLogWordFile implements ExportFile {
    @Override
    public boolean export(String data) {
        System.out.println("導(dǎo)出日志W(wǎng)ord文件");
        return true;
    }
}
class ExportUserPdfFile implements ExportFile {
    @Override
    public boolean export(String data) {
        System.out.println("導(dǎo)出用戶Pdf文件");
        return true;
    }
}
class ExportLogPdfFile implements ExportFile {
    @Override
    public boolean export(String data) {
        System.out.println("導(dǎo)出日志Pdf文件");
        return true;
    }
}

二、Spring框架中應(yīng)用

1、場(chǎng)景描述

基于spring框架的配置實(shí)現(xiàn)如下流程:汽車工廠根據(jù)不同的國(guó)家,生產(chǎn)不同類型的汽車。

2、核心工廠類

public class ProductCar implements CarFactory {
    private Map<String, CarEntity> carMap = null;
    public ProductCar() {
        carMap = new HashMap<>();
        carMap.put("china", new CarEntity("中國(guó)", "黑色","紅旗"));
        carMap.put("america", new CarEntity("美國(guó)", "白色","雪佛蘭"));
    }
    @Override
    public CarEntity getCar(String type) {
        return carMap.get(type);
    }
}

3、核心Xml配置文件

<bean id="productCarFactory" class="com.model.design.spring.node03.factoryMethod.ProductCar" />
<bean id="car1" factory-bean="productCarFactory" factory-method="getCar">
    <constructor-arg name="type" value="china" />
</bean>
<bean id="car2" factory-bean="productCarFactory" factory-method="getCar">
    <constructor-arg name="type" value="america" />
</bean>

4、測(cè)試類

1)、代碼塊

public class SpringTest {
    @Test
    public void test01 (){
        ApplicationContext context01 = new ClassPathXmlApplicationContext("/spring/spring-factorymethod.xml");
        CarEntity car1 = (CarEntity)context01.getBean("car1") ;
        CarEntity car2 = (CarEntity)context01.getBean("car2") ;
        System.out.println(car1);
        System.out.println(car2);
    }
}

2)、輸出結(jié)果

CarEntity{country='中國(guó)', color='黑色', name='紅旗'}
CarEntity{country='美國(guó)', color='白色', name='雪佛蘭'}

三、工廠方法小結(jié)

工廠方法中,把創(chuàng)建類的動(dòng)作延遲,就是通過(guò)對(duì)應(yīng)的工廠來(lái)生成類的對(duì)象,這種設(shè)計(jì)方式符合“開(kāi)閉”原則。缺點(diǎn)就是當(dāng)產(chǎn)品的種類過(guò)多的時(shí)候,需要定義很多產(chǎn)品對(duì)應(yīng)的工廠類。

四、源代碼地址

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Java描述設(shè)計(jì)模式(03):工廠方法模式

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)標(biāo)題:Java描述設(shè)計(jì)模式(03):工廠方法模式-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article2/cdcjic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、小程序開(kāi)發(fā)、云服務(wù)器網(wǎng)站維護(hù)、網(wǎng)站營(yíng)銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)