深入探究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/n/141856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
IPOKIPOK
上一篇 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

发表回复

登录后才能评论