codecamp

春天教程 - 春天松散耦合

春天教程 - 春天松散耦合


例子...

项目代码

我们将创建一个有几个类的项目。项目将以不同的格式输出数据。我们可以选择以CSV格式或JSON格式输出数据。

我们将创建一个有几个类的项目。项目将以不同的格式输出数据。我们可以选择以CSV格式或JSON格式输出数据。...

package com.java2s.output;
public interface Printer
{
  public void print();
}

之后,我们将创建CSV打印机,将输出CSV格式的数据。CSV打印机实现打印机接口。

package com.java2s.output.impl;
import com.java2s.output.Printer;
public class CSVPrinter implements Printer
{
  public void print(){
    System.out.println("Csv Output Printer");
  }
}

然后是时间创建JSON打印机将输出JSON格式的消息。JSON打印机还实现了打印机接口。

package com.java2s.output.impl;
import com.java2s.output.Printer;
public class JSONPrinter implements Printer
{
  public void print(){
    System.out.println("Json Output Printer");
  }
}


夫妇代码

我们有几种方法来使用CSVPrinter或JSONPrinter。 首先我们可以直接调用它。

package com.www.w3cschool.cnmon;
import com.java2s.output.Printer;
import com.java2s.output.impl.CSVPrinter;
public class App 
{
    public static void main( String[] args )
    {
      Printer output = new CSVPrinter();
      output.print();
    }
}

这样很容易创建CSVPrinter。 但是如果我们想要改变源代码的话切换到JSONPrinter,我们将不得不更改源代码并重新编译。

对于上面的代码,它很容易改变,因为它有两行代码。 假设我们有成千上万代码和CSVPrinter已被声明了几百次。



春天

通过使用Spring依赖注入(DI),我们可以在Spring配置XML文件中声明Java Bean。 然后在xml文件中连接Java Bean。这样Spring可以使我们的打印机松散耦合到不同的打印机实现。

我们更改Helper类以接受打印机。

package com.java2s.output;
import com.java2s.output.Printer;
public class OutputHelper
{
  Printer outputGenerator;
  public void print(){
    outputGenerator.print();
  }
  public void setOutputGenerator(Printer outputGenerator){
    this.outputGenerator = outputGenerator;
  }
}

然后我们要创建一个Spring bean配置文件并在此处声明所有Java对象依赖关系。

<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.java2s.output.OutputHelper">
    <property name="outputGenerator" ref="csvPrinter" />
  </bean>
  <bean id="csvPrinter" class="com.java2s.output.impl.CSVPrinter" />
  <bean id="jsonPrinter" class="com.java2s.output.impl.JSONPrinter" />
</beans>

通过Spring调用它

package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2s.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.print();
    }
}

要切换打印机,我们只需要为不同的打印机更改Spring XML文件。当Printer更改时,我们需要修改Spring XML文件。

Spring教程 - Spring构造函数注入
Spring教程 - Spring Ref Bean
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }