表現層-業務層-持久層(工廠模式解耦)
阿新 • • 發佈:2020-07-11
package com.example.ui; import com.example.factory.BeanFactory; import com.example.service.IAccountService; import com.example.service.impl.AccountServiceImpl; /** * 模擬一個表現層,用於呼叫業務層 */ public class Client { public static void main(String[] args) { //IAccountService as=new AccountServiceImpl();IAccountService as= (IAccountService) BeanFactory.getBean("accountService"); as.saveAccount(); } }
package com.example.factory; import java.io.IOException; import java.io.InputStream; 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; //使用靜態程式碼塊為Properties物件賦值 static{ //例項化物件 props=new Properties(); //獲取properties物件的流物件 InputStream in= BeanFactory.class.getClassLoader().getResourceAsStream("Bean.properties");try { props.load(in); } catch (IOException e) { throw new ExceptionInInitializerError("初始化properties失敗"); } } /** * 根據bean的名稱獲取bean物件 * @param beanName * @return */ public static Object getBean(String beanName){ Object bean = null; String beanPath = props.getProperty(beanName); System.out.println(beanPath); 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; import com.example.dao.impl.AccountDaoImpl; /** * 賬戶的業務層實現類 */ public class AccountServiceImpl implements IAccountService { //private IAccountDao accountDao=new AccountDaoImpl(); 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("儲存了賬戶"); } }
#Bean.properties accountService=com.example.service.impl.AccountServiceImpl accountDao=com.example.dao.impl.AccountDaoImpl
<!--pom.xm--> <?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>