论坛首页 Java企业应用论坛

再次小结领域模型的种种观点

浏览 145614 次
该帖已经被评为精华帖
作者 正文
   发表时间:2006-11-13  
听的感觉好难啊!楼主的那个看明白了,往下越来越难,@在java里面是什么意思啊?还有<T>
0 请登录后投票
   发表时间:2007-01-17  
为什么不能综合几个模型的好处呢?
详见我的博客。
1.先定义一个失血模型;这样做的好处是和数据库一一对应,便于ORM。
2.定义一个贫血模型,继承自失血模型;这样做可以在其内定义领域逻辑,更OO。
3.合并service和dao,用Factory输出,只允许在贫血模型里面调用。

以下是那个例子的实现
/**
 * 定义一个失血模型
 */
public class AbstractItem implements Serializable {  
    private Long id = null;  
    private int version;  
    private String name;  
    private User seller;  
    private String description;  
    private MonetaryAmount initialPrice;  
    private MonetaryAmount reservePrice;  
    private Date startDate;  
    private Date endDate;  
    private Set categorizedItems = new HashSet();  
    private Collection bids = new ArrayList();  
    private Bid successfulBid;  
    private ItemState state;  
    private User approvedBy;  
    private Date approvalDatetime;  
    private Date created = new Date();  
    //  getter/setter方法省略不写,避免篇幅太长  
} 


/**
 * 输出DAO,便于装卸其他Impl
 * 可以在xml中配置Impl
 */
public class ItemDaoFactory {  
    public ItemDao getItemDao(){
        return new ItemDaoHibernateImpl();
    } 
} 

/**
 * 定义DAO
 */
public interface ItemDao {  
    public Item getItemById(Long id);  
    public Collection findAll();  
    public void updateItem(Item item);  
} 

/**
 * 实现DAO
 */
public class ItemDaoHibernateImpl implements ItemDao extends HibernateDaoSupport {  
    public Item getItemById(Long id) {  
        return (Item) getHibernateTemplate().load(Item.class, id);  
    }  
    public Collection findAll() {  
        return (List) getHibernateTemplate().find("from Item");  
    }  
    public void updateItem(Item item) {  
        getHibernateTemplate().update(item);  
    }  
}


/**
 * 定义一个贫血模型,继承自失血模型
 * 吸收了充血模型的“业务逻辑放在domain object里面”
 * 吸收了胀血模型的“取消Service层”
 */
public class Item extends AbstractItem {  
    public static Bid loadItemById(Long id) {   
        ItemDaoFactory.getItemDao().getItemById(id);  
    }  
    public static Collection listAllItems() {  
        return  ItemDaoFactory.getItemDao().findAll();  
    }  
/**
 *可以在外部简化,如在Action里面,不过写在这里也没有关系
 */
    public static Bid placeBid(Item item, User bidder, MonetaryAmount bidAmount,  
                        Bid currentMaxBid, Bid currentMinBid) throws BusinessException {  
            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 ( item.getEndDate().before( new Date() ) )  
        throw new BusinessException("Can't place new bid, auction already ended.");  
      
    // Create new Bid  
    Bid newBid = new Bid(bidAmount, item, bidder);  
      
    // Place bid for this Item  
    item.getBids().add(newBid);  
    ItemDaoFactory.getItemDao().update(item);     
//  调用DAO完成持久化操作  
    return newBid;  
    }  
}
0 请登录后投票
   发表时间:2007-01-17  


在Action中调用的只有Item,其他对于客户程序员都透明。Factory里面的方法是static,
我是即兴写的,没有调试,大家能够理解我的思想就可以了。

/** 
 * 输出DAO,便于装卸其他Impl 
 * 可以在xml中配置Impl 
 */  
public class ItemDaoFactory {    
    static public ItemDao getItemDao(){  
        return new ItemDaoHibernateImpl();  
    }   
}  
0 请登录后投票
   发表时间:2007-01-17  
做了那么多项目几乎就没有一个不失血的,最近一个项目用上了贫血,甚至还有点充血,由于绝大多数domain object由另外一同事定义,更绝的是该同事整了把这个就辞职了,读代码读得偶那个痛苦啊
1 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics