Dao 层:数据访问层;
Service层:业务层;
举个实例:现在要做一个银行操作系统:
Dao模型需要先提供一个Dao接口;
然后再提供一个Dao接口的实现类;
再编写一个Dao工厂,Service通过工厂来获取Dao实现;
package com.why.dao;
import java.util.List;
import com.why.domain.Account;
/**
* 账户Dao,专门对账户做增删改查操作的
* @author TF
*
*/
public interface AccountDao {
/**
* 完成对银行账户数据的持久化操作
* @param accounts 账户对象的集合
* @throws Exception
*/
void write(List<Account> accounts) throws Exception;
/**
* 读取所有银行账户对象的集合
* @return
* @throws Exception
*/
List<Account> read() throws Exception;
/**
* 根据账户读取账户对象
* @param accountNo 银行账户
* @return 返回满足账号的账户对象
* @throws Exception
*/
Account read(String accountNo) throws Exception;
/**
* 根据登录时管理员的账户和密码的合法性
* @param accountNo 管理员账户
* @param accountPwd 管理员密码
* @return 管理员对象,如果账户密码不合法则返回null
*
* @throws Exception
*/
Account read(String accountNo,String accountPwd) throws Exception;
}
package com.why.dao.bean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.ResourceBundle;
import com.why.dao.AccountDao;
import com.why.domain.Account;
import com.why.globals.Globals;
public class AccountDaoBean implements AccountDao {
@Override
public void write(List<Account> accounts) throws Exception {
ObjectOutputStream objectOutputStream=null;
try{
objectOutputStream=new ObjectOutputStream(new FileOutputStream(Globals.ACCOUNT_FILE));
objectOutputStream.writeObject(accounts);
objectOutputStream.flush();
}finally{
if(objectOutputStream!=null){
objectOutputStream.close();
objectOutputStream=null;
}
}
}
@Override
public List<Account> read() throws Exception {
ObjectInputStream objectInputStream=null;
try{
File file=new File(Globals.ACCOUNT_FILE);
if(!file.exists()){
throw new Exception("账户文件不存在");
//return null;
}
objectInputStream=new ObjectInputStream(new FileInputStream(file));
List<Account> accounts=(List<Account>) objectInputStream.readObject();
return accounts;
}finally{
if(objectInputStream!=null){
objectInputStream.close();
objectInputStream=null;
}
}
}
@Override
public Account read(String accountNo) throws Exception {
List<Account> accounts=this.read();
if(accounts!=null){
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
return account;
}
}
throw new Exception("账户为"+accountNo+"不存在");
}else{
throw new Exception("银行账户文件不存在");
}
}
@Override
public Account read(String accountNo, String accountPwd) throws Exception {
ResourceBundle resourceBundle=ResourceBundle.getBundle("AdminConfig");
String name=resourceBundle.getString("userName");
String pwd=resourceBundle.getString("pwd");
if(!(accountNo.equals(name) && accountPwd.equals(pwd))){
throw new Exception("账户或密码错误");
}
Account account=new Account(name,pwd,0,null);
return account;
}
}
package com.why.dao.factory;
import com.why.dao.AccountDao;
import com.why.dao.bean.AccountDaoBean;
/**
*
* 自定义Dao工厂类
* 工厂类的好处:
* 1、将对象的实例化进行了统一管理
* 2、由于方法返回的是接口类型,因为对于调用者而言,
* 只识别接口类型的对象即可,不需要知道具体实现是什么
*
* @author TF
*
*/
public class DaoFactory {
/**
* 方法返回的是接口类型,但是实现类中的实例化了接口的实现类
* @return
*/
public static AccountDao getAccountDao(){
return new AccountDaoBean();
}
}
Service是业务层,也是先提供一个Service接口;
然后再提供一个Service接口的实现类;
再编写一个Service工厂,方便通过工厂来获取Service实现;
package com.why.service;
import java.util.List;
import com.why.domain.Account;
public interface AccountService {
/**
* 普通银行账户的登录
* @param accountNo 登录的账号
* @param accountPwd 登录的密码
* @return 登录成功返回true,登陆失败返回false
*/
boolean login(String accountNo,String accountPwd) throws Exception;
/**
* 管理员登录
* @param accountNo 登录的账号
* @param accountPwd 登录的密码
* @return 登录成功返回true,登陆失败返回false
* @throws Exception
*/
boolean adminLogin(String accountNo,String accountPwd) throws Exception;
/**
* 开户功能
* @param account 开户时对应的账户对象
* @return 开户成功返回true
* @throws Exception
*/
boolean open(Account account) throws Exception;
/**
* 查询所有账户
* @return 返回所有账户的集合
* @throws Exception
*/
List<Account> query() throws Exception;
/**
* 根据银行账号查询银行账户
* @param accountNo 要查询的银行账号
* @return 返回要查询的银行账户
* @throws Exception
*/
Account query(String accountNo) throws Exception;
/**
* 存款
* @param accountNo 账号
* @param balance 存款的金额
* @return 存款是否成功
* @throws Exception
*/
boolean deposit(double balance) throws Exception;
/**
* 取款
* @param accountNo 账号
* @param balance 取款金额
* @return 取款是否成功
* @throws Exception
*/
boolean withdraw(double balance) throws Exception;
/**
* 转账
* @param accountNo 对方账号
* @param balance 转账金额
* @return 转账是否成功
* @throws Exception
*/
boolean transfer(String accountNo,double balance) throws Exception;
/**
* 修改密码
* @param newPwd 新密码
* @return 修改密码是否成功
* @throws Exception
*/
boolean changePwd(String newPwd) throws Exception;
/**
* 冻结账户
* @param accountNo 账号
* @return 冻结账户是否成功
* @throws Exception
*/
boolean freeze(String accountNo) throws Exception;
/**
* 解冻账户
* @param accountNo 账号
* @return 冻结账户是否成功
* @throws Exception
*/
boolean unfreeze(String accountNo) throws Exception;
/**
* 挂失指定账户
* @param accountNo 需要挂失账户的账号
* @return 挂失是否成功
* @throws Exception
*/
boolean lost(String accountNo) throws Exception;
/**
* 解挂指定账户
* @param accountNo 需要解挂的账户的账号
* @return 解挂是否成功
* @throws Exception
*/
boolean unlost(String accountNo) throws Exception;
/**
* 根据账号销毁指定账户对象
* @param accountNo 需要销户的账号
* @return 销户是否成功
* @throws Exception
*/
boolean destroy(String accountNo) throws Exception;
}
package com.why.service.bean;
import java.util.List;
import com.why.dao.AccountDao;
import com.why.dao.factory.DaoFactory;
import com.why.domain.Account;
import com.why.domain.AccountStatus;
import com.why.service.AccountService;
public class AccountServiceBean implements AccountService {
private AccountDao accountDao;
private String currentAccountNo;
public String getCurrentAccountNo() {
return currentAccountNo;
}
public AccountServiceBean() {
accountDao=DaoFactory.getAccountDao();
}
@Override
public boolean login(String accountNo, String accountPwd) throws Exception {
List<Account> accounts=accountDao.read();
boolean isLogin=false;
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo) && account.getAccountPwd().equals(accountPwd)){
this.currentAccountNo=accountNo;
isLogin=true;
}
}
return isLogin;
}
@Override
public boolean adminLogin(String accountNo, String accountPwd) throws Exception {
Account account=accountDao.read(accountNo, accountPwd);
boolean isLogin=false;
if(account.getAccountNo().equals(accountNo) && account.getAccountPwd().equals(accountPwd)){
this.currentAccountNo=accountNo;
isLogin=true;
}
return isLogin;
}
@Override
public boolean open(Account account) throws Exception {
List<Account> accounts=accountDao.read();
for (Account item : accounts) {
if(account.getAccountNo().equals(item.getAccountNo())){
System.out.println("账户已存在");
return false;
}
}
accounts.add(account);
accountDao.write(accounts);
return true;
}
@Override
public List<Account> query() throws Exception {
return accountDao.read();
}
@Override
public Account query(String accountNo) throws Exception {
return accountDao.read(accountNo);
}
@Override
public boolean deposit(double balance) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(this.currentAccountNo)){
account.setBalance(account.getBalance()+balance);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean withdraw(double balance) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(this.currentAccountNo)){
double result=account.getBalance()-balance;
if(result<0){
throw new Exception("余额不足,取款失败");
}
account.setBalance(result);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean transfer(String accountNo, double balance) throws Exception {
List<Account> accounts=accountDao.read();
boolean isOne=false;
boolean isTwo=false;
for (Account account : accounts) {
if(account.getAccountNo().equals(this.currentAccountNo)){
double result=account.getBalance()-balance;
if(result<0){
throw new Exception("余额不足,转账失败");
}
account.setBalance(result);
isOne=true;
}
if(account.getAccountNo().equals(accountNo)){
account.setBalance(account.getBalance()+balance);
isTwo=true;
}
}
if(isOne && isTwo){
accountDao.write(accounts);
return true;
}
return false;
}
@Override
public boolean changePwd(String newPwd) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(this.currentAccountNo)){
account.setAccountPwd(newPwd);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean freeze(String accountNo) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
account.setAccountStatus(AccountStatus.FREEZE);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean unfreeze(String accountNo) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
account.setAccountStatus(AccountStatus.NORMAL);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean lost(String accountNo) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
account.setAccountStatus(AccountStatus.LOST);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean unlost(String accountNo) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
account.setAccountStatus(AccountStatus.NORMAL);
accountDao.write(accounts);
return true;
}
}
return false;
}
@Override
public boolean destroy(String accountNo) throws Exception {
List<Account> accounts=accountDao.read();
for (Account account : accounts) {
if(account.getAccountNo().equals(accountNo)){
accounts.remove(account);
accountDao.write(accounts);
return true;
}
}
return false;
}
}
package com.why.service.factory;
import com.why.service.AccountService;
import com.why.service.bean.AccountServiceBean;
public class ServiceFactory {
public static AccountService getAccountService(){
return new AccountServiceBean();
}
}