日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Java啤酒生产系统描述_Java描述设计模式(03):工厂方法模式

發布時間:2025/3/12 windows 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java啤酒生产系统描述_Java描述设计模式(03):工厂方法模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、工廠方法模式

1、生活場景

系統常見的數據導出功能:數據導出PDF、WORD等常見格式。

2、工廠方法模式

是類的創建模式,又叫做虛擬構造子(Virtual Constructor)模式或者多態性工廠(Polymorphic Factory)模式。工廠方法模式的用意是定義一個創建產品對象的工廠接口,將實際創建工作推遲到子類中。

3、核心角色

1)、抽象工廠角色

這個角色的是工廠方法模式的核心,任何在模式中創建對象的工廠類必須實現這個接口。在實際的系統中,這個角色也常常使用抽象類實現。

2)、具體工廠角色

擔任這個角色的是實現了抽象工廠接口的具體JAVA類。具體工廠角色含有與業務密切相關的邏輯,并且受到使用者的調用以創建導出類。

3)、抽象導出角色

工廠方法模式所創建的對象的超類,也就是所有導出類的共同父類或共同擁有的接口。在實際的系統中,這個角色也常常使用抽象類實現。

4)、具體導出角色

這個角色實現了抽象導出角色所聲明的接口,工廠方法模式所創建的每一個對象都是某個具體導出角色的實例。

4、代碼UML關系圖

5、源代碼實現

// 客戶端角色

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("導出日志Word文件");

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框架中應用

1、場景描述

基于spring框架的配置實現如下流程:汽車工廠根據不同的國家,生產不同類型的汽車。

2、核心工廠類

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);

}

}

3、核心Xml配置文件

4、測試類

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='雪佛蘭'}

三、工廠方法小結

工廠方法中,把創建類的動作延遲,就是通過對應的工廠來生成類的對象,這種設計方式符合“開閉”原則。缺點就是當產品的種類過多的時候,需要定義很多產品對應的工廠類。

四、源代碼地址

GitHub·地址

https://github.com/cicadasmile/model-arithmetic-parent

GitEE·地址

https://gitee.com/cicadasmile/model-arithmetic-parent

總結

以上是生活随笔為你收集整理的Java啤酒生产系统描述_Java描述设计模式(03):工厂方法模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。