表现层-业务层-持久层(工厂模式解耦)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
表现层-业务层-持久层(⼯⼚模式解耦)
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="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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>/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>
<piler.source>14</piler.source>
<piler.target>14</piler.target>
<encoding>UTF-8</encoding>
</properties>
</project>。