Pattern of Domain Object(3)
第三种模型印象中好像是firebody或者是Archie提出的(也有可能不是,记不清楚了),简单的来说,这种模型就是把第二种模型的domain object和business object合二为一了。所以ItemManager就不需要了,在这种模型下面,只有三个类,他们分别是:
Item:包含了实体类信息,也包含了所有的业务逻辑
ItemDao:持久化DAO接口类
ItemDaoHibernateImpl:DAO接口的实现类
由于ItemDao和ItemDaoHibernateImpl和上面完全相同,就省略了。
java 代码
- public class Item implements Serializable {
- // 所有的属性和getter/setter方法都省略
- private static ItemDao itemDao;
- public void setItemDao(ItemDao itemDao) {this.itemDao = itemDao;}
- public static Item loadItemById(Long id) {
- return (Item) itemDao.loadItemById(id);
- }
- public static Collection findAll() {
- return (List) itemDao.findAll();
- }
- public Bid placeBid(User bidder, MonetaryAmount bidAmount,
- Bid currentMaxBid, Bid currentMinBid)
- throws BusinessException {
- // Check highest bid (can also be a different Strategy (pattern))
- if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {
- throw new BusinessException("Bid too low.");
- }
- // Auction is active
- if ( !state.equals(ItemState.ACTIVE) )
- throw new BusinessException("Auction is not active yet.");
- // Auction still valid
- if ( this.getEndDate().before( new Date() ) )
- throw new BusinessException("Can't place new bid, auction already ended.");
- // Create new Bid
- Bid newBid = new Bid(bidAmount, this, bidder);
- // Place bid for this Item
- this.addBid(newBid);
- itemDao.update(this); // 调用DAO进行显式持久化
- return newBid;
- }
- }
在这种模型中,所有的业务逻辑全部都在Item中,事务管理也在Item中实现。