java Spring loosely coupled efficient application of simple case analysis

  • 2020-05-19 04:49:50
  • OfStack

java Spring loose coupling

The object-oriented concept is a good design to break the system into 1 group of reusable objects. However, as the system gets larger, especially in the Java project, the large object dependencies cause 1 to be tightly coupled causing objects to be difficult to manage or modify. In this case, the Spring framework can be used as a core module to easily and efficiently manage all object dependencies.

Output generator example

Let's look at an example. Suppose your project has a function output in CSV or JSON format. Your code might look like this:


File : IOutputGenerator.java  �   Output generator interface 

package com.yiibai.output;

public interface IOutputGenerator
{
 public void generateOutput();
}
File : CsvOutputGenerator.java  �  1 a CSV Output generators are used for implementation IOutputGenerator Interface. 

package com.yiibai.output.impl;

import com.yiibai.output.IOutputGenerator;

public class CsvOutputGenerator implements IOutputGenerator
{
 public void generateOutput(){
 System.out.println("Csv Output Generator");
 }
}
File : JsonOutputGenerator.java  �  1 a JSON Output generators are used for implementation IOutputGenerator Interface. 

package com.yiibai.output.impl;

import com.yiibai.output.IOutputGenerator;

public class JsonOutputGenerator implements IOutputGenerator
{
 public void generateOutput(){
 System.out.println("Json Output Generator");
 }
}

There are several methods to call IOutputGenerator, and how to use Spring to keep objects from binding to each other.

1. Method 1. Direct call

The normal way, call it directly.


package com.yiibai.common;

import com.yiibai.output.IOutputGenerator;
import com.yiibai.output.impl.CsvOutputGenerator;
/* http://www.manongjc.com/article/1602.html */
public class App 
{
  public static void main( String[] args )
  {
   IOutputGenerator output = new CsvOutputGenerator();
   output.generateOutput();
  }
}

There is a problem

In this way, the problem is that "output" is tightly coupled to CsvOutputGenerator, and every change produced by the output may involve a code change. If this code is scattered throughout your project, you will suffer every change in output generation.

Method 2 calls it with the helper class

You might want to create a helper class to implement all the output inside the class.


package com.yiibai.output;

import com.yiibai.output.IOutputGenerator;
import com.yiibai.output.impl.CsvOutputGenerator;

public class OutputHelper
{
 IOutputGenerator outputGenerator;
 
 public OutputHelper(){
 outputGenerator = new CsvOutputGenerator();
 }
 
 public void generateOutput(){
 outputGenerator.generateOutput();
 }
 
}

Call it through the helper class


package com.yiibai.common;

import com.yiibai.output.OutputHelper;

public class App 
{
  public static void main( String[] args )
  {
   OutputHelper output = new OutputHelper();
   output.generateOutput(); 
  }
}

There is a problem

This looks more elegant than before, with only one helper class to manage, but the helper class is still tightly coupled to CsvOutputGenerator, and each change produced by the output still involves small code changes.

Method 3. Hang Spring

In this case, Spring dependency injection (DI) is a good choice. Spring allows output generation to be loosely coupled to the output generator.

Smaller modifications to the OutputHelper class.


package com.yiibai.output;

import com.yiibai.output.IOutputGenerator;

public class OutputHelper
{
 IOutputGenerator outputGenerator;
 
 public void generateOutput(){
 outputGenerator.generateOutput();
 }
 
 public void setOutputGenerator(IOutputGenerator outputGenerator){
 this.outputGenerator = outputGenerator;
 }
}

Create a configuration file for Spring bean and declare the dependencies for all Java objects here.


<!-- Spring-Common.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="OutputHelper" class="com.yiibai.output.OutputHelper">
 <property name="outputGenerator" ref="CsvOutputGenerator" />
 </bean>
 
 <bean id="CsvOutputGenerator" class="com.yiibai.output.impl.CsvOutputGenerator" />
 <bean id="JsonOutputGenerator" class="com.yiibai.output.impl.JsonOutputGenerator" />
 
</beans>

Call it via Spring


package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiibai.output.OutputHelper;

public class App 
{
  public static void main( String[] args )
  {
   ApplicationContext context = 
    new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml"});

   OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
   output.generateOutput();
    
  }
}

Now, you just need to change the Spring XML file to use a different output generator. Modify only the Spring XML file without uncoded modification, which means fewer errors.

Conclusion: with the Spring framework - this useful feature of dependency management with dependency injection (DI) as an object, large Java project development management is more elegant, highly flexible and maintainable.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: