深入探究Factory Method設計模式

一、Factory Method設計模式介紹

Factory Method設計模式是一種創建型設計模式,它的主要目的是為了將對象的創建過程從主要業務邏輯中抽離出來,從而提高代碼的可維護性和可擴展性。在使用Factory Method模式時,我們將對象的創建交給了Factory Method,而業務邏輯的代碼只需要調用Factory Method提供的接口即可獲得對象。

Factory Method模式的核心是一個接口(或抽象類),用於規範所有具體工廠類所需要實現的方法。具體工廠類實現該接口,生產出具體的產品對象。

二、Factory Method設計模式的實現

1、簡單工廠模式

在簡單工廠模式中,我們需要一個根據傳入參數動態創建對象的工廠類。對於每一個需要創建的對象,我們都需要手動添加對應的創建方法。當需要創建新的對象時,我們不得不修改工廠類的內部邏輯。這種方式不僅不符合開閉原則,而且增加了工廠類的負擔。

interface Fruit {
    void produce();
}

class Apple implements Fruit {
    public void produce() {
        System.out.println("產生了一個蘋果!");
    }
}

class Strawberry implements Fruit {
    public void produce() {
        System.out.println("產生了一個草莓!");
    }
}

class FruitFactory {
    public Fruit createFruit(String type) {
        if("apple".equals(type)) {
            return new Apple();
        } else if("strawberry".equals(type)) {
            return new Strawberry();
        } else {
            return null;
        }
    }
}

public class SimpleFactoryTest {
    public static void main(String[] args) {
        FruitFactory factory = new FruitFactory();
        Fruit apple = factory.createFruit("apple");
        Fruit strawberry = factory.createFruit("strawberry");
        apple.produce();
        strawberry.produce();
    }
}

2、工廠方法模式

工廠方法模式是對簡單工廠模式的改進,在工廠方法模式中,我們將創建一個對象的方法抽象出來,在工廠抽象類中定義該方法的接口,具體工廠類實現這個方法來創建具體的產品對象。使用工廠方法模式時,我們可以通過擴展工廠抽象類和具體工廠類來創建新的對象,而無需修改已有的代碼。

interface Fruit {
    void produce();
}

class Apple implements Fruit {
    public void produce() {
        System.out.println("產生了一個蘋果!");
    }
}

class Strawberry implements Fruit {
    public void produce() {
        System.out.println("產生了一個草莓!");
    }
}

interface FruitFactory {
    Fruit createFruit();
}

class AppleFactory implements FruitFactory {
    public Fruit createFruit() {
        return new Apple();
    }
}

class StrawberryFactory implements FruitFactory {
    public Fruit createFruit() {
        return new Strawberry();
    }
}

public class FactoryMethodTest {
    public static void main(String[] args) {
        FruitFactory appleFactory = new AppleFactory();
        Fruit apple = appleFactory.createFruit();
        apple.produce();

        FruitFactory strawberryFactory = new StrawberryFactory();
        Fruit strawberry = strawberryFactory.createFruit();
        strawberry.produce();
    }
}

3、抽象工廠模式

抽象工廠模式是進一步的改進,它解決了工廠方法模式內部只能有一種產品類型的問題。在抽象工廠模式中,我們將多個工廠類抽象出來,每個工廠類負責創建一系列相關聯的產品。這樣,我們就可以生產不同的產品族並且可以保證一組產品一起生產,從而保證產品的兼容性。

interface Fruit {
    void produce();
}

interface Cake {
    void make();
}

class Apple implements Fruit {
    public void produce() {
        System.out.println("產生了一個蘋果!");
    }
}

class Strawberry implements Fruit {
    public void produce() {
        System.out.println("產生了一個草莓!");
    }
}

class Mousse implements Cake {
    public void make() {
        System.out.println("製作了一份慕斯蛋糕!");
    }
}

class Cheesecake implements Cake {
    public void make() {
        System.out.println("製作了一份芝士蛋糕!");
    }
}

interface FruitFactory {
    Fruit createFruit();
}

interface CakeFactory {
    Cake createCake();
}

class AppleFactory implements FruitFactory {
    public Fruit createFruit() {
        return new Apple();
    }
}

class StrawberryFactory implements FruitFactory {
    public Fruit createFruit() {
        return new Strawberry();
    }
}

class MousseFactory implements CakeFactory {
    public Cake createCake() {
        return new Mousse();
    }
}

class CheesecakeFactory implements CakeFactory {
    public Cake createCake() {
        return new Cheesecake();
    }
}

interface AbstractFactory {
    FruitFactory createFruitFactory();
    CakeFactory createCakeFactory();
}

class SpringFactory implements AbstractFactory {
    public FruitFactory createFruitFactory() {
        return new AppleFactory();
    }
    public CakeFactory createCakeFactory() {
        return new MousseFactory();
    }
}

class SummerFactory implements AbstractFactory {
    public FruitFactory createFruitFactory() {
        return new StrawberryFactory();
    }
    public CakeFactory createCakeFactory() {
        return new CheesecakeFactory();
    }
}

public class AbstractFactoryTest {
    public static void main(String[] args) {
        AbstractFactory factory;
        Fruit fruit;
        Cake cake;

        factory = new SpringFactory();
        fruit = factory.createFruitFactory().createFruit();
        cake = factory.createCakeFactory().createCake();
        fruit.produce();
        cake.make();

        factory = new SummerFactory();
        fruit = factory.createFruitFactory().createFruit();
        cake = factory.createCakeFactory().createCake();
        fruit.produce();
        cake.make();
    }
}

三、結語

Factory Method模式是一種非常重要的設計模式,它可以幫助我們將對象的創建過程抽離出來,並實現業務邏輯和對象創建的松耦合。Factory Method模式有多種實現方式,每一種實現方式都有自己的特點和局限性,我們可以根據需要選擇合適的實現方式。

原創文章,作者:IPOK,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/141856.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
IPOK的頭像IPOK
上一篇 2024-10-09 09:52
下一篇 2024-10-09 09:53

相關推薦

  • 手機安全模式怎麼解除?

    安全模式是一種手機自身的保護模式,它會禁用第三方應用程序並使用僅限基本系統功能。但有時候,安全模式會使你無法使用手機上的一些重要功能。如果你想解除手機安全模式,可以嘗試以下方法: …

    編程 2025-04-28
  • Qt State Machine與狀態機模式

    本文將介紹Qt State Machine和狀態機模式在Qt中的實現。Qt提供了QStateMachine和QState兩個類,可以方便地實現狀態機模式,並且能有效地處理複雜的、多…

    編程 2025-04-27
  • 顯示C++設計模式

    本文將詳細介紹顯示C++設計模式的概念、類型、優點和代碼實現。 一、概念 C++設計模式是在軟件設計階段定義,用於處理常見問題的可重用解決方案。這些解決方案是經過測試和驗證的,並已…

    編程 2025-04-27
  • Centos7進入單用戶模式的解釋

    本文將介紹如何在Centos7中進入單用戶模式,並從以下幾個方面進行詳細的闡述。 一、Centos7進入單用戶模式的解答 在Centos7中進入單用戶模式需要執行以下步驟: 1. …

    編程 2025-04-27
  • Java中通過method獲取類上的泛型

    本文將從以下幾個方面詳細闡述如何通過Java Method獲取類上的泛型: 一、方法的定義與用途 首先,我們需要了解方法的定義與用途。在Java編程中,方法是類中的一種行為,用於實…

    編程 2025-04-27
  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25

發表回復

登錄後才能評論