工廠模式解耦

在這裏插入圖片描述

持久層接口及實現類
//持久層接口
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;
    }*/
}

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