简单的耦合与解耦

耦合

接下来用一个案例来展示程序之间的耦合

项目结构

在这里插入图片描述

项目代码

接口类IAccountDao

public interface IAccountDao {
    void saveAccount();
}

账户的持久层实现类IAccountDaolmpl

public class AccountDaoImpl implements IAccountDao {
    public  void saveAccount(){
        System.out.println("保存了账户");
    }
}

接口类IAccountService

public interface IAccountService {
    void saveAccount();
}

账户的业务层实现类AccountServiceImpl

public class AccountServiceImpl implements IAccountService {
   private IAccountDao accountDao = new AccountDaoImpl();
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

实现类Client

public class Client {
    public static void main(String[] args) {
        IAccountService as= new AccountServiceImpl();
        as.saveAccount();
    }
}

结果展示

在这里插入图片描述

工厂模式解耦

项目结构

在这里插入图片描述

项目代码

实现类BeanFactory

public class BeanFactory {
    private static Properties props;
    //定义一个Map,用于存放我们要创建的对象。我们把它称之为容器
    private static Map<String,Object> beans;
    //使用静态代码块为Properties对象赋值
    static {
        try {
            //实例化对象
            props = new Properties();
            //获取properties文件的流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
            //实例化容器
            beans = new HashMap<String,Object>();
            //取出配置文件中所有的Key
            Enumeration keys = props.keys();
            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个Key
                String key = keys.nextElement().toString();
                //根据key获取value
                String beanPath = props.getProperty(key);
                //反射创建对象
                Object value = Class.forName(beanPath).newInstance();
                //把key和value存入容器中
                beans.put(key,value);
            }
        }catch(Exception e){
            throw new ExceptionInInitializerError("初始化properties失败!");
        }
    }

    /**
     * 根据bean的名称获取对象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return beans.get(beanName);
    }
}

配置文件Bean.properties

accountService=com.hncj.service.impl.AccountServiceImpl
accountDao=com.hncj.dao.impl.AccountDaoImpl

账户的业务层实现类AccountServiceImpl

public class AccountServiceImpl implements IAccountService {
   //private IAccountDao accountDao = new AccountDaoImpl();
    private IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

实现类Client

public class Client {
    public static void main(String[] args) {
        //IAccountService as= new AccountServiceImpl();
        IAccountService as= (IAccountService) BeanFactory.getBean("accountService");
        as.saveAccount();
    }
}

运行结果

在这里插入图片描述

对比

在初始代码中,如果删除一个文件,就会出现编译时错误
在这里插入图片描述
而在工厂模式下,则出现运行时异常
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值