十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本文源碼:GitHub·點這里 || GitEE·點這里
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站建設、網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元隆回做網(wǎng)站,已為上家服務,為隆回各地企業(yè)和個人服務,聯(lián)系電話:18982081108系統(tǒng)常見的數(shù)據(jù)導出功能:數(shù)據(jù)導出PDF、WORD等常見格式。
是類的創(chuàng)建模式,又叫做虛擬構造子(Virtual Constructor)模式或者多態(tài)性工廠(Polymorphic Factory)模式。工廠方法模式的用意是定義一個創(chuàng)建產(chǎn)品對象的工廠接口,將實際創(chuàng)建工作推遲到子類中。
1)、抽象工廠角色
這個角色的是工廠方法模式的核心,任何在模式中創(chuàng)建對象的工廠類必須實現(xiàn)這個接口。在實際的系統(tǒng)中,這個角色也常常使用抽象類實現(xiàn)。
2)、具體工廠角色
擔任這個角色的是實現(xiàn)了抽象工廠接口的具體JAVA類。具體工廠角色含有與業(yè)務密切相關的邏輯,并且受到使用者的調(diào)用以創(chuàng)建導出類。
3)、抽象導出角色
工廠方法模式所創(chuàng)建的對象的超類,也就是所有導出類的共同父類或共同擁有的接口。在實際的系統(tǒng)中,這個角色也常常使用抽象類實現(xiàn)。
4)、具體導出角色
這個角色實現(xiàn)了抽象導出角色所聲明的接口,工廠方法模式所創(chuàng)建的每一個對象都是某個具體導出角色的實例。

// 客戶端角色
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("沒有找到對象") ;
}
}
}
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("沒有找到對象") ;
}
}
}
// 抽象導出角色
interface ExportFile {
boolean export (String data) ;
}
// 具體導出角色
class ExportUserWordFile implements ExportFile {
@Override
public boolean export(String data) {
System.out.println("導出用戶Word文件");
return true;
}
}
class ExportLogWordFile implements ExportFile {
@Override
public boolean export(String data) {
System.out.println("導出日志W(wǎng)ord文件");
return true;
}
}
class ExportUserPdfFile implements ExportFile {
@Override
public boolean export(String data) {
System.out.println("導出用戶Pdf文件");
return true;
}
}
class ExportLogPdfFile implements ExportFile {
@Override
public boolean export(String data) {
System.out.println("導出日志Pdf文件");
return true;
}
}
基于spring框架的配置實現(xiàn)如下流程:汽車工廠根據(jù)不同的國家,生產(chǎn)不同類型的汽車。
public class ProductCar implements CarFactory {
private Map carMap = null;
public ProductCar() {
carMap = new HashMap<>();
carMap.put("china", new CarEntity("中國", "黑色","紅旗"));
carMap.put("america", new CarEntity("美國", "白色","雪佛蘭"));
}
@Override
public CarEntity getCar(String type) {
return carMap.get(type);
}
}
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)、輸出結果
CarEntity{country='中國', color='黑色', name='紅旗'}
CarEntity{country='美國', color='白色', name='雪佛蘭'}
工廠方法中,把創(chuàng)建類的動作延遲,就是通過對應的工廠來生成類的對象,這種設計方式符合“開閉”原則。缺點就是當產(chǎn)品的種類過多的時候,需要定義很多產(chǎn)品對應的工廠類。
GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。