Java設(shè)計(jì)模式-(創(chuàng)建型)抽象工廠模式

代碼倉庫:https://gitee.com/yangsiyuan/Design-Pattern

創(chuàng)建型設(shè)計(jì)模式:聚焦于實(shí)例化對象,通常提供一種隱藏創(chuàng)建邏輯的形式,取代直接使用new運(yùn)算符實(shí)例化對象

1. 定義

抽象工廠模式(Abstract Factory Pattern):通過構(gòu)造一個(gè)接口充當(dāng)超級工廠(即抽象工廠),來創(chuàng)建其他工廠

  • 核心定義:定義一個(gè)對象接口,用于規(guī)范所有對象;定義一個(gè)工廠抽象類,用于規(guī)范工廠類獲取對象的方式;再提供一個(gè)生成器類,提供靜態(tài)方法對象統(tǒng)一提供獲取工廠的方法。

  • 優(yōu)點(diǎn):當(dāng)一個(gè)產(chǎn)品族中的多個(gè)對象被設(shè)計(jì)成一起工作時(shí),它能保證客戶端始終只使用同一個(gè)產(chǎn)品族中的對象

  • 缺點(diǎn):由于存在兩層封裝,若擴(kuò)展比較麻煩

  • 應(yīng)用場景:針對不同操作系統(tǒng)進(jìn)行適配的場景;針對不用語言/時(shí)區(qū)進(jìn)行適配的場景

2. 代碼實(shí)現(xiàn)

1)定義對象接口

即描述對象的多個(gè)維度

public interface ICarType {
    void run();
}
public interface ICarPower {
    void power();
}

2)定義對象實(shí)現(xiàn)類

  • ICarType的實(shí)現(xiàn)類
// 實(shí)現(xiàn)類1
public class Honda implements ICarType{
    @Override
    public void run() {
        System.out.println("Honda is running!!");
    }
}

// 實(shí)現(xiàn)類2
public class Benz implements ICarType {
    @Override
    public void run() {
        System.out.println("Benz is runnning!!");
    }
}
  • ICarPower的實(shí)現(xiàn)類
// 實(shí)現(xiàn)類1
public class Oil implements ICarPower{
    @Override
    public void power() {
        System.out.println("I am oil car!!");
    }
}

// 實(shí)現(xiàn)類2
public class Electric implements ICarPower{
    @Override
    public void power() {
        System.out.println("I am Electric car!!");
    }
}

3) 定義抽象工廠

定義獲取對象的api

public abstract class AbstractCarFactory {
    public abstract ICarType getCarType(String brandName);
    public abstract ICarPower getCarPower(String powerName);
}

4)定義工廠實(shí)現(xiàn)類

  • 獲取ICarType對象的工廠
public class CarTypeFactory extends AbstractCarFactory{

    @Override
    public ICarType getCarType(String brandName) {
        if(brandName == null){
            return null;
        }
        if("Benz".equals(brandName)){
            return new Benz();
        }else if("Honda".equals(brandName)){
            return new Honda();
        }
        return null;
    }

    @Override
    public ICarPower getCarPower(String powerName) {
        return null;
    }
}
  • 獲取ICarPower對象的工廠
public class CarPowerFactory extends AbstractCarFactory{

    @Override
    public ICarType getCarType(String brandName) {
        return null;
    }

    @Override
    public ICarPower getCarPower(String powerName) {
        if(powerName == null){
            return null;
        }

        if("oil".equals(powerName)){
            return new Oil();
        }else if("Electric".equals(powerName)){
            return new Electric();
        }

        return null;
    }
}

5)定義工廠生成器

用于獲取工廠類

public class CarFactoryProducer {

    public static AbstractCarFactory getCarFactory(String factoryType){
        if("type".equals(factoryType)){
            return new CarTypeFactory();
        }else if("power".equals(factoryType)){
            return new CarPowerFactory();
        }
        return null;
    }
}

6)調(diào)用demo

public class AbastractFactoryTest {
    public static void main(String[] args) {
        // 1. 通過生成器獲取工廠
        AbstractCarFactory carFactory = CarFactoryProducer.getCarFactory("type");
       // 2. 通過工廠獲取對象
        ICarType benz = carFactory.getCarType("Benz");
      // 3. 調(diào)用對象的方法
        benz.run();

        AbstractCarFactory carFactory1 = CarFactoryProducer.getCarFactory("power");
        ICarPower oil = carFactory1.getCarPower("oil");
        oil.power();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容