工厂模式解耦的升级版
生活随笔
收集整理的這篇文章主要介紹了
工厂模式解耦的升级版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.learn.service;/*** 賬戶業務層的接口*/
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;/*** 賬戶的業務層實現類*/
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;/*** 模擬一個表現層,用于調用業務層*/
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;/*** 一個創建Bean對象的工廠** Bean:在計算機英語中,有可重用組件的含義。* JavaBean:用java語言編寫的可重用組件。* javabean > 實體類** 它就是創建我們的service和dao對象的。** 第一個:需要一個配置文件來配置我們的service和dao* 配置的內容:唯一標識=全限定類名(key=value)* 第二個:通過讀取配置文件中配置的內容,反射創建對象** 我的配置文件可以是xml也可以是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;}*/
}
package com.learn.dao.impl;import com.learn.dao.IAccountDao;/*** 賬戶的持久層實現類*/
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
?
總結
以上是生活随笔為你收集整理的工厂模式解耦的升级版的全部內容,希望文章能夠幫你解決所遇到的問題。