目录
程序间的依赖关系
包括:
类之间的依赖。
方法间的依赖。
降低程序间的依赖关系。
应该做到编译期不依赖,运行时才依赖。
public interface IAccountDao {
/**
* 模拟保护账户
*/
void saveAccount();
}
实现类
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println("保存了");
}
}
public interface IAccountService {
/**
* 模拟保护账户
*/
void saveAccount();
}
实现类
这里有着很强的依赖关系
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao=new AccountDaoImpl();
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
IAccountService as=new AccountServiceImpl();
as.saveAccount();
}
}
使用工厂模式解耦
一个创建Bean对象的工厂
Bean:在计算机英语中,有可重用组件的含义。
JavaBean: java语言编写的可重用组件* javabean > 实体类 javabean不等于实体类,要远大于实体类
第一个:需要一个配置文件来配置service和dao,配置的内容:唯一标识=全限定类名(key=value)
第二个:通过读取配置文件中配置内容,反射创建对象** 配置
文件可以是 xml 也可以是 properties
/**
* 描述:
* 〈一个创建Bean对象的工厂〉
* Bean:在计算机英语中,有可重用组件的含义。
* JavaBean: java语言编写的可重用组件
* javabean > 实体类 javabean不等于实体类,要远大于实体类
*
* 第一个:需要一个配置文件来配置service和dao
* 配置的内容:唯一标识=全限定类名(key=value)
* 第二个:通过读取配置文件中配置内容,反射创建对象
*
* 配置文件可以是 xml 也可以是 properties
*
* @author zuiren
* @create 2019/8/22
* @since 1.0.0
*/
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 bean 名称
* @return
*/
public static Object getBean(String beanName){
Object bean=null;
try {
String beanPath=props.getProperty(beanName);
//每次都会调用默认构造函数创建对象
bean=Class.forName(beanPath).newInstance();
}catch (Exception e){
e.printStackTrace();
}
return bean;
}
}
accountService=service.impl.AccountServiceImpl
accountDao=dao.impl.AccountDaoImpl
public class AccountServiceImpl implements IAccountService {
// private IAccountDao accountDao=new AccountDaoImpl();
private IAccountDao accountDao= (IAccountDao) BeanFactory.getBean("accountDao");
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
// IAccountService as=new AccountServiceImpl();
IAccountService as= (IAccountService) BeanFactory.getBean("accountService");
as.saveAccount();
}
}
此刻我们可以正常运行,而且没有错误
当我们将 AccountServiceImpl 删除时,编译环境没有错误显示,而运行时显示错误,这就是工厂模式解耦
public class Client {
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();
}
}
}
运行结果
service.impl.AccountServiceImpl@1540e19d
保存了
service.impl.AccountServiceImpl@677327b6
保存了
service.impl.AccountServiceImpl@14ae5a5
保存了
service.impl.AccountServiceImpl@7f31245a
保存了
service.impl.AccountServiceImpl@6d6f6e28
保存了
可以看出现在是多例模式,每次运行对象都会创建一次
public class BeanFactory {
/**
* 定义一个 Properties 对象
*/
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 的名称获取 bean 对象
* @param beanName bean 名称
* @return
*/
public static Object getBean(String beanName){
return beans.get(beanName);
}
}
运行主函数
service.impl.AccountServiceImpl@1540e19d
保存了
service.impl.AccountServiceImpl@1540e19d
保存了
service.impl.AccountServiceImpl@1540e19d
保存了
service.impl.AccountServiceImpl@1540e19d
保存了
service.impl.AccountServiceImpl@1540e19d
保存了
原文:https://www.cnblogs.com/zuiren/p/11415411.html