java - 使用spring框架我们在哪里实现松耦合呢?

标签 java spring factory-pattern

我正在学习spring框架。我从网站上阅读了很多教程,但我无法得到他们的解释。请用简单、简短的方式向我解释一下。

  1. 这里我提出了Factory设计模式来实现松耦合,以及我们如何在Spring中使用这种设计模式。

  2. 我无法理解这一点(句子)“此模式提供了创建对象的最佳方法之一”。

    public interface Shape {
           void draw();
        }
    
    public class Rectangle implements Shape {
    
       @Override
       public void draw() {
          System.out.println("Inside Rectangle::draw() method.");
       }
    }
    
    public class Square implements Shape {
    
       @Override
       public void draw() {
          System.out.println("Inside Square::draw() method.");
       }
    }
    
    public class Circle implements Shape {
    
       @Override
       public void draw() {
          System.out.println("Inside Circle::draw() method.");
       }
    }
    
    public class ShapeFactory {
    
       //use getShape method to get object of type shape 
       public Shape getShape(String shapeType){
          if(shapeType == null){
             return null;
          }     
          if(shapeType.equalsIgnoreCase("CIRCLE")){
             return new Circle();
    
          } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
             return new Rectangle();
    
          } else if(shapeType.equalsIgnoreCase("SQUARE")){
             return new Square();
          }
    
          return null;
       }
    }
    
    public class FactoryPatternDemo {
    
       public static void main(String[] args) {
          ShapeFactory shapeFactory = new ShapeFactory();
    
          //get an object of Circle and call its draw method.
          Shape shape1 = shapeFactory.getShape("CIRCLE");
    
          //call draw method of Circle
          shape1.draw();
    
          //get an object of Rectangle and call its draw method.
          Shape shape2 = shapeFactory.getShape("RECTANGLE");
    
          //call draw method of Rectangle
          shape2.draw();
    
          //get an object of Square and call its draw method.
          Shape shape3 = shapeFactory.getShape("SQUARE");
    
          //call draw method of circle
          shape3.draw();
       }
    }
    

    输出:

    Inside Circle::draw() method.
    Inside Rectangle::draw() method.
    Inside Square::draw() method.
    

最佳答案

在这里,您使用了一个经典工厂,它在每次调用时创建新实例。 但工厂忽略了两点:getShape() 应该提供静态方法,并且工厂类不应该被实例化多次。

对工厂类使用单例和静态方法会带来一些缺点:测试期间的模拟更加复杂,工厂增加了其职责(它是单例,但也是工厂类),在应用程序的类之间创建了高耦合:客户端类和工厂。
Spring 提供的依赖注入(inject)(但它不是唯一的)解决了这些问题。
Spring 确实扮演了工厂的角色,单例问题也由 Spring 处理。

在 Spring 中,您可以通过一些方法来完成类似的事情:

  • 使用工厂 bean 和工厂方法。您有一个 XML 版本和一个 Java 版本。
    XML 方式就是 XML 方式:冗长,如果您更喜欢直接注释您的类而不是创建间接读取使用的 Spring 配置,则不一定适合。
    Java 版本没有间接缺点,但它有点冗长,因为您的工厂类必须实现 Spring FactoryBean 接口(interface)。

  • 使用带有 prototype 范围注释的经典 Spring bean。

Spring 中的等价物可能是:

@Bean
@Scope("prototype")
public Shape shape(String shapeType) {   
      if(shapeType == null){
         return null;
      }     
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();

      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();

      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }

      return null;   
}

无论如何,您应该使用 BeanFactoryApplicationContextObject getBean(String name, Object...args) 方法来传输 shapeType 参数。

例如:

Shape shape = (Shape) applicationContext.getBean("shape", "CIRCLE");

关于java - 使用spring框架我们在哪里实现松耦合呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43556279/

相关文章:

c++ - 工厂方法是否适合我的问题?

c++ - Factory 类的典型 C++ 实现是否存在缺陷?

java - 我有一个随机颜色程序,我需要它快速切换颜色

java - 使用 .java 文件的路径和外部 jar 的路径在 linux 中编译和运行 java 程序

java - 将 GitHub 库导入现有 Eclipse 项目

java - org.springframework.web.client.HttpClientErrorException : 400 null

java - JDBC 连接 : Not Recconecting after Timeout

java - Spring异常时事务回滚

java - Spring webflow 是一个好的 portlet 技术选择吗?

wcf - 如何将值传递给 wcf 服务上的构造函数?