zoukankan      html  css  js  c++  java
  • 如何减少程序间的耦合度?_DI与接口

    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层)和业务层的耦合度





  • 相关阅读:
    字节跳动--今日头条iOS客户端启动速度优化
    RSA加密
    几种浏览器
    Linux定时任务crontab无法执行
    Python报错ImportError: No Module Named Typing的解决
    微信小程序:A、B两个小程序相互跳转,出现点击A小程序底部导航栏菜单,第一次点击无法跳转B小程序,需要点击第二次才会触发跳转到B小程序
    c# core 生成随机图文验证码
    携程Apollo统一配置管理中心
    WPF程序中嵌入winForm窗体
    sqlserver 转 postgresql
  • 原文地址:https://www.cnblogs.com/Black-YeJing/p/9131128.html
Copyright © 2011-2022 走看看