博客
关于我
设计模式【2.1】-- 简单工厂模式怎么演变成工厂方法模式?
阅读量:398 次
发布时间:2019-03-05

本文共 2198 字,大约阅读时间需要 7 分钟。

工厂方法模式详解

在前几篇文章中,我们已经学习了简单工厂模式的实现方法。今天,我们将探讨另一种常见的设计模式——工厂方法模式。这个模式在软件开发中非常有用,尤其是在需要创建多种不同产品的情况下。

背景介绍

工厂方法模式的核心思想是:通过抽象工厂接口,为每个具体的产品类型提供对应的工厂类。这样一来,每个工厂类负责创建特定类型的产品,而调用者只需要通过抽象工厂接口调用相应的工厂方法来获取所需的产品。

工厂接口的定义

首先,我们定义一个抽象工厂接口 FruitFactory,这个接口包含一个 getFruit 方法,返回一个 Fruit 类型的对象。

public interface FruitFactory {    public Fruit getFruit();}

水果的具体实现

接下来,我们定义水果的具体实现类。每个水果类都必须实现 Fruit 接口,并提供自己的 process 方法,用于展示该水果的特性。

public class Apple implements Fruit {    public void process() {        System.out.println("I am an Apple");    }}public class Pear implements Fruit {    public void process() {        System.out.println("I am a Pear");    }}public class Orange implements Fruit {    public void process() {        System.out.println("I am an Orange");    }}

具体工厂实现

对于每种水果,我们都需要一个对应的工厂类,这些工厂类实现 FruitFactory 接口,并提供 getFruit 方法来返回对应的水果实例。

public class AppleFactory implements FruitFactory {    public Fruit getFruit() {        return new Apple();    }}public class PearFactory implements FruitFactory {    public Fruit getFruit() {        return new Pear();    }}public class OrangeFactory implements FruitFactory {    public Fruit getFruit() {        return new Orange();    }}

测试代码

为了验证我们的实现是否正确,我们编写一个测试类 FruitTest,创建不同类型的工厂,并调用它们的 getFruit 方法来获取相应的水果实例,然后执行 process 方法。

public class FruitTest {    public static void main(String[] args) {        FruitFactory appleFactory = new AppleFactory();        Fruit apple = appleFactory.getFruit();        apple.process();                FruitFactory pearFactory = new PearFactory();        Fruit pear = pearFactory.getFruit();        pear.process();    }}

工厂方法模式的角色分析

工厂方法模式由以下几个角色组成:

  • 抽象工厂(Abstract Factory):提供了创建产品的接口,调用者通过它访问具体工厂的工厂方法 getFruit() 来创建水果产品。

  • 具体工厂(Concrete Factory):实现抽象工厂中的抽象方法,创建具体的产品。每个具体工厂对应一种产品类型。

  • 抽象产品(Abstract Product):定义了产品规范,例如所有水果共同的特性。

  • 具体产品(Concrete Product):实现了抽象产品角色所定义的接口,由具体工厂来创建,且与具体工厂一一对应。

  • 工厂方法模式与简单工厂模式的区别

    工厂方法模式与简单工厂模式的主要区别在于:

    • 一对一的关系:工厂方法模式中,每个工厂对应一种产品,各司其职。这种一对一的关系使得当需要扩展新的产品类型时,只需添加新的工厂类和相应的产品实现类即可。

    • 可扩展性:对于产品类型较多的情况,工厂方法模式比简单工厂模式更容易扩展,因为只需要添加新的工厂和产品类,而无需修改现有的代码。

    • 维护方便:每个工厂类只负责创建一种产品,代码结构清晰,方便维护和理解。

    总结

    工厂方法模式通过将产品的创建过程从具体的产品类中解耦,使得系统更加灵活和可扩展。通过定义抽象工厂接口和具体工厂类,我们可以轻松地添加新的产品类型,而无需对现有的代码进行修改。这种设计模式在面对多种产品类型且创建过程复杂的情况下,非常有用。

    转载地址:http://mdmzz.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>