一、Ioc的定义
IOC是Inversion of Control的缩写,控制反转的含义。表示对象控制权的转移,将对象创建、销毁等移交到Ioc容器来管理,使用该对象的调用者,也需要到Ioc容器中获取该对象。
下面我们就开始创建自己的Ioc容器来管理和创建对象了。
二、传统模式下代码的实现
2.1 我们使用idea创建一个空的maven项目,并建立好一个简单的三层调用目录和类,如图所示:
dao层:数据访问层
service层:业务层
ui层:暴露给外部使用的层
2.2 补全代码,使用传统的new调用的来编写代码
(1)dao层的代码:
接口定义:
public interface IAccountDao {
void saveAccount();
}
实现类:
package org.study.dao.impl;
import org.study.dao.IAccountDao;
public class AccountDaoImpl implements IAccountDao {
@Override
public void saveAccount() {
System.out.println("保存了...");
}
}
(2)service层代码
接口类:
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层代码
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)代码编写完成后,我们的代码目录、以及最终运行效果如下图:
代码目录:
运行结果:
2.3 问题分析
我们的代码如期地正确运行了,但是问题也比较明显,代码耦合度高,ui层依赖于service层,service层依赖与dao层。
有没有好的办法,能降低我们模块间的耦合呢,下面我们通过工厂模式创建对象,降低对象间的耦合。
三、使用工厂方法来管理对象、创建对象
3.1 引入beanFactory类,来读取配置文件,创建对象
(一)编写配置文件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来创建对象;
3.2 改造原来的Service层和ui层方法
改造后的代码层次如下:
(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();
运行代码,结果如下:
四、总结
一、第一种方式编写代码是我们常见的方式,缺点是代码间的耦合度高,一个类强依赖于另一个类。
二、第二种方式,我们将对象的创建都放到工厂类里来执行,类之间只依赖于接口,不依赖于具体的实现;通过此方式,我们降低了对象之间的耦合,可以随时修改接口的实现类;缺点是,对象的创建过程变得更复杂了,不那么直观。
三、beanFactory是一个简单的ioc容器类,我们的对象都通过这个容器创建,功能也很单一。
下一篇,我们开始引入spring框架帮我们管理对象,让我们一起学习spring中的ioc。