Java Spring Loose coupling efficient application Simple example analysis _java

Source: Internet
Author: User
Tags generator

Java Spring Loose coupling

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

Examples of output generators

Let's take a look at an example, assuming that your project has a function that outputs the content in CSV or JSON format. Your code might look like the following example:

file:ioutputgenerator.java– Output Generator Interface

package com.yiibai.output;

Public interface Ioutputgenerator
{public
 void Generateoutput ();
}
file:csvoutputgenerator.java– a CSV output generator to implement the 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– a JSON output generator to implement the 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 ways to invoke Ioutputgenerator, and how to use Spring to prevent objects from combining tightly with each other.

1. Method of the direct call

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) c7/>{
   ioutputgenerator output = new Csvoutputgenerator ();
   Output.generateoutput ();
  }


There are problems

In this way, the problem is "output" tight to csvoutputgenerator coupling, and each change in output generated may involve code changes. If this code is dispersed in your project, every change in output generation will make you suffer.

Method 2– call it with a secondary class

You might want to create an auxiliary class that implements all the output within 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 a secondary 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 are problems

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

Method 3–spring

In this case, Spring Dependency Injection (DI) is a good choice. Spring lets the output generate 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 a Spring bean and declare the dependencies of 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 through 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 CLASSP Athxmlapplicationcontext (new string[] {"Spring-common.xml"});

   Outputhelper output = (outputhelper) context.getbean ("Outputhelper");
   Output.generateoutput ();
    
  }


Now, just change the Spring XML file using a different output builder. Modifying only the Spring XML file without uncensored modification means fewer errors.

Conclusion: The spring framework-this dependency injection (DI)-is a useful feature of dependency management for objects, making it more elegant, highly flexible, and maintainable in large Java project development management.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.