Spring(1)——初识程序的耦合性和工厂模式解耦

 

  • 1.实现利用JDBC连接数据库分析程序的耦合性
package com.itheima.jdbc;
import java.sql.*;
/*
说明程序的耦合
* */
/*
* 解耦的思路:
* 1.不用new的方式创建对象,使用反射原理来创建对象
* 2.通过读取配置文件来获取要创建对象的全限定类名
* */
//下面的代码如果不依赖com.mysql.jdbc.Driver()没办法编译
public class JdbcDemo1 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        1.注册驱动
//        DriverManager.registerDriver(new com.mysql.jdbc.Driver());//必须依赖com.mysql.jdbc.Driver()整个代码才能运行
        Class.forName("com.mysql.jdbc.Driver");
//        2.获取连接
          Connection conn = DriverManager.getConnection("jdbc.mysql://localhost:3306/eesy","root","123456");
//        3.获取操作数据库的预处理对象
        PreparedStatement pstm = conn.prepareStatement("select * from account");
//        4.执行SQL,得到结果
        ResultSet rs = pstm.executeQuery();
//        5.遍历结果
        while (rs.next()){
            System.out.println(rs.getString("name"));
        }
//        6.释放资源
        rs.close();
        pstm.close();
        conn.close();
    }
}
//以上过程中自定义一个eesy的数据库 
  •  2. 用一个例子解释各对象之间的依赖关系,高耦合度性。
/*
 * 账户业务层的接口
 * */
public interface IAccountService {
    /*
     * 模拟账户保存
     * */
    void saveAccount();
}
/*
* 账户业务层实现类
* */
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao = new AccountDaoImpl();
    public void saveAccount() {
        accountDao.saveAccount();
    }
}
/*
* 账户的持久层接口
* */
public interface IAccountDao {
    /*
    * 模拟保存账户
    * */
    void saveAccount();
}
/*
* 账户的持久层实现类
* */
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存了账户");
    }
}
/*
* 模拟一个表现层调用业务层
* */
public class Client {
    public static void main(String[] args) {
        IAccountService as = new AccountServiceImpl();
        as.saveAccount();
    }
}
  •   2.1 从上面的例子中可以看到依赖关系及解耦合的办法:
//账户的业务实现类

//    方法一:创建类需要依赖接口
private IAccountDao accountDao = new AccountDaoImpl();

 //  方法二:不依赖接口,从Bean配置文件中得到对象
 private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");
  • 2.2在表现层中通过Bean文件调用业务层:
    public static void main(String[] args) {
      //IAccountService as = new AccountServiceImpl();
        for(int i=0;i<5;i++) {
            IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
            System.out.println(as);
            as.saveAccount();
        }
    }
  • (创建Bean文件)——使用工厂模式创建对象从而降低耦合性:
/*
 * 一个创建Bean对象的工厂
 * Bean:可重用组件的含义
 * JavaBean: 和实体类不等,JavaBean>实体类
 *   创建service和dao对象
 *
 * 第一步:需要一个配置文件,配置service和dao
 *       配置内容:唯一标识=全限定类名(key-value)
 * 第二步:通过读取配置文件中的内容,反射创建对象
 *
 * 配置文件有两种:xml and properties
 * */
public class BeanFactory {
    //定义一个Properties对象
    private static Properties props;
    /*
     * 使用静态代码块为Properties对象赋值
     * */
    static {
        try {
            //实例化
            props = new Properties();
            //获取Properties文件的流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
        }catch (Exception e){
            throw  new ExceptionInInitializerError("初始化properties失败!");
        }
    }
    /*
    * 根据bean的名称获取bean对象
    * @param beanName
    * @return
    * */
    public Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Object bean = null;
        try {
            String beanPath = props.getProperty(beanName);
            bean = Class.forName(beanPath).newInstance();
        }catch(Exception e){
            e.printStackTrace();
        }
        return bean;
    }
}

 

posted @ 2019-11-06 16:06  YrZf  阅读(130)  评论(0)    收藏  举报