工厂模式解耦的升级版
生活随笔
收集整理的這篇文章主要介紹了
工厂模式解耦的升级版
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.learn.service;/*** 賬戶業(yè)務(wù)層的接口*/
public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();
}
package com.learn.service.impl;import com.learn.dao.IAccountDao;
import com.learn.factory.BeanFactory;
import com.learn.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++;}
}
package com.learn.ui;import com.learn.factory.BeanFactory;
import com.learn.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();}}
}
package com.learn.factory;import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;/*** 一個(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*/
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;}*/
}
package com.learn.dao.impl;import com.learn.dao.IAccountDao;/*** 賬戶的持久層實(shí)現(xiàn)類*/
public class AccountDaoImpl implements IAccountDao {public void saveAccount(){System.out.println("保存了賬戶");}
}
package com.learn.dao;/*** 賬戶的持久層接口*/
public interface IAccountDao {/*** 模擬保存賬戶*/void saveAccount();
}
accountService=com.learn.service.impl.AccountServiceImpl
accountDao=com.learn.dao.impl.AccountDaoImpl
?
總結(jié)
以上是生活随笔為你收集整理的工厂模式解耦的升级版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。