spring 开发提倡接口编程,配合DI技术可以更好的减少层(程序)与层(程序)之间的解耦合

例子说明:
  任务:要求:
        1.打印机依赖纸张和墨盒
        2.纸张有A4和B5两种
        3.墨盒有彩色和黑色2种
        4.使用A4纸张和彩色墨盒打印指定内容
        5.使用B5纸张和黑色墨盒打印指定内容

7.要求使用接口

代码:

package Ink;

public interface Ink {
public String getInk(); }
package Ink;

public class Black_Ink implements  Ink{
public String getInk()
{
return "黑白";
}
}
package Ink;

public class Color_Ink implements Ink{
public String getInk()
{
return "彩色";
}
}

package Paper

package Paper;

public interface Paper {
public String getPaper();
}
package Paper;

public class A4_Paper implements Paper{
public String getPaper()
{
return "A5纸张";
}
}
package Paper;

public class A5_Paper implements  Paper{
public String getPaper()
{
return "A5纸张";
}
}

package Print

package Print;

import Paper.Paper;
import Ink.Ink; public class print {
private Ink ink;
private Paper paper; public void setInk(Ink ink) {
this.ink = ink;
} public void setPaper(Paper paper) {
this.paper = paper;
}
public void print(String context)
{
System.out.print("使用"+ink.getInk()+"在"+paper.getPaper()+"上,打印:"+context);
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd"> <bean id="A4_Paper" class="Paper.A4_Paper"></bean>
<bean id="Color_Ink" class="Ink.Color_Ink"></bean> <bean id="Print" class="Print.print">
<property name="paper">
<ref bean="A4_Paper" />
</property>
<property name="ink">
<ref bean="Color_Ink"/>
</property>
</bean> </beans>

package Test

package Test;

import Print.print;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String []args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
print p=(print)context.getBean("Print");
p.print("你好!");
}
}

通过上面的案例,我们可以体会到DI配合接口编程,确实可以减少层(web层)和业务层的耦合度

最新文章

  1. 如何在MySql中记录SQL日志记录
  2. JAVA中获取路径
  3. map学习笔记
  4. LTE Module User Documentation(翻译6)——物理误差模型、MIMO模型、天线模型
  5. .Net枚举类型小结
  6. lazyload 图片延迟加载
  7. iOS 的一点理解(一) 代理delegate
  8. 08. 删除重复&amp;海量数据
  9. redis 学习笔记——redis集群
  10. HDU 2255 奔小康赚大钱 KM算法的简单解释
  11. Mesos源码分析(2): Mesos Master的启动之一
  12. vhdl 数组
  13. 自制操作系统Antz(7)——实现内核 (上)
  14. Django的rest_framework的分页组件源码分析
  15. Javascript高级编程学习笔记(11)—— 垃圾回收机制
  16. Centos7 环境下 Python2.7 换成 Python3.7 运行 scrapy 应用所遇到的问题记录
  17. spring+quartz报错:Table &#39;XXXX.QRTZ_TRIGGERS&#39; doesn&#39;t exist
  18. [日常] Go语言圣经--作用域,基础数据类型,整型
  19. 15.C++-操作符重载、并实现复数类
  20. google自定义广告系列

热门文章

  1. USACO2018DEC GOLD
  2. JavaWeb_(MVC)管理员后台商品查询demo
  3. python学习之路(10)--难点
  4. 「HEOI 2016/TJOI 2016」求和
  5. jQuery动态添加和删除表格行
  6. 完美解决前端跨域之 easyXDM 的使用和解析
  7. git 指定自己的sshkey
  8. LeetCode 61. 旋转链表(Rotate List)
  9. HTML控件 隐藏
  10. dnSpy