IOC是Inversion of Control的缩写,控制反转的含义。表示对象控制权的转移,将对象建立、销毁等移交到Ioc容器来管理,使用该对象的调用者,也须要到Ioc容器中获取该对象。
下面咱们就开始建立本身的Ioc容器来管理和建立对象了。java
dao层:数据访问层
service层:业务层
ui层:暴露给外部使用的层spring
(1)dao层的代码:
接口定义:框架
public interface IAccountDao { void saveAccount(); }
实现类:maven
package org.study.dao.impl; import org.study.dao.IAccountDao; public class AccountDaoImpl implements IAccountDao { @Override public void saveAccount() { System.out.println("保存了..."); } }
(2)service层代码
接口类:ide
package org.study.service; public interface IAccountService { void saveAccount(); }
实现类:学习
package org.study.service.impl; import org.study.dao.IAccountDao; import org.study.dao.impl.AccountDaoImpl; import org.study.service.IAccountService; public class AccountServiceImpl implements IAccountService { @Override public void saveAccount() { IAccountDao accountDao=new AccountDaoImpl(); accountDao.saveAccount(); } }
(3)ui层代码ui
package org.study.ui; import org.study.service.IAccountService; import org.study.service.impl.AccountServiceImpl; public class Client { public static void main(String[] args) { IAccountService accountService = new AccountServiceImpl(); accountService.saveAccount(); } }
(4)代码编写完成后,咱们的代码目录、以及最终运行效果以下图:
代码目录:
运行结果:
idea
咱们的代码如期地正确运行了,可是问题也比较明显,代码耦合度高,ui层依赖于service层,service层依赖与dao层。
有没有好的办法,能下降咱们模块间的耦合呢,下面咱们经过工厂模式建立对象,下降对象间的耦合。code
(一)编写配置文件bean.properties,将要管理的类放入配置文件中对象
accountDao=org.study.dao.impl.AccountDaoImpl accountService=org.study.service.impl.AccountServiceImpl
(二)编写beanFactory类,读取配置文件,并建立对象,代码以下
package org.study.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 final Map<String,String> beanProperties= new HashMap<>(); static { InputStream inputStream= beanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); Properties properties =new Properties(); try { properties.load(inputStream); Enumeration<String> names =(Enumeration<String> )properties.propertyNames(); while (names.hasMoreElements()) { String key = names.nextElement(); String value = properties.getProperty(key); beanProperties.put(key, value); } } catch (IOException e) { e.printStackTrace(); }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public Object getBean(String name){ String value = beanProperties.get(name); if (value!=null){ try { return Class.forName(value).newInstance(); } catch (Exception e) { e.printStackTrace(); } } return null; } }
该类提供了一个方法getBean,该方法经过key来建立对象;
改造后的代码层次以下:
(1)改造AccountServiceImpl的类,经过beanFactory建立accountDao,代码以下:
package org.study.service.impl; import org.study.dao.IAccountDao; import org.study.factory.beanFactory; import org.study.service.IAccountService; public class AccountServiceImpl implements IAccountService { @Override public void saveAccount() { IAccountDao accountDao=(IAccountDao) beanFactory.getBean("accountDao"); accountDao.saveAccount(); } }
(2)改造ui层的类,经过beanFactory建立accountService,代码以下:
IAccountService accountService = (IAccountService) beanFactory.getBean("accountService"); accountService.saveAccount();
运行代码,结果以下:
1、第一种方式编写代码是咱们常见的方式,缺点是代码间的耦合度高,一个类强依赖于另外一个类。 2、第二种方式,咱们将对象的建立都放到工厂类里来执行,类之间只依赖于接口,不依赖于具体的实现;经过此方式,咱们下降了对象之间的耦合,能够随时修改接口的实现类;缺点是,对象的建立过程变得更复杂了,不那么直观。 3、beanFactory是一个简单的ioc容器类,咱们的对象都经过这个容器建立,功能也很单一。 下一篇,咱们开始引入spring框架帮咱们管理对象,让咱们一块儿学习spring中的ioc。