设计模式--简单工厂方法

简介

简单工厂模式属于创建型模式,是工厂模式的一种。简单工厂模式通过定义一个工厂类,它可以根据参数的不同返回不同类的实例,被创建的实例通常都具有共同的父类,这个父类具有共有的属性和方法。因在简单工厂模式中用于创建实例的方法通常是静态方法,因此也称为静态工厂方法模式

  • Product 表示产品接口,也可用抽象类实现,封装产品间共有的属性

  • ProductA、ProductB 表示具体的实际产品

  • Factory 表示工厂,工厂根据需要来创建相应的产品

这样一来,当添加新的 ProductC 时,只需要修改工厂部分代码即可,而传统方法需要在每一个创建产品的地方都进行修改


实现

  • 产品接口
    • public interface Product {
          void display();
      }
  • 产品接口实现子类
    • 产品A类

      public class ProductA implements Product {
      
          @Override
          public void display() {
              System.out.println("This is product A");
          }
      }
    •  产品B类
      public class ProductB implements Product {
      
          @Override
          public void display() {
              System.out.println("This is product B");
          }
      }
  •  简单工厂类
    • public class Factory {
      
          /**
           * 方式一:根据名称创建具体的日志记录器
           * @param type
           * @return
           */
          public static Product createProductByType(String type){
      
              Product product = null;
              if ("ProductA".equals(type)) {
                  product = new ProductA();
              } else if ("ProductB".equals(type)) {
                  product = new ProductB();
              }
      
              return product;
          }
      
          /**
           * 通过反射创建日志记录器
           * @param cls
           * @return
           */
          public static Product createProductByClass(Class cls){
      
              Product product = null;
              try {
                  product = (Product) Class.forName(cls.getName()).newInstance();
              } catch (InstantiationException e) {
                  e.printStackTrace();
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }
      
              return product;
          }
      }
  • 测试
    • public class Test {
          public static void main(String[] args) {
      
              Product productA = Factory.createProductByType("ProductA");
              Product productB = Factory.createProductByType("ProductB");
              productA.display();
              productB.display();
      
              System.out.println("----------------------------------------");
      
              productA = Factory.createProductByClass(productA.getClass());
              productB = Factory.createProductByClass(productB.getClass());
              productA.display();
              productB.display();
          }
      }
      This is product A
      This is product B
      ----------------------------------------
      This is product A
      This is product B

优势和缺点

优势

  • 将创建实例与使用实例分开,使用者不必关心类对象创建细节,实现了解耦

缺点

  • 违背开闭原则,一旦添加新产品就得修改工厂类的逻辑,这样就会造成工厂逻辑过于复杂
  • 由于使用了静态方法,静态方法不能被继承和重写,会造成工厂类无法继承

应用场景

  • 工厂类负责创建的产品对象比较少:创建的对象较少,工厂方法中的业务逻辑不会太复杂
  • 客户端对于如何创建对象细节不关心:客户端不需要关心创建细节,只需要传输所对应的参数即可
posted @   伊文小哥  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示