曾经案例中问题 与 工厂模式解耦
生活随笔
收集整理的這篇文章主要介紹了
曾经案例中问题 与 工厂模式解耦
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個創建Bean對象的工廠
* Bean:在計算機英語中,有可重用組件的含義。
* JavaBean:用java語言編寫的可重用組件。
*????? javabean >? 實體類
*
*?? 它就是創建我們的service和dao對象的。
*
*?? 第一個:需要一個配置文件來配置我們的service和dao
*?????????? 配置的內容:唯一標識=全限定類名(key=value)
*?? 第二個:通過讀取配置文件中配置的內容,反射創建對象
*
*?? 我的配置文件可以是xml也可以是properties
bean.properties
accountService=com.itheima.service.impl.AccountServiceImpl accountDao=com.itheima.dao.impl.AccountDaoImplBeanFactory.java
package com.itheima.factory;import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties;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>();//取出配置文件中所有的KeyEnumeration keys = props.keys();//遍歷枚舉while (keys.hasMoreElements()){//取出每個KeyString key = keys.nextElement().toString();//根據key獲取valueString beanPath = props.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);}/*** 根據Bean的名稱獲取bean對象* @param beanName* @returnpublic static Object getBean(String beanName){Object bean = null;try {String beanPath = props.getProperty(beanName); // System.out.println(beanPath);bean = Class.forName(beanPath).newInstance();//每次都會調用默認構造函數創建對象}catch (Exception e){e.printStackTrace();}return bean;}*/ }IAccountDao.java
package com.itheima.dao;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 模擬保存賬戶*/void saveAccount(); }AccountDaoImpl.java
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao;/*** 賬戶的持久層實現類*/ public class AccountDaoImpl implements IAccountDao {public void saveAccount(){System.out.println("保存了賬戶");} }IAccountService.java
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.factory.BeanFactory; import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService {// private IAccountDao accountDao = new AccountDaoImpl();private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");// private int i = 1;public void saveAccount(){int i = 1;accountDao.saveAccount();System.out.println(i);i++;} }Client.java
package com.itheima.ui;import com.itheima.factory.BeanFactory; import com.itheima.service.IAccountService;/*** 模擬一個表現層,用于調用業務層*/ 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();}} }總結
以上是生活随笔為你收集整理的曾经案例中问题 与 工厂模式解耦的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序的耦合及解耦
- 下一篇: 使用 spring 的 IOC 解决程序