曾经案例中问题 与 工厂模式解耦
生活随笔
收集整理的這篇文章主要介紹了
曾经案例中问题 与 工厂模式解耦
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)創(chuàng)建Bean對(duì)象的工廠
* Bean:在計(jì)算機(jī)英語(yǔ)中,有可重用組件的含義。
* JavaBean:用java語(yǔ)言編寫的可重用組件。
*????? javabean >? 實(shí)體類
*
*?? 它就是創(chuàng)建我們的service和dao對(duì)象的。
*
*?? 第一個(gè):需要一個(gè)配置文件來(lái)配置我們的service和dao
*?????????? 配置的內(nèi)容:唯一標(biāo)識(shí)=全限定類名(key=value)
*?? 第二個(gè):通過(guò)讀取配置文件中配置的內(nèi)容,反射創(chuàng)建對(duì)象
*
*?? 我的配置文件可以是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 {//定義一個(gè)Properties對(duì)象private static Properties props;//定義一個(gè)Map,用于存放我們要?jiǎng)?chuàng)建的對(duì)象。我們把它稱之為容器private static Map<String,Object> beans;//使用靜態(tài)代碼塊為Properties對(duì)象賦值static {try {//實(shí)例化對(duì)象props = new Properties();//獲取properties文件的流對(duì)象InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");props.load(in);//實(shí)例化容器beans = new HashMap<String,Object>();//取出配置文件中所有的KeyEnumeration keys = props.keys();//遍歷枚舉while (keys.hasMoreElements()){//取出每個(gè)KeyString key = keys.nextElement().toString();//根據(jù)key獲取valueString beanPath = props.getProperty(key);//反射創(chuàng)建對(duì)象Object value = Class.forName(beanPath).newInstance();//把key和value存入容器中beans.put(key,value);}}catch(Exception e){throw new ExceptionInInitializerError("初始化properties失敗!");}}/*** 根據(jù)bean的名稱獲取對(duì)象* @param beanName* @return*/public static Object getBean(String beanName){return beans.get(beanName);}/*** 根據(jù)Bean的名稱獲取bean對(duì)象* @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();//每次都會(huì)調(diào)用默認(rèn)構(gòu)造函數(shù)創(chuàng)建對(duì)象}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;/*** 賬戶的持久層實(shí)現(xiàn)類*/ public class AccountDaoImpl implements IAccountDao {public void saveAccount(){System.out.println("保存了賬戶");} }IAccountService.java
package com.itheima.service;/*** 賬戶業(yè)務(wù)層的接口*/ 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;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ 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;/*** 模擬一個(gè)表現(xiàn)層,用于調(diào)用業(yè)務(wù)層*/ 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();}} }總結(jié)
以上是生活随笔為你收集整理的曾经案例中问题 与 工厂模式解耦的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 程序的耦合及解耦
- 下一篇: 使用 spring 的 IOC 解决程序