工厂模式解耦

在这里插入图片描述

持久层接口及实现类
//持久层接口
public interface IAccountDao {
    /**
     * 模拟保存方法
     */
    void saveAccount();
}
//持久层实现类
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存账户");
    }
}
业务层接口及实现类
//业务层接口
public interface IAccountService {

    /**
     * 模拟保存
     */
    void saveAccount();
}
//业务层实现类
public class AccountImpl implements IAccountService {
    //private IAccountDao iAccountDao = new AccountDaoImpl();
    private IAccountDao iAccountDao = (IAccountDao) BeanFactory.getBean("accountDao");

    public void saveAccount() {
        iAccountDao.saveAccount();
    }
}
表示层
//表示层
public class Client {
    public static void main(String[] args) {
        //IAccountService iAccountService = new AccountImpl();
        IAccountService iAccountService = (IAccountService) BeanFactory.getBean("accountService");
        iAccountService.saveAccount();
    }
}
工厂类
package com.sx.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;

/**
 * @Date 2019/7/25 - 16:39
 * <p>
 * 用于创建Bean对象的工厂
 * JavaBean:JavaBean是一种JAVA语言写成的可重用组件
 * <p>
 * 创建service和dao对象
 * 1.需要一个配置文件来配置service和dao(唯一标识=全限定类名)
 * 2.通过读取配置文件内容,反射创建对象
 */
public class BeanFactory {
    //定义一个Properties对象
    private static Properties properties;

    //定义一个Map,用于存放创建的对象,称之为容器
    private static Map<String, Object> beans;

    //使用静态代码块为Properties赋值
    static {
        try {
            //实例化对象
            properties = new Properties();
            //获取properties文件的流对象
            InputStream inputStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            properties.load(inputStream);
            //实例化容器
            beans = new HashMap<>();
            //取出配置文件中所有的key
            Enumeration keys = properties.keys();
            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个key
                String key = keys.nextElement().toString();
                //根据key获取value
                String beanPath = properties.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);
    }

   /* public static Object getBean(String beanName) {
        Object bean = null;
        String beanPath = properties.getProperty(beanName);
        try {
            bean = Class.forName(beanPath).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }*/
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章