互联网科技JavaJava 杂谈

Spring 程序间的耦合和解耦

2019-07-19  本文已影响2人  java欧阳丰

1、耦合

大家可以看以下代码,jdbc的注册驱动以及连接数据库:
在这里的第一行代码,大家可以看到,这就有很大的耦合性关系,因为DriverManager依赖于new com.mysql.jdbc.Driver()对象,connection对象依赖于DriverManager,大家可以尝试一下,如果你把msql的jar包去掉,那么运行时,在编译器就出现错误。而无法到达运行期。这里的耦合说的就是一个类依赖于另一个类,如果另一个类凉了,那么这个类就会在编译器出错。

DriverManager.registerDriver(new com.mysql.jdbc.Driver())
Connection conn =  DriverManager.getConnection(url,username,password);

所以呢,我们在之前的例子中都是这种注册驱动,看下面的代码,这种反射的方式不再是创建一个新对象,避免使用new关键字,而是用字符串表示。这样就降低了耦合性。在编译器不会出错。

Class.forName("com.mysql.jdbc.Driver");

耦合是指程序间的依赖关系
它包括 :类之间的依赖、方法之间的依赖
解耦:降低程序之间的依赖关系
实际开发中:编译器不依赖、运行时才依赖。
解耦思路:
第一步,使用反射来创建对象,而避免使用new关键字
第二部:通过读取配置文件来获取创建对象的全限定类名

2、举例

项目目录

先举一个耦合性的模拟保存小例子
目录结构:分别是持久层、业务层、表现层。


IAccountDao接口

package com.spring.dao;
/**
 * 持久层
 */
public interface IAccountDao {
    void saveAccount();
}

IAccountDaoImpl实现类

package com.spring.dao.Impl;
import com.spring.dao.IAccountDao;
public class IAccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存");
    }
}

IAccountService接口

package com.spring.service;
import com.spring.domain.Account;
/**
 * 业务层
 */
public interface IAccountService {
    void saveAccount(Account account);
}

IAccountServiceImpl实现类

package com.spring.service.Impl;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
public class IAccountServiceImpl implements IAccountService {
    private IAccountDaoImpl accountDao = new IAccountDaoImpl();
    public void saveAccount(Account account) {
        accountDao.saveAccount();
    }
}

Account类就不写了 就是一个简单的类
Client表现层

package com.spring.ul;
import com.spring.domain.Account;
import com.spring.service.Impl.IAccountServiceImpl;
/**
 * 表现层
 */
public class Client {
    public static void main(String[] args) {
        IAccountServiceImpl accountService = new IAccountServiceImpl();
        accountService.saveAccount(new Account());
    }
}

在这里表现层调用业务层时,使用了new关键字,业务层调用持久层时,也使用了new关键字。这里就有耦合性。接下来我们就去解决这个问题。

解决耦合性

创建一个BeanFactory类。

1、项目目录

增加的类。

2、BeanFactory类

在这里我们用类加载器去读取配置文件,而不用绝对路径和相对路径。之后我们创建一个Map容器,用于读取配置文件中所有的对象的全限定类名,再利用反射机制创建对象。这个容器保证了无论利用工厂创建多个对象,它永远指向一个对象,也就是单例思想。

package com.spring.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    private static Properties props;
    private  static Map<String,Object> beans;
    static{
        props = new Properties();
        //获取文件输入流对象
        InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            props.load(in);
//            实例化容器
            beans = new HashMap<String, Object>();
//            获取配置文件中的key
            Enumeration<Object> keys = props.keys();
//            遍历枚举
            while(keys.hasMoreElements()){
                String key = keys.nextElement().toString();
                String beanPath = props.getProperty(key);
                Object value = Class.forName(beanPath).newInstance();
                beans.put(key,value);
            }

        } catch (Exception e) {
            throw new ExceptionInInitializerError("初始化properties失败!!");
        }
    }
    public static Object getBean(String beanName){
        return beans.get(beanName);
    }
}

3、配置文件信息

accountService=com.spring.service.Impl.IAccountServiceImpl
accountDao=com.spring.dao.Impl.IAccountDaoImpl

4、Client类

package com.spring.ul;

import com.spring.domain.Account;
import com.spring.factory.BeanFactory;
import com.spring.service.IAccountService;
import com.spring.service.Impl.IAccountServiceImpl;
/**
 * 表现层
 */
public class Client {
    public static void main(String[] args) {
        IAccountServiceImpl accountService = (IAccountServiceImpl) BeanFactory.getBean("accountService");
        accountService.saveAccount(new Account());
    }
}

5、运行结果

利用工厂模式加反射机制极大的降低了程序间的耦合性。但是请大家注意,程序间的耦合性不能完全消除。



作者:Zesystem
原文:https://blog.csdn.net/weixin_44588495/article/details/92793213

不求打赏 只求关注和点赞

上一篇 下一篇

猜你喜欢

热点阅读