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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java描述设计模式(04):抽象工厂模式

發布時間:2025/3/17 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java描述设计模式(04):抽象工厂模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、抽象工廠模式

1、生活場景

汽車生產根據用戶選擇的汽車類型,指定不同的工廠進行生產,選擇紅旗轎車,就要使用中國工廠,選擇奧迪轎車,就要使用德國工廠。

2、抽象工廠模式

  • 抽象工廠模式:定義了一個interface用于創建相關對象或相互依賴的對象,而無需指明具體的類;
  • 抽象工廠模式可以將簡單工廠模式和工廠方法模式進行整合;
  • 從設計層面看,抽象工廠模式就是對簡單工廠模式的改進(或者稱為進一步的抽象)。
  • 將工廠抽象成兩層,AbstractFactory(抽象工廠) 和 具體實現的工廠子類,方便程序擴展。
  • 3、代碼UML圖

    4、源代碼實現

    /*** 抽象工廠模式*/ public class C01_AbstractFactory {public static void main(String[] args) {CarProductFactory factory = new ChinaCarFactory() ;factory.getCar("hq") ;factory = new GermanyCarFactory () ;factory.getCar("ad") ;} }// 汽車生產抽象工廠 interface CarProductFactory {CarProduct getCar (String type) ; } // 中國汽車工廠 class ChinaCarFactory implements CarProductFactory {@Overridepublic CarProduct getCar(String type) {CarProduct product = null ;if ("hq".equals(type)){product = new HQCar() ;product.name="紅旗一號" ;product.date="1999-09-19" ;product.material();product.origin();} else if ("df".equals(type)){product = new DFCar() ;product.name="東風一號" ;product.date="2019-09-19" ;product.material();product.origin();}return product ;} } // 德國汽車工廠 class GermanyCarFactory implements CarProductFactory {@Overridepublic CarProduct getCar(String type) {CarProduct product = null ;if ("ad".equals(type)){product = new ADCar() ;product.name="奧迪A8" ;product.date="2017-09-19" ;product.material();product.origin();} else if ("bm".equals(type)){product = new BMCar() ;product.name="寶馬X8" ;product.date="2018-09-19" ;product.material();product.origin();}return product ;} } // 汽車生產抽象類 abstract class CarProduct {/*** 汽車名稱*/protected String name ;/*** 生產日期*/protected String date ;/*** 材料*/abstract void material () ;/*** 產地*/abstract void origin () ; } // 紅旗車 class HQCar extends CarProduct {@Overridevoid material() {System.out.println(super.name+"材料...");}@Overridevoid origin() {System.out.println(super.date+":"+super.name+"在中國北京生產");} } // 東風車 class DFCar extends CarProduct {@Overridevoid material() {System.out.println(super.name+"材料...");}@Overridevoid origin() {System.out.println(super.date+":"+super.name+"在中國南京生產");} } // 奧迪車 class ADCar extends CarProduct {@Overridevoid material() {System.out.println(super.name+"材料...");}@Overridevoid origin() {System.out.println(super.date+":"+super.name+"在德國柏林生產");} } // 寶馬車 class BMCar extends CarProduct {@Overridevoid material() {System.out.println(super.name+"材料...");}@Overridevoid origin() {System.out.println(super.date+":"+super.name+"在德國慕尼黑生產");} }

    二、Spring框架應用

    1、場景描述

    Spring框架中獲取配置文件中Bean的多種方式。

    2、核心配置

    <bean id="carBean" class="com.model.design.spring.node04.abstractFactory.CarBean"><property name="name" value="中國紅旗" /> </bean> <bean id="carBean1" class="com.model.design.spring.node04.abstractFactory.CarBean"><property name="name" value="德國奧迪" /> </bean>

    3、測試文件

    這里使用了兩種方式獲取。

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:/spring/spring-abstract-factory.xml"}) public class SpringTest {@Resourceprivate BeanFactory beanFactory ;@Testpublic void test01 (){CarBean carBean = (CarBean)beanFactory.getBean("carBean") ;System.out.println(carBean.getName());}@Testpublic void test02 (){ApplicationContext context01 = new ClassPathXmlApplicationContext("/spring/spring-abstract-factory.xml");CarBean carBean = (CarBean)context01.getBean("carBean1") ;System.out.println(carBean.getName());} }

    4、結構分析


    抽象工廠封裝對象的創建。在Spring中,通過實現BeanFactory??梢詮腟pring的各種容器獲取bean。根據Bean的配置,getBean方法可以返回不同類型的對象(單例作用域)或初始化新的對象(原型作用域)。在BeanFactory的實現中,我們可以區分:ClassPathXmlApplicationContext,XmlWebApplicationContext等。

    三、工廠模式小結

    三種工廠模式 (簡單工廠模式、工廠方法模式、抽象工廠模式),工廠模式的核心用意將實例化對象的代碼封裝起來,放到工廠類中統一管理和維護,完成代碼依賴關系的解耦。從而提高程序的可擴展性和維護性。

    四、源代碼地址

    GitHub地址:知了一笑 https://github.com/cicadasmile/model-arithmetic-parent 碼云地址:知了一笑 https://gitee.com/cicadasmile/model-arithmetic-parent


    總結

    以上是生活随笔為你收集整理的Java描述设计模式(04):抽象工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。

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