一、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