贫血模型or领域模型 (2).doc

上传人:ni****g 文档编号:543548679 上传时间:2023-07-27 格式:DOC 页数:15 大小:185.51KB
返回 下载 相关 举报
贫血模型or领域模型 (2).doc_第1页
第1页 / 共15页
贫血模型or领域模型 (2).doc_第2页
第2页 / 共15页
贫血模型or领域模型 (2).doc_第3页
第3页 / 共15页
贫血模型or领域模型 (2).doc_第4页
第4页 / 共15页
贫血模型or领域模型 (2).doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《贫血模型or领域模型 (2).doc》由会员分享,可在线阅读,更多相关《贫血模型or领域模型 (2).doc(15页珍藏版)》请在金锄头文库上搜索。

1、最近taowen同学连续发起了两起关于贫血模型和领域模型的讨论,引起了大家的广泛热烈的讨论,但是讨论(或者说是争论)的结果到底怎样,我想值得商榷。问题是大家对贫血模型和领域模型都有自己的看法,如果没有对此达到概念上的共识,那么讨论的结果应该可想而知,讨论的收获也是有的,至少知道了分歧的存在。为了使问题具有确定性,我想从一个简单例子着手,用我对贫血模型和领域模型的概念来分别实现例子。至于我的理解对与否,大家可以做评判,至少有个可以评判的标准在这。一个例子 我要举的是一个银行转帐的例子,又是一个被用滥了的例子。但即使这个例子也不是自己想出来的,而是剽窃的中的例子,原谅我可怜的想像力 。当钱从一个帐

2、户转到另一个帐户时,转帐的金额不能超过第一个帐户的存款余额,余额总数不能变,钱只是从一个账户流向另一个帐户,因此它们必须在一个事务内完成,每次事务成功完成都要记录此次转帐事务,这是所有的规则。贫血模型 我们首先用贫血模型来实现。所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个对象充当的就是一个数据容器,用C语言的话来说就是一个结构体,所有的业务方法都在一个无状态的Service类中实现,Service类仅仅包含一些行为。这是Java Web程序采用的最常用开发模型,你可能采用的就是这种方法,虽然可能不知道它有个&ldq

3、uo;贫血模型”的称号,这要多亏Martin Flower(这个家伙惯会发明术语!)。包结构 在讨论具体的实现之前,我们先来看来贫血模型的包结构,以便对此有个大概的了解。 贫血模型的实现一般包括如下包: dao:负责持久化逻辑 model:包含数据对象,是service操纵的对象 service:放置所有的服务类,其中包含了所有的业务逻辑 facade:提供对UI层访问的入口 代码实现 先看model包的两个类,Account和TransferTransaction对象,分别代表帐户和一次转账事务。由于它们不包含业务逻辑,就是一个普通的Java Bean,下面的代码省略了get和s

4、et方法。Java代码 1. publicclassAccount 2. privateStringaccountId; 3. privateBigDecimalbalance; 4. 5. publicAccount() 6. publicAccount(StringaccountId,BigDecimalbalance) 7. this.accountId=accountId; 8. this.balance=balance; 9. 10. /getterandsetter. 11. 12. public class Account private String accountId;pri

5、vate BigDecimal balance;public Account() public Account(String accountId, BigDecimal balance) this.accountId = accountId;this.balance = balance;/ getter and setter .Java代码 1. publicclassTransferTransaction 2. privateDatetimestamp; 3. privateStringfromAccountId; 4. privateStringtoAccountId; 5. privat

6、eBigDecimalamount; 6. 7. publicTransferTransaction() 8. 9. publicTransferTransaction(StringfromAccountId,StringtoAccountId,BigDecimalamount,Datetimestamp) 10. this.fromAccountId=fromAccountId; 11. this.toAccountId=toAccountId; 12. this.amount=amount; 13. this.timestamp=timestamp; 14. 15. 16. /getter

7、andsetter. 17. public class TransferTransaction private Date timestamp;private String fromAccountId;private String toAccountId;private BigDecimal amount;public TransferTransaction() public TransferTransaction(String fromAccountId, String toAccountId, BigDecimal amount, Date timestamp) this.fromAccou

8、ntId = fromAccountId;this.toAccountId = toAccountId;this.amount = amount;this.timestamp = timestamp;/ getter and setter .这两个类没什么可说的,它们就是一些数据容器。接下来看service包中TransferService接口和它的实现TransferServiceImpl。TransferService定义了转账服务的接口,TransferServiceImpl则提供了转账服务的实现。Java代码 1. publicinterfaceTransferService 2. T

9、ransferTransactiontransfer(StringfromAccountId,StringtoAccountId,BigDecimalamount) 3. throwsAccountNotExistedException,AccountUnderflowException; 4. public interface TransferService TransferTransaction transfer(String fromAccountId, String toAccountId, BigDecimal amount) throws AccountNotExistedExce

10、ption, AccountUnderflowException;Java代码 1. publicclassTransferServiceImplimplementsTransferService 2. privateAccountDAOaccountDAO; 3. privateTransferTransactionDAOtransferTransactionDAO; 4. 5. publicTransferServiceImpl(AccountDAOaccountDAO, 6. TransferTransactionDAOtransferTransactionDAO) 7. this.ac

11、countDAO=accountDAO; 8. this.transferTransactionDAO=transferTransactionDAO; 9. 10. 11. 12. publicTransferTransactiontransfer(StringfromAccountId,StringtoAccountId, 13. BigDecimalamount)throwsAccountNotExistedException,AccountUnderflowException 14. Validate.isTrue(pareTo(BigDecimal.ZERO)0); 15. 16. A

12、ccountfromAccount=accountDAO.findAccount(fromAccountId); 17. if(fromAccount=null)thrownewAccountNotExistedException(fromAccountId); 18. if(fromAccount.getBalance().compareTo(amount)0) 19. thrownewAccountUnderflowException(fromAccount,amount); 20. 21. 22. AccounttoAccount=accountDAO.findAccount(toAcc

13、ountId); 23. if(toAccount=null)thrownewAccountNotExistedException(toAccountId); 24. fromAccount.setBalance(fromAccount.getBalance().subtract(amount); 25. toAccount.setBalance(toAccount.getBalance().add(amount); 26. 27. accountDAO.updateAccount(fromAccount);/对Hibernate来说这不是必须的 28. accountDAO.updateAccount(toAccount);/对Hibernate来说这不是必须的 29. returntransferTransactionDAO.create(fromAccountId,toAccountId,amount); 30. 31

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号