国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計軟件教程 > 總結(jié)一下最近關(guān)于domain object以及相關(guān)的討論

總結(jié)一下最近關(guān)于domain object以及相關(guān)的討論
2010-01-14 22:28:47  作者:  來源:
在最近的圍繞domain object的討論中浮現(xiàn)出來了三種模型,(還有一些其他的旁枝,不一一分析了),經(jīng)過一番討論,各種問題逐漸清晰起來,在這里我試圖做一個總結(jié),便于大家了解和掌握。

第一種模型:只有g(shù)etter/setter方法的純數(shù)據(jù)類,所有的業(yè)務(wù)邏輯完全由business object來完成(又稱TransactionScript),這種模型下的domain object被Martin Fowler稱之為“貧血的domain object”。下面用舉一個具體的代碼來說明,代碼來自Hibernate的caveatemptor,但經(jīng)過我的改寫:

一個實體類叫做Item,指的是一個拍賣項目
一個DAO接口類叫做ItemDao
一個DAO接口實現(xiàn)類叫做ItemDaoHibernateImpl
一個業(yè)務(wù)邏輯類叫做ItemManager(或者叫做ItemService)

Java代碼 復(fù)制代碼
  1. public class Item implements Serializable {   
  2.     private Long id = null;   
  3.     private int version;   
  4.     private String name;   
  5.     private User seller;   
  6.     private String description;   
  7.     private MonetaryAmount initialPrice;   
  8.     private MonetaryAmount reservePrice;   
  9.     private Date startDate;   
  10.     private Date endDate;   
  11.     private Set categorizedItems = new HashSet();   
  12.     private Collection bids = new ArrayList();   
  13.     private Bid successfulBid;   
  14.     private ItemState state;   
  15.     private User approvedBy;   
  16.     private Date approvalDatetime;   
  17.     private Date created = new Date();   
  18.     //  getter/setter方法省略不寫,避免篇幅太長   
  19. }  


Java代碼 復(fù)制代碼
  1. public interface ItemDao {   
  2.     public Item getItemById(Long id);   
  3.     public Collection findAll();   
  4.     public void updateItem(Item item);   
  5. }  


ItemDao定義持久化操作的接口,用于隔離持久化代碼。

Java代碼 復(fù)制代碼
  1. public class ItemDaoHibernateImpl implements ItemDao extends HibernateDaoSupport {   
  2.     public Item getItemById(Long id) {   
  3.         return (Item) getHibernateTemplate().load(Item.class, id);   
  4.     }   
  5.     public Collection findAll() {   
  6.         return (List) getHibernateTemplate().find("from Item");   
  7.     }   
  8.     public void updateItem(Item item) {   
  9.         getHibernateTemplate().update(item);   
  10.     }   
  11. }  

ItemDaoHibernateImpl完成具體的持久化工作,請注意,數(shù)據(jù)庫資源的獲取和釋放是在ItemDaoHibernateImpl里面處理的,每個DAO方法調(diào)用之前打開Session,DAO方法調(diào)用之后,關(guān)閉Session。(Session放在ThreadLocal中,保證一次調(diào)用只打開關(guān)閉一次)

Java代碼 復(fù)制代碼
  1. public class ItemManager {   
  2.     private ItemDao itemDao;   
  3.     public void setItemDao(ItemDao itemDao) { this.itemDao = itemDao;}   
  4.     public Bid loadItemById(Long id) {    
  5.         itemDao.loadItemById(id);   
  6.     }   
  7.     public Collection listAllItems() {   
  8.         return  itemDao.findAll();   
  9.     }   
  10.     public Bid placeBid(Item item, User bidder, MonetaryAmount bidAmount,   
  11.                         Bid currentMaxBid, Bid currentMinBid) throws BusinessException {   
  12.             if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {   
  13.         throw new BusinessException("Bid too low.");   
  14.     }   
  15.        
  16.     // Auction is active   
  17.     if ( !state.equals(ItemState.ACTIVE) )   
  18.         throw new BusinessException("Auction is not active yet.");   
  19.        
  20.     // Auction still valid   
  21.     if ( item.getEndDate().before( new Date() ) )   
  22.         throw new BusinessException("Can't place new bid, auction already ended.");   
  23.        
  24.     // Create new Bid   
  25.     Bid newBid = new Bid(bidAmount, item, bidder);   
  26.        
  27.     // Place bid for this Item   
  28.     item.getBids().add(newBid);   
  29.     itemDao.update(item);     //  調(diào)用DAO完成持久化操作   
  30.     return newBid;   
  31.     }   
  32. }  


事務(wù)的管理是在ItemManger這一層完成的,ItemManager實現(xiàn)具體的業(yè)務(wù)邏輯。除了常見的和CRUD有關(guān)的簡單邏輯之外,這里還有一個placeBid的邏輯,即項目的競標(biāo)。

以上是一個完整的第一種模型的示例代碼。在這個示例中,placeBid,loadItemById,findAll等等業(yè)務(wù)邏輯統(tǒng)統(tǒng)放在ItemManager中實現(xiàn),而Item只有g(shù)etter/setter方法。

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢
相關(guān)熱詞搜索: