IOC是Inversion of Control的縮寫,控制反轉的含義。表示對象控制權的轉移,將對象建立、銷燬等移交到Ioc容器來管理,使用該對象的調用者,也須要到Ioc容器中獲取該對象。
下面咱們就開始建立本身的Ioc容器來管理和建立對象了。java
dao層:數據訪問層
service層:業務層
ui層:暴露給外部使用的層spring
(1)dao層的代碼:
接口定義:框架
public interface IAccountDao { void saveAccount(); }
實現類:maven
package org.study.dao.impl; import org.study.dao.IAccountDao; public class AccountDaoImpl implements IAccountDao { @Override public void saveAccount() { System.out.println("保存了..."); } }
(2)service層代碼
接口類:ide
package org.study.service; public interface IAccountService { void saveAccount(); }
實現類:學習
package org.study.service.impl; import org.study.dao.IAccountDao; import org.study.dao.impl.AccountDaoImpl; import org.study.service.IAccountService; public class AccountServiceImpl implements IAccountService { @Override public void saveAccount() { IAccountDao accountDao=new AccountDaoImpl(); accountDao.saveAccount(); } }
(3)ui層代碼ui
package org.study.ui; import org.study.service.IAccountService; import org.study.service.impl.AccountServiceImpl; public class Client { public static void main(String[] args) { IAccountService accountService = new AccountServiceImpl(); accountService.saveAccount(); } }
(4)代碼編寫完成後,咱們的代碼目錄、以及最終運行效果以下圖:
代碼目錄:
運行結果:
idea
咱們的代碼如期地正確運行了,可是問題也比較明顯,代碼耦合度高,ui層依賴於service層,service層依賴與dao層。
有沒有好的辦法,能下降咱們模塊間的耦合呢,下面咱們經過工廠模式建立對象,下降對象間的耦合。code
(一)編寫配置文件bean.properties,將要管理的類放入配置文件中對象
accountDao=org.study.dao.impl.AccountDaoImpl accountService=org.study.service.impl.AccountServiceImpl
(二)編寫beanFactory類,讀取配置文件,並建立對象,代碼以下
package org.study.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; public class beanFactory { private static final Map<String,String> beanProperties= new HashMap<>(); static { InputStream inputStream= beanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); Properties properties =new Properties(); try { properties.load(inputStream); Enumeration<String> names =(Enumeration<String> )properties.propertyNames(); while (names.hasMoreElements()) { String key = names.nextElement(); String value = properties.getProperty(key); beanProperties.put(key, value); } } catch (IOException e) { e.printStackTrace(); }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public Object getBean(String name){ String value = beanProperties.get(name); if (value!=null){ try { return Class.forName(value).newInstance(); } catch (Exception e) { e.printStackTrace(); } } return null; } }
該類提供了一個方法getBean,該方法經過key來建立對象;
改造後的代碼層次以下:
(1)改造AccountServiceImpl的類,經過beanFactory建立accountDao,代碼以下:
package org.study.service.impl; import org.study.dao.IAccountDao; import org.study.factory.beanFactory; import org.study.service.IAccountService; public class AccountServiceImpl implements IAccountService { @Override public void saveAccount() { IAccountDao accountDao=(IAccountDao) beanFactory.getBean("accountDao"); accountDao.saveAccount(); } }
(2)改造ui層的類,經過beanFactory建立accountService,代碼以下:
IAccountService accountService = (IAccountService) beanFactory.getBean("accountService"); accountService.saveAccount();
運行代碼,結果以下:
1、第一種方式編寫代碼是咱們常見的方式,缺點是代碼間的耦合度高,一個類強依賴於另外一個類。 2、第二種方式,咱們將對象的建立都放到工廠類裏來執行,類之間只依賴於接口,不依賴於具體的實現;經過此方式,咱們下降了對象之間的耦合,能夠隨時修改接口的實現類;缺點是,對象的建立過程變得更復雜了,不那麼直觀。 3、beanFactory是一個簡單的ioc容器類,咱們的對象都經過這個容器建立,功能也很單一。 下一篇,咱們開始引入spring框架幫咱們管理對象,讓咱們一塊兒學習spring中的ioc。