日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

我們一起聊聊抽象工廠模式(AbstractFactoty)

來源: 責編: 時間:2023-08-09 23:03:48 307觀看
導讀今天給大家介紹《Java極簡設(shè)計模式》的第02章,抽象工廠模式(AbstractFactoty),多一句沒有,少一句不行,用最簡短的篇幅講述設(shè)計模式最核心的知識,好了,開始今天的內(nèi)容。一、概述提供一個創(chuàng)建一系列相關(guān)或相互依賴對象的接口,

今天給大家介紹《Java極簡設(shè)計模式》的第02章,抽象工廠模式(AbstractFactoty),多一句沒有,少一句不行,用最簡短的篇幅講述設(shè)計模式最核心的知識,好了,開始今天的內(nèi)容。LU928資訊網(wǎng)——每日最新資訊28at.com

一、概述

提供一個創(chuàng)建一系列相關(guān)或相互依賴對象的接口,而無需指定它們具體的類。LU928資訊網(wǎng)——每日最新資訊28at.com

二、為何使用

工廠模式是我們最常用的模式了,著名的Jive論壇 ,就大量使用了工廠模式,工廠模式在Java程序系統(tǒng)可以說是隨處可見。LU928資訊網(wǎng)——每日最新資訊28at.com

為什么工廠模式是如此常用?因為工廠模式就相當于創(chuàng)建實例對象的new,我們經(jīng)常要根據(jù)類Class生成實例對象,如A a=new A() 工廠模式也是用來創(chuàng)建實例對象的,所以以后new時就要多個心眼,是否可以考慮實用工廠模式,雖然這樣做,可能多做一些工作,但會給你系統(tǒng)帶來更大的可擴展性和盡量少的修改量。LU928資訊網(wǎng)——每日最新資訊28at.com

三、實用性

  1. 一個系統(tǒng)要獨立于它的產(chǎn)品的創(chuàng)建、組合和表示時。
  2. 一個系統(tǒng)要由多個產(chǎn)品系列中的一個來配置時。
  3. 當你要強調(diào)一系列相關(guān)的產(chǎn)品對象的設(shè)計以便進行聯(lián)合使用時。
  4. 當你提供一個產(chǎn)品類庫,而只想顯示它們的接口而不是實現(xiàn)時。

四、參與者

  1. AbstractFactory 聲明一個創(chuàng)建抽象產(chǎn)品對象的操作接口。
  2. ConcreteFactory 實現(xiàn)創(chuàng)建具體產(chǎn)品對象的操作。
  3. AbstractProduct 為一類產(chǎn)品對象聲明一個接口。
  4. ConcreteProduct 定義一個將被相應的具體工廠創(chuàng)建的產(chǎn)品對象。實現(xiàn)AbstractProduct接口。
  5. Client 僅使用由AbstractFactory和AbstractProduct類聲明的接口

五、類圖


LU928資訊網(wǎng)——每日最新資訊28at.com

圖片圖片LU928資訊網(wǎng)——每日最新資訊28at.com

六、示例

  • AbstractFactory

定義抽象工程類IAnimalFactoryLU928資訊網(wǎng)——每日最新資訊28at.com

public interface IAnimalFactory {    /**     * 定義創(chuàng)建Icat接口實例的方法     * @return     */    ICat createCat();    /**     * 定義創(chuàng)建IDog接口實例的方法     * @return     */    IDog createDog();}
  • ConcreteFactory

創(chuàng)建抽象工廠類的兩個實現(xiàn)類,WhiteAnimalFactory和BlackAnimalFactoryLU928資訊網(wǎng)——每日最新資訊28at.com

public class WhiteAnimalFactory implements IAnimalFactory {    public ICat createCat() {        return new WhiteCat();    }    public IDog createDog() {        return new WhiteDog();    }}
public class BlackAnimalFactory implements IAnimalFactory { @Override    public ICat createCat() {        return new BlackCat();    }    public IDog createDog() {        return new BlackDog();    }}
  • AbstractProduct

定義抽象工廠中要生產(chǎn)的抽象產(chǎn)品接口ICat和IDogLU928資訊網(wǎng)——每日最新資訊28at.com

public interface ICat {    /**     * 定義方法     */    void eat();}
public interface IDog {  /**     * 定義方法     */    void eat();}
  • ConcreteProduct

創(chuàng)建產(chǎn)品的實現(xiàn)類BlackCat、BlackDog、WhiteCat、WhiteDogLU928資訊網(wǎng)——每日最新資訊28at.com

public class BlackCat implements ICat { @Override    public void eat() {        System.out.println("The black cat is eating!");    }}
public class BlackDog implements IDog { @Override    public void eat() {        System.out.println("The black dog is eating");    }}
public class WhiteCat implements ICat { @Override    public void eat() {        System.out.println("The white cat is eating!");    }}
public class WhiteDog implements IDog { @Override    public void eat() {        System.out.println("The white dog is eating!");    }}
  • Client

定義一個測試類TestLU928資訊網(wǎng)——每日最新資訊28at.com

public class Test { public static void main(String[] args) {     IAnimalFactory blackAnimalFactory = new BlackAnimalFactory();     ICat blackCat = blackAnimalFactory.createCat();     blackCat.eat();     IDog blackDog = blackAnimalFactory.createDog();     blackDog.eat();          IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory();     ICat whiteCat = whiteAnimalFactory.createCat();     whiteCat.eat();     IDog whiteDog = whiteAnimalFactory.createDog();     whiteDog.eat(); }}
  • 輸出結(jié)果
The black cat is eating!The black dog is eatingThe white cat is eating!The white dog is eating!

七、總結(jié)

由此可見,工廠方法確實為系統(tǒng)結(jié)構(gòu)提供了非常靈活強大的動態(tài)擴展機制,只要我們更換一下具體的工廠方法,系統(tǒng)其他地方無需一點變換,就有可能將系統(tǒng)功能進行改頭換面的變化。LU928資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-5195-0.html我們一起聊聊抽象工廠模式(AbstractFactoty)

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 圖解算法,原理逐步揭開「GitHub 熱點速覽」

下一篇: Go-Zero 是如何做路由管理的?

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 榆林市| 衡南县| 庆元县| 荥经县| 庆安县| 通化市| 门源| 称多县| 淅川县| 东台市| 同心县| 清新县| 长阳| 钟山县| 香港 | 新建县| 兴宁市| 朔州市| 孝感市| 宜良县| 新乐市| 五莲县| 富源县| 尉犁县| 沙河市| 左权县| 临夏县| 龙陵县| 孟津县| 武夷山市| 隆化县| 论坛| 达孜县| 福清市| 大石桥市| 宁陕县| 临澧县| 浮山县| 白城市| 湘乡市| 白水县|