表現層-業務層-持久層(工廠模式解耦-單列版)
阿新 • • 發佈:2020-07-11
package com.example.ui; import com.example.factory.BeanFactory; import com.example.service.IAccountService; /** * 模擬一個表現層,用於呼叫業務層 */ public class Client { public static void main(String[] args) { IAccountService as= (IAccountService) BeanFactory.getBean("accountService"); as.saveAccount(); } }
package com.example.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; /** * 一個建立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物件賦值 private static boolean flag; static{ //設定初始化狀態為正在初始化 flag=true; //例項化物件props=new Properties(); //例項化容器 beans=new HashMap<String,Object>(); //獲取properties物件的流物件 InputStream in= BeanFactory.class.getClassLoader().getResourceAsStream("Bean.properties"); try { props.load(in); } catch (IOException e) { throw new ExceptionInInitializerError("初始化properties失敗"); } //獲取列舉物件 Enumeration keys = props.keys(); while(keys.hasMoreElements()){ //獲取bean物件的key String key = keys.nextElement().toString(); //把bean物件放入beans容器中 beans.put(key,getBean(key)); } //設定初始化狀態為已初始化 flag=false; } /** * 根據bean的名稱獲取bean物件 * @param beanName * @return */ public static Object getBean(String beanName){ Object bean= beans.get(beanName); //以下if語句解決初始化順序帶來的隱患,flag標識初始化中 if(bean==null&&flag){ String beanPath = props.getProperty(beanName); try { bean = Class.forName(beanPath).newInstance(); } catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) { e.printStackTrace(); } } return bean; } }
package com.example.service; /** * 賬戶業務層介面 */ public interface IAccountService { /** * 模擬儲存賬戶 */ void saveAccount(); }
package com.example.service.impl; import com.example.factory.BeanFactory; import com.example.service.IAccountService; import com.example.dao.IAccountDao; /** * 賬戶的業務層實現類 */ public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao= (IAccountDao) BeanFactory.getBean("accountDao"); @Override public void saveAccount() { accountDao.saveAccount(); } }
package com.example.dao; /** * 賬戶的持久層介面 */ public interface IAccountDao { /** * 模擬儲存賬戶 */ void saveAccount(); }
package com.example.dao.impl; import com.example.dao.IAccountDao; /** * 賬戶的持久層實現類 */ public class AccountDaoImpl implements IAccountDao { @Override public void saveAccount(){ System.out.println("儲存了賬戶"); } }
accountDao=com.example.dao.impl.AccountDaoImpl
accountService=com.example.service.impl.AccountServiceImpl
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>learn</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> </dependencies> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>14</java.version> <maven.compiler.source>14</maven.compiler.source> <maven.compiler.target>14</maven.compiler.target> <encoding>UTF-8</encoding> </properties> </project>