﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-SoulEdge</title><link>http://www.cnblogs.com/SoulEdge/</link><description /><language>zh-cn</language><lastBuildDate>Sun, 23 Nov 2008 09:24:05 GMT</lastBuildDate><pubDate>Sun, 23 Nov 2008 09:24:05 GMT</pubDate><ttl>60</ttl><item><title>Pattern of Domain Object(4)</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681722.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Tue, 20 Mar 2007 13:16:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681722.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/681722.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681722.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/681722.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/681722.html</trackback:ping><description><![CDATA[<p>在上面三种模型之外，还有很多这三种模型的变种，例如partech的模型就是把第二种模型中的DAO和Manager三个类合并为一个类后形成的模型；例如frain....(id很长记不住)的模型就是把第三种模型的三个类完全合并为一个单类后形成的模型；例如Archie是把第三种模型的Item又分出来一些纯数据类(可能是，不确定)形成的一个模型。</p>
<p>但是不管怎么变，基本模型归纳起来就是上面的三种模型，下面分别简单评价一下：</p>
<p>第一种模型绝大多数人都反对，因此反对理由我也不多讲了。但遗憾的是，我观察到的实际情形是，很多使用Hibernate的公司最后都是这种模型，这里面有很大的原因是很多公司的技术水平没有达到这种层次，所以导致了这种贫血模型的出现。从这一点来说，Martin Fowler的批评声音不是太响了，而是太弱了，还需要再继续呐喊。</p>
<p>第二种模型就是Martin Fowler一直主张的模型，实际上也是我一直在实际项目中采用这种模型。我没有看过Martin的POEAA，之所以能够自己摸索到这种模型，也是因为从02年我已经开始思考这个问题并且寻求解决方案了，但是当时没有看到Hibernate，那时候做的一个小型项目我已经按照这种模型来做了，但是由于没有O/R Mapping的支持，写到后来又不得不全部改成贫血的domain object，项目做完以后再继续找，随后就发现了Hibernate。当然，现在很多人一开始就是用Hibernate做项目，没有经历过我经历的那个阶段。</p>
<p>不过我觉得这种模型仍然不够完美，因为你还是需要一个业务逻辑层来封装所有的domain logic，这显得非常罗嗦，并且业务逻辑对象的接口也不够稳定。如果不考虑业务逻辑对象的重用性的话(业务逻辑对象的可重用性也不可能好)，很多人干脆就去掉了xxxManager这一层，在Web层的Action代码直接调用xxxDao，同时容器事务管理配置到Action这一层上来。Hibernate的caveatemptor就是这样架构的一个典型应用。</p>
<p>第三种模型是我很反对的一种模型，这种模型下面，Domain Object和DAO形成了双向依赖关系，无法脱离框架测试，并且业务逻辑层的服务也和持久层对象的状态耦合到了一起，会造成程序的高度的复杂性，很差的灵活性和糟糕的可维护性。也许将来技术进步导致的O/R Mapping管理下的domain object发展到足够的动态持久透明化的话，这种模型才会成为一个理想的选择。就像O/R Mapping的流行使得第二种模型成为了可能(O/R Mapping流行以前，我们只能用第一种模型，第二种模型那时候是不现实的)。</p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/681722.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43630/" target="_blank">[新闻]Silverlight 2 SDK中文版发布</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>Pattern of Domain Object(3)</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681720.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Tue, 20 Mar 2007 13:14:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681720.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/681720.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681720.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/681720.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/681720.html</trackback:ping><description><![CDATA[<p>第三种模型印象中好像是firebody或者是Archie提出的(也有可能不是，记不清楚了)，简单的来说，这种模型就是把第二种模型的domain object和business object合二为一了。所以ItemManager就不需要了，在这种模型下面，只有三个类，他们分别是：</p>
<p>Item：包含了实体类信息，也包含了所有的业务逻辑 <br>ItemDao：持久化DAO接口类 <br>ItemDaoHibernateImpl：DAO接口的实现类</p>
<p>由于ItemDao和ItemDaoHibernateImpl和上面完全相同，就省略了。</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;Item&nbsp;</span><span class=keyword>implements</span><span>&nbsp;Serializable&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;&nbsp;所有的属性和getter/setter方法都省略 </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;</span><span class=keyword>static</span><span>&nbsp;ItemDao&nbsp;itemDao; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>void</span><span>&nbsp;setItemDao(ItemDao&nbsp;itemDao)&nbsp;{</span><span class=keyword>this</span><span>.itemDao&nbsp;=&nbsp;itemDao;} &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>static</span><span>&nbsp;Item&nbsp;loadItemById(Long&nbsp;id)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;(Item)&nbsp;itemDao.loadItemById(id); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>static</span><span>&nbsp;Collection&nbsp;findAll()&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;(List)&nbsp;itemDao.findAll(); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;placeBid(User&nbsp;bidder,&nbsp;MonetaryAmount&nbsp;bidAmount, &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMaxBid,&nbsp;Bid&nbsp;currentMinBid) &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throws</span><span>&nbsp;BusinessException&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Check&nbsp;highest&nbsp;bid&nbsp;(can&nbsp;also&nbsp;be&nbsp;a&nbsp;different&nbsp;Strategy&nbsp;(pattern)) </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(currentMaxBid&nbsp;!=&nbsp;</span><span class=keyword>null</span><span>&nbsp;&amp;&amp;&nbsp;currentMaxBid.getAmount().compareTo(bidAmount)&nbsp;&gt;&nbsp;</span><span class=number>0</span><span>)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Bid&nbsp;too&nbsp;low."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;is&nbsp;active </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;!state.equals(ItemState.ACTIVE)&nbsp;) &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Auction&nbsp;is&nbsp;not&nbsp;active&nbsp;yet."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;still&nbsp;valid </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;</span><span class=keyword>this</span><span>.getEndDate().before(&nbsp;</span><span class=keyword>new</span><span>&nbsp;Date()&nbsp;)&nbsp;) &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Can't&nbsp;place&nbsp;new&nbsp;bid,&nbsp;auction&nbsp;already&nbsp;ended."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Create&nbsp;new&nbsp;Bid </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;newBid&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;Bid(bidAmount,&nbsp;</span><span class=keyword>this</span><span>,&nbsp;bidder); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Place&nbsp;bid&nbsp;for&nbsp;this&nbsp;Item </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>this</span><span>.addBid(newBid); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemDao.update(</span><span class=keyword>this</span><span>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;&nbsp;调用DAO进行显式持久化 </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;newBid; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&#160;</p>
<p>在这种模型中，所有的业务逻辑全部都在Item中，事务管理也在Item中实现。</p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/681720.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43630/" target="_blank">[新闻]Silverlight 2 SDK中文版发布</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>Pattern of Domain Object(2)</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681718.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Tue, 20 Mar 2007 13:11:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681718.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/681718.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681718.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/681718.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/681718.html</trackback:ping><description><![CDATA[<p>第二种模型，也就是Martin Fowler指的rich domain object是下面这样子的：</p>
<p>一个带有业务逻辑的实体类，即domain object是Item <br>一个DAO接口ItemDao <br>一个DAO实现ItemDaoHibernateImpl <br>一个业务逻辑对象ItemManager</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;Item&nbsp;</span><span class=keyword>implements</span><span>&nbsp;Serializable&nbsp;{ &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;&nbsp;所有的属性和getter/setter方法同上，省略 </span><span>&nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;placeBid(User&nbsp;bidder,&nbsp;MonetaryAmount&nbsp;bidAmount, &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMaxBid,&nbsp;Bid&nbsp;currentMinBid) &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throws</span><span>&nbsp;BusinessException&nbsp;{ &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Check&nbsp;highest&nbsp;bid&nbsp;(can&nbsp;also&nbsp;be&nbsp;a&nbsp;different&nbsp;Strategy&nbsp;(pattern)) </span><span>&nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(currentMaxBid&nbsp;!=&nbsp;</span><span class=keyword>null</span><span>&nbsp;&amp;&amp;&nbsp;currentMaxBid.getAmount().compareTo(bidAmount)&nbsp;&gt;&nbsp;</span><span class=number>0</span><span>)&nbsp;{ &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Bid&nbsp;too&nbsp;low."</span><span>); &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;is&nbsp;active </span><span>&nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;!state.equals(ItemState.ACTIVE)&nbsp;) &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Auction&nbsp;is&nbsp;not&nbsp;active&nbsp;yet."</span><span>); &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;still&nbsp;valid </span><span>&nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;</span><span class=keyword>this</span><span>.getEndDate().before(&nbsp;</span><span class=keyword>new</span><span>&nbsp;Date()&nbsp;)&nbsp;) &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Can't&nbsp;place&nbsp;new&nbsp;bid,&nbsp;auction&nbsp;already&nbsp;ended."</span><span>); &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Create&nbsp;new&nbsp;Bid </span><span>&nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;newBid&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;Bid(bidAmount,&nbsp;</span><span class=keyword>this</span><span>,&nbsp;bidder); &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Place&nbsp;bid&nbsp;for&nbsp;this&nbsp;Item </span><span>&nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>this</span><span>.getBids.add(newBid);&nbsp;&nbsp;</span><span class=comment>//&nbsp;请注意这一句，透明的进行了持久化，但是不能在这里调用ItemDao，Item不能对ItemDao产生依赖！ </span><span>&nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;newBid; &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span>
    <li class=""><span>}&nbsp;&nbsp;</span> </li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&nbsp;</p>
<p>竞标这个业务逻辑被放入到Item中来。请注意this.getBids.add(newBid); 如果没有Hibernate或者JDO这种O/R Mapping的支持，我们是无法实现这种透明的持久化行为的。但是请注意，Item里面不能去调用ItemDAO，对ItemDAO产生依赖！</p>
<p>ItemDao和ItemDaoHibernateImpl的代码同上，省略。</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;ItemManager&nbsp;{&nbsp; &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;ItemDao&nbsp;itemDao;&nbsp; &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>void</span><span>&nbsp;setItemDao(ItemDao&nbsp;itemDao)&nbsp;{&nbsp;</span><span class=keyword>this</span><span>.itemDao&nbsp;=&nbsp;itemDao;}&nbsp; &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;loadItemById(Long&nbsp;id)&nbsp;{&nbsp; &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemDao.loadItemById(id);&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp; &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Collection&nbsp;listAllItems()&nbsp;{&nbsp; &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;&nbsp;itemDao.findAll();&nbsp; &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp; &nbsp;&nbsp;</span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;placeBid(Item&nbsp;item,&nbsp;User&nbsp;bidder,&nbsp;MonetaryAmount&nbsp;bidAmount,&nbsp; &nbsp;&nbsp;</span></span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMaxBid,&nbsp;Bid&nbsp;currentMinBid)&nbsp;</span><span class=keyword>throws</span><span>&nbsp;BusinessException&nbsp;{&nbsp; &nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.placeBid(bidder,&nbsp;bidAmount,&nbsp;currentMaxBid,&nbsp;currentMinBid); &nbsp;&nbsp;</span>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemDao.update(item);&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;必须显式的调用DAO，保持持久化 </span><span>&nbsp;&nbsp;</span></span>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span>
    <li class=alt><span>}&nbsp;&nbsp;</span> </li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&nbsp;</p>
<p>在第二种模型中，placeBid业务逻辑是放在Item中实现的，而loadItemById和findAll业务逻辑是放在ItemManager中实现的。不过值得注意的是，即使placeBid业务逻辑放在Item中，你仍然需要在ItemManager中简单的封装一层，以保证对placeBid业务逻辑进行事务的管理和持久化的触发。</p>
<p>这种模型是Martin Fowler所指的真正的domain model。在这种模型中，有三个业务逻辑方法：placeBid，loadItemById和findAll，现在的问题是哪个逻辑应该放在Item中，哪个逻辑应该放在ItemManager中。在我们这个例子中，placeBid放在Item中(但是ItemManager也需要对它进行简单的封装)，loadItemById和findAll是放在ItemManager中的。</p>
<p>切分的原则是什么呢？ Rod Johnson提出原则是&#8220;case by case&#8221;，可重用度高的，和domain object状态密切关联的放在Item中，可重用度低的，和domain object状态没有密切关联的放在ItemManager中。</p>
<p><span style="COLOR: red">我提出的原则是：看业务方法是否显式的依赖持久化。</span></p>
<p>Item的placeBid这个业务逻辑方法没有显式的对持久化ItemDao接口产生依赖，所以要放在Item中。<span style="COLOR: red">请注意，如果脱离了Hibernate这个持久化框架，Item这个domain object是可以进行单元测试的，他不依赖于Hibernate的持久化机制。它是一个独立的，可移植的，完整的，自包含的域对象</span>。</p>
<p>而loadItemById和findAll这两个业务逻辑方法是必须显式的对持久化ItemDao接口产生依赖，否则这个业务逻辑就无法完成。如果你要把这两个方法放在Item中，那么Item就无法脱离Hibernate框架，无法在Hibernate框架之外独立存在。</p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/681718.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43630/" target="_blank">[新闻]Silverlight 2 SDK中文版发布</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>Pattern of Domain Object (1)</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681715.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Tue, 20 Mar 2007 13:08:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681715.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/681715.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/20/681715.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/681715.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/681715.html</trackback:ping><description><![CDATA[<p>第一种模型：只有getter/setter方法的纯数据类，所有的业务逻辑完全由business object来完成(又称TransactionScript)，这种模型下的domain object被Martin Fowler称之为&#8220;贫血的domain object&#8221;。下面用举一个具体的代码来说明，代码来自Hibernate的caveatemptor，但经过我的改写：</p>
<p>一个实体类叫做Item，指的是一个拍卖项目 <br>一个DAO接口类叫做ItemDao <br>一个DAO接口实现类叫做ItemDaoHibernateImpl <br>一个业务逻辑类叫做ItemManager(或者叫做ItemService) </p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;Item&nbsp;</span><span class=keyword>implements</span><span>&nbsp;Serializable&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Long&nbsp;id&nbsp;=&nbsp;</span><span class=keyword>null</span><span>; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;</span><span class=keyword>int</span><span>&nbsp;version; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;String&nbsp;name; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;User&nbsp;seller; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;String&nbsp;description; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;MonetaryAmount&nbsp;initialPrice; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;MonetaryAmount&nbsp;reservePrice; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Date&nbsp;startDate; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Date&nbsp;endDate; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Set&nbsp;categorizedItems&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;HashSet(); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Collection&nbsp;bids&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;ArrayList(); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Bid&nbsp;successfulBid; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;ItemState&nbsp;state; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;User&nbsp;approvedBy; &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Date&nbsp;approvalDatetime; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;Date&nbsp;created&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;Date(); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;&nbsp;getter/setter方法省略不写，避免篇幅太长 </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&#160;</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>interface</span><span>&nbsp;ItemDao&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Item&nbsp;getItemById(Long&nbsp;id); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Collection&nbsp;findAll(); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>void</span><span>&nbsp;updateItem(Item&nbsp;item); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&#160;</p>
<p>ItemDao定义持久化操作的接口，用于隔离持久化代码。</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;ItemDaoHibernateImpl&nbsp;</span><span class=keyword>implements</span><span>&nbsp;ItemDao&nbsp;</span><span class=keyword>extends</span><span>&nbsp;HibernateDaoSupport&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Item&nbsp;getItemById(Long&nbsp;id)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;(Item)&nbsp;getHibernateTemplate().load(Item.</span><span class=keyword>class</span><span>,&nbsp;id); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Collection&nbsp;findAll()&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;(List)&nbsp;getHibernateTemplate().find(</span><span class=string>"from&nbsp;Item"</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>void</span><span>&nbsp;updateItem(Item&nbsp;item)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getHibernateTemplate().update(item); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div>
<script>render_code();</script>
<br>ItemDaoHibernateImpl完成具体的持久化工作，请注意，数据库资源的获取和释放是在ItemDaoHibernateImpl里面处理的，每个DAO方法调用之前打开Session，DAO方法调用之后，关闭Session。(Session放在ThreadLocal中，保证一次调用只打开关闭一次)
<p>&#160;</p>
<p>
<div class=code_title>java 代码</div>
<div class=code_div>
<div class=dp-highlighter>
<div class=bar></div>
<ol class=dp-j>
    <li class=alt><span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>class</span><span>&nbsp;ItemManager&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>private</span><span>&nbsp;ItemDao&nbsp;itemDao; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;</span><span class=keyword>void</span><span>&nbsp;setItemDao(ItemDao&nbsp;itemDao)&nbsp;{&nbsp;</span><span class=keyword>this</span><span>.itemDao&nbsp;=&nbsp;itemDao;} &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;loadItemById(Long&nbsp;id)&nbsp;{&nbsp; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemDao.loadItemById(id); &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Collection&nbsp;listAllItems()&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;&nbsp;itemDao.findAll(); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>public</span><span>&nbsp;Bid&nbsp;placeBid(Item&nbsp;item,&nbsp;User&nbsp;bidder,&nbsp;MonetaryAmount&nbsp;bidAmount, &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMaxBid,&nbsp;Bid&nbsp;currentMinBid)&nbsp;</span><span class=keyword>throws</span><span>&nbsp;BusinessException&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(currentMaxBid&nbsp;!=&nbsp;</span><span class=keyword>null</span><span>&nbsp;&amp;&amp;&nbsp;currentMaxBid.getAmount().compareTo(bidAmount)&nbsp;&gt;&nbsp;</span><span class=number>0</span><span>)&nbsp;{ &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Bid&nbsp;too&nbsp;low."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;is&nbsp;active </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;!state.equals(ItemState.ACTIVE)&nbsp;) &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Auction&nbsp;is&nbsp;not&nbsp;active&nbsp;yet."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Auction&nbsp;still&nbsp;valid </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>if</span><span>&nbsp;(&nbsp;item.getEndDate().before(&nbsp;</span><span class=keyword>new</span><span>&nbsp;Date()&nbsp;)&nbsp;) &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>throw</span><span>&nbsp;</span><span class=keyword>new</span><span>&nbsp;BusinessException(</span><span class=string>"Can't&nbsp;place&nbsp;new&nbsp;bid,&nbsp;auction&nbsp;already&nbsp;ended."</span><span>); &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Create&nbsp;new&nbsp;Bid </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;newBid&nbsp;=&nbsp;</span><span class=keyword>new</span><span>&nbsp;Bid(bidAmount,&nbsp;item,&nbsp;bidder); &nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;Place&nbsp;bid&nbsp;for&nbsp;this&nbsp;Item </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;item.getBids().add(newBid); &nbsp;&nbsp;</span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;itemDao.update(item);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=comment>//&nbsp;&nbsp;调用DAO完成持久化操作 </span><span>&nbsp;&nbsp;</span></span></li>
    <li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span class=keyword>return</span><span>&nbsp;newBid; &nbsp;&nbsp;</span></span></li>
    <li class=alt><span>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</span></li>
    <li class=""><span>}&nbsp;&nbsp;</span></li>
</ol>
</div>
</div>
<script>render_code();</script>
<p>&#160;</p>
<p>事务的管理是在ItemManger这一层完成的，ItemManager实现具体的业务逻辑。除了常见的和CRUD有关的简单逻辑之外，这里还有一个placeBid的逻辑，即项目的竞标。</p>
<p>以上是一个完整的第一种模型的示例代码。在这个示例中，placeBid，loadItemById，findAll等等业务逻辑统统放在ItemManager中实现，而Item只有getter/setter方法。</p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/681715.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43630/" target="_blank">[新闻]Silverlight 2 SDK中文版发布</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>结构型模式 组合模式</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669469.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Fri, 09 Mar 2007 08:23:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669469.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/669469.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669469.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/669469.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/669469.html</trackback:ping><description><![CDATA[<strong><font color=#aa0000>Composite</font></strong> <br><strong><font size=2>Definition</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image10 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" height=1 src="http://www.cnblogs.com/Images/pixel.gif" width=1> </td>
            <td><font face="arial, helvetica" size=2>Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. </font></td>
        </tr>
    </tbody>
</table>
<strong><font size=2>UML class diagram</font></strong> <br><img height=325 alt=组合模式 hspace=5 src="http://www.cnblogs.com/images/cnblogs_com/lw/composite.gif" width=449 align=baseline longDesc=组合模式> <br><strong><font size=2>Participants</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image11 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" height=1 src="http://www.cnblogs.com/Images/pixel.gif" width=1> </td>
            <td><font face="arial, helvetica" size=2>&nbsp;&nbsp;&nbsp;The classes and/or objects participating in the Composite pattern are: <br><br>
            <ul>
                <li><font color=#aa0000><strong>Component </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(DrawingElement)</strong></font>
                <ul>
                    <li>declares the interface for objects in the composition. </li>
                    <li>implements default behavior for the interface common to all classes, as appropriate. </li>
                    <li>declares an interface for accessing and managing its child components. </li>
                    <li>(optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Leaf </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(PrimitiveElement)</strong></font>
                <ul>
                    <li>represents leaf objects in the composition. A leaf has no children. </li>
                    <li>defines behavior for primitive objects in the composition. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Composite </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(CompositeElement)</strong></font>
                <ul>
                    <li>defines behavior for components having children. </li>
                    <li>stores child components. </li>
                    <li>implements child-related operations in the Component interface. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Client</strong></font>&nbsp;&nbsp;<font face=Arial color=#007733><strong>(CompositeApp)</strong></font>
                <ul>
                    <li>manipulates objects in the composition through the Component interface. </li>
                </ul>
                </li>
            </ul>
            </font></td>
        </tr>
    </tbody>
</table>
<br>This <font color=#bb0000>structural</font> code demonstrates the Composite pattern which allows the creation of a tree structure in which individual nodes are accessed uniformly whether they are leaf nodes or branch (composite) nodes. <br><br><font face="Courier New"><code><font color=#0000ff>using</font> System;</code> <br><code><font color=#0000ff>using</font> System.Text;</code> <br><code><font color=#0000ff>using</font> System.Collections;</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Component"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">abstract</font></font><font face="Courier New"> <font color=#0000ff>class</font> Component</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>string</font> name;</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Constructors</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Component( <font color=#0000ff>string</font> name )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.name = name;</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Add(Component c);</code> <br><code><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Remove( Component c );</code> <br><code><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> depth );</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Composite"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Composite : Component</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> ArrayList children = <font color=#0000ff>new</font> ArrayList();</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Constructors</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Composite( <font color=#0000ff>string</font> name ) : <font color=#0000ff>base</font>( name ) {}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Add( Component component )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>children.Add( component );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Remove( Component component )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>children.Remove( component );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> depth )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( <font color=#0000ff>new</font> String( '-', depth ) + name );</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Display each of the node's children</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; foreach</font>( Component component <font color=#0000ff>in</font> children )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>component.Display( depth + 2 );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Leaf"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Leaf : Component</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Constructors</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Leaf( <font color=#0000ff>string</font> name ) : <font color=#0000ff>base</font>( name ) {}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Add( Component c )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Cannot add to a leaf");</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Remove( Component c )</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Cannot remove from a leaf");</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> depth )</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( <font color=#0000ff>new</font> String( '-', depth ) + name );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><code><font color=#808080><br><font face="Courier New">///</font></font><font face="Courier New"><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></font></code> <br><font face="Courier New"><code><font color=#808080>///</font><font color=#008000> Client test</font></code> <br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code> <br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> Client</code> <br><code>{</code> <br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create a tree structure</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Composite root = <font color=#0000ff>new</font> Composite( "root" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( <font color=#0000ff>new</font> Leaf( "Leaf A" ));</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( <font color=#0000ff>new</font> Leaf( "Leaf B" ));</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Composite comp = <font color=#0000ff>new</font> Composite( "Composite X" );</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New">comp.Add( <font color=#0000ff>new</font> Leaf( "Leaf XA" ) );</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>comp.Add( <font color=#0000ff>new</font> Leaf( "Leaf XB" ) );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( comp );</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New">root.Add( <font color=#0000ff>new</font> Leaf( "Leaf C" ));</font></code> <br><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Add and remove a leaf</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Leaf l = <font color=#0000ff>new</font> Leaf( "Leaf D" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( l );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Remove( l );</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Recursively display nodes</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Display( 1 );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code></font> <br><br>
<p>This <font color=#00aa55>real-world</font> code demonstrates the Composite pattern used in building a graphical tree structure made up of primitive nodes (lines, circles, etc) and composite nodes (groups of drawing elements that make up more complex elements). <br><br><br><code><font face="Courier New"><font color=#0000ff>using</font> System; <br><font color=#0000ff>using</font> System.Collections; <br></font><font color=#008000><br><font face="Courier New">// "Component"</font></font> <br><font color=#0000ff><br><font face="Courier New">abstract</font></font><font face="Courier New"> <font color=#0000ff>class</font> DrawingElement <br>{ <br><font color=#0000ff>&nbsp; </font><font color=#008000>// Fields</font> <br><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>string</font> name; <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font><font face="Courier New"><font color=#008000>// Constructors</font> <br><font color=#0000ff>&nbsp; public</font> DrawingElement( <font color=#0000ff>string</font> name ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.name = name; <br><font color=#0000ff>&nbsp; </font>} <br>&nbsp; <br><font color=#0000ff>&nbsp; </font><font color=#008000>// Methods</font> <br><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Add( DrawingElement d ); <br><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Remove( DrawingElement d ); <br><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> indent ); <br>} <br></font><font color=#008000><br><font face="Courier New">// "Leaf"</font></font> <br><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> PrimitiveElement : DrawingElement <br>{ <br><font color=#0000ff>&nbsp; </font><font color=#008000>// Constructors</font> <br><font color=#0000ff>&nbsp; public</font> PrimitiveElement( <font color=#0000ff>string</font> name ) : <font color=#0000ff>base</font>( name ) {} <br><br><font color=#0000ff>&nbsp; </font><font color=#008000>// Methods</font> <br><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Add( DrawingElement c ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Cannot Add"); <br><font color=#0000ff>&nbsp; </font>} <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Remove( DrawingElement c ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Cannot Remove"); <br><font color=#0000ff>&nbsp; </font>} <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> indent ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( <font color=#0000ff>new</font> String( '-', indent ) +&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " draw a {0}", name ); <br><font color=#0000ff>&nbsp; </font>} <br>} <br></font><font color=#008000><br><font face="Courier New">// "Composite"</font></font> <br><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> CompositeElement : DrawingElement <br>{ <br><font color=#0000ff>&nbsp; </font><font color=#008000>// Fields</font> <br><font color=#0000ff>&nbsp; private</font> ArrayList elements = <font color=#0000ff>new</font> ArrayList(); <br>&nbsp; <br><font color=#0000ff>&nbsp; </font><font color=#008000>// Constructors</font> <br><font color=#0000ff>&nbsp; public</font> CompositeElement( <font color=#0000ff>string</font> name ) <br><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color=#0000ff>&nbsp;&nbsp; </font>: <font color=#0000ff>base</font>( name ) {} <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font><font face="Courier New"><font color=#008000>// Methods</font> <br><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Add( DrawingElement d ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>elements.Add( d ); <br><font color=#0000ff>&nbsp; </font>} <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Remove( DrawingElement d ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>elements.Remove( d ); <br><font color=#0000ff>&nbsp; </font>} <br></font><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display( <font color=#0000ff>int</font> indent ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( <font color=#0000ff>new</font> String( '-', indent ) + <br><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>"+ " + name ); <br></font><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New"><font color=#008000>// Display each child element on this node</font> <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; foreach</font>( DrawingElement c <font color=#0000ff>in</font> elements ) <br><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>c.Display( indent + 2 ); <br><font color=#0000ff>&nbsp; </font>} <br>} <br>&nbsp; <br><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font> <br><font color=#808080>///</font><font color=#008000>&nbsp; CompositeApp test</font> <br><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font> <br><font color=#0000ff>public</font> <font color=#0000ff>class</font> CompositeApp <br>{ <br><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args ) <br><font color=#0000ff>&nbsp; </font>{ <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font><font color=#008000>// Create a tree structure</font> <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>CompositeElement root = <font color=#0000ff>new</font> &nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CompositeElement( "Picture" ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( <font color=#0000ff>new</font> PrimitiveElement( "Red Line" )); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( <font color=#0000ff>new</font> PrimitiveElement( "Blue Circle" )); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( <font color=#0000ff>new</font> PrimitiveElement( "Green Box" )); <br></font><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New">CompositeElement comp = <font color=#0000ff>new</font> &nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CompositeElement( "Two Circles" ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>comp.Add( <font color=#0000ff>new</font> PrimitiveElement( "Black Circle" ) ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>comp.Add( <font color=#0000ff>new</font> PrimitiveElement( "White Circle" ) ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( comp ); <br></font><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New"><font color=#008000>// Add and remove a PrimitiveElement</font> <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>PrimitiveElement l = <font color=#0000ff>new</font> &nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrimitiveElement( "Yellow Line" ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Add( l ); <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Remove( l ); <br></font><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font><font face="Courier New"><font color=#008000>// Recursively display nodes</font> <br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>root.Display( 1 ); <br><font color=#0000ff>&nbsp; </font>} <br>}</font></code> </p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/669469.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43629/" target="_blank">[新闻][译稿]微软将 jQuery IntelliSense整合到Visual Studio</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>结构型模式，装饰模式</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669463.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Fri, 09 Mar 2007 08:17:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669463.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/669463.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/09/669463.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/669463.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/669463.html</trackback:ping><description><![CDATA[<strong><font face=Arial size=2>Definition</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image10 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. </font></td>
        </tr>
    </tbody>
</table>
<strong><font face=Arial size=2>UML class diagram</font></strong> <br><img height=356 alt=decorator[1].gif src="http://www.cnblogs.com/images/cnblogs_com/lw/decorator[1].gif" width=431 border=0><br><strong><font face=Arial size=2>Participants</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image11 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>&nbsp;&nbsp;&nbsp;The classes and/or objects participating in the Decorator pattern are:<br><br>
            <ul>
                <li><font color=#aa0000><strong>Component </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(LibraryItem)</strong></font>
                <ul>
                    <li>defines the interface for objects that can have responsibilities added to them dynamically. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>ConcreteComponent </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Book, Video)</strong></font>
                <ul>
                    <li>defines an object to which additional responsibilities can be attached. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Decorator </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Decorator)</strong></font>
                <ul>
                    <li>maintains a reference to a Component object and defines an interface that conforms to Component's interface. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>ConcreteDecorator </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Borrowable)</strong></font>
                <ul>
                    <li>adds responsibilities to the component. </li>
                </ul>
                </li>
            </ul>
            </font></td>
        </tr>
    </tbody>
</table>
<font size=2><font face=Arial><strong>Sample code in C#</strong> <br>This <font color=#bb0000>structural</font> code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object. <br>
<table cellSpacing=0 cellPadding=0 width="100%" bgColor=#fffaef border=0>
    <tbody>
        <tr>
            <td><code><font color=green>// Decorator pattern -- Structural example </font></code></td>
        </tr>
        <tbody id=Structure>
            <tr>
                <td><br><code><font color=#0000ff>using</font> System;</code><br><font color=#008000><code><br>// "Component"</code></font><br><code><font color=#0000ff><br>abstract</font> <font color=#0000ff>class</font> Component</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Operation();</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteComponent"</code></font><br><code><font color=#0000ff><br>class</font> ConcreteComponent : Component</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Operation()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("ConcreteComponent.Operation()");</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "Decorator"</code></font><br><code><font color=#0000ff><br>abstract</font> <font color=#0000ff>class</font> Decorator : Component</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; protected</font> Component component;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>void</font> SetComponent( Component component )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.component = component;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Operation()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; if</font>( component != <font color=#0000ff>null</font> )</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>component.Operation();</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteDecoratorA"</code></font><br><code><font color=#0000ff><br>class</font> ConcreteDecoratorA : Decorator</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>string</font> addedState;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Operation()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; base</font>.Operation();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>addedState = "new state";</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("ConcreteDecoratorA.Operation()");</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteDecoratorB"</code></font><br><code><font color=#0000ff><br>class</font> ConcreteDecoratorB : Decorator</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Operation()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; base</font>.Operation();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>AddedBehavior();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("ConcreteDecoratorB.Operation()");</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; void</font> AddedBehavior()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><code><font color=#808080><br>///</font><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></code><br><code><font color=#808080>///</font><font color=#008000> Client test</font></code><br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code><br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> Client</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create ConcreteComponent and two Decorators</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>ConcreteComponent c = <font color=#0000ff>new</font> ConcreteComponent();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>ConcreteDecoratorA d1 = <font color=#0000ff>new</font> ConcreteDecoratorA();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>ConcreteDecoratorB d2 = <font color=#0000ff>new</font> ConcreteDecoratorB();</code><br><code><font color=#0000ff><br>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Link decorators</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>d1.SetComponent( c );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>d2.SetComponent( d1 );<br></code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>d2.Operation();</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code></td>
            </tr>
        </tbody>
    </table>
    </font></font>
    <p>This <font color=#00aa55>real-world</font> code demonstrates the Decorator pattern in which 'borrowable' functionality is added to existing library items (books and videos). <br>
    <table cellSpacing=0 cellPadding=0 width="100%" bgColor=#fffaef border=0>
        <tbody>
            <tr>
                <td><code><font color=green>// Decorator pattern -- Real World example </font></code></td>
            </tr>
            <tbody id=RealWorld>
                <tr>
                    <td><br><code><font color=#0000ff>using</font> System;</code><br><code><font color=#0000ff>using</font> System.Collections;</code><br><font color=#008000><code><br>// "Component"</code></font><br><code><font color=#0000ff><br>abstract</font> <font color=#0000ff>class</font> LibraryItem</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>int</font> numCopies;</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Properties</code></font><br><font size=2></font><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>int</font> NumCopies</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; get</font>{ <font color=#0000ff>return</font> numCopies; }</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; set</font>{ numCopies = <font color=#0000ff>value</font>; }</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>abstract</font> <font color=#0000ff>void</font> Display();</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteComponent"</code></font><br><code><font color=#0000ff><br>class</font> Book : LibraryItem</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>string</font> author;</code><br><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>string</font> title;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Constructors</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Book(<font color=#0000ff>string</font> author,<font color=#0000ff>string</font> title,<font color=#0000ff>int</font> numCopies)</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.author = author;</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.title = title;</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.NumCopies = numCopies;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "\nBook ------ " );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " Author: {0}", author );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " Title: {0}", title );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " # Copies: {0}", NumCopies );</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteComponent"</code></font><br><code><font color=#0000ff><br>class</font> Video : LibraryItem</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>string</font> director;</code><br><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>string</font> title;</code><br><code><font color=#0000ff>&nbsp; private</font> <font color=#0000ff>int</font> playTime;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Constructor</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Video( <font color=#0000ff>string</font> director, <font color=#0000ff>string</font> title,</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int</font> numCopies, <font color=#0000ff>int</font> playTime )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.director = director;</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.title = title;</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.NumCopies = numCopies;</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.playTime = playTime;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;</font> Console.WriteLine( "\nVideo ----- " );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " Director: {0}", director );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " Title: {0}", title );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " # Copies: {0}", NumCopies );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " Playtime: {0}", playTime );</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "Decorator"</code></font><br><code><font color=#0000ff><br>abstract</font> <font color=#0000ff>class</font> Decorator : LibraryItem</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; protected</font> LibraryItem libraryItem;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Constructors</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Decorator ( LibraryItem libraryItem )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.libraryItem = libraryItem;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>libraryItem.Display();</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "ConcreteDecorator"</code></font><br><code><font color=#0000ff><br>class</font> Borrowable : Decorator</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; protected</font> ArrayList borrowers = <font color=#0000ff>new</font> ArrayList();</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Constructors</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Borrowable( LibraryItem libraryItem )</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>: <font color=#0000ff>base</font>( libraryItem ) {}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>void</font> BorrowItem( <font color=#0000ff>string</font> name )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>borrowers.Add( name );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>libraryItem.NumCopies--;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>void</font> ReturnItem( <font color=#0000ff>string</font> name )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>borrowers.Remove( name );</code><br><code><font color=#0000ff>&nbsp;&nbsp;</font> <font color=#0000ff>&nbsp; </font>libraryItem.NumCopies++;</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>override</font> <font color=#0000ff>void</font> Display()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; base</font>.Display();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; foreach</font>( <font color=#0000ff>string</font> borrower <font color=#0000ff>in</font> borrowers )</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( " borrower: {0}", borrower );</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font size=2>&nbsp;<br></font><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></code><br><code><font color=#808080>///</font><font color=#008000> &nbsp;DecoratorApp test</font></code><br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code><br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> DecoratorApp</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args )</code><br><code><font color=#0000ff>&nbsp; </font>{<br><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create book and video and display</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Book book = <font color=#0000ff>new</font> Book( "Schnell", "My Home", 10 );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Video video = <font color=#0000ff>new</font> Video( "Spielberg",</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>"Schindler's list", 23, 60 );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>book.Display();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>video.Display();</code><br><code><font color=#0000ff><br>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Make video borrowable, then borrow and display</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "\nVideo made borrowable:" );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Borrowable borrowvideo = <font color=#0000ff>new</font> Borrowable( video );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>borrowvideo.BorrowItem( "Cindy Lopez" );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>borrowvideo.BorrowItem( "Samuel King" );</code><br><code><font color=#0000ff><br>&nbsp;&nbsp;&nbsp; </font>borrowvideo.Display();</code><br><code><font color=#0000ff><br>&nbsp; </font>}</code><br><code>}</code> </td>
                </tr>
            </tbody>
        </table>
        </p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/669463.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43629/" target="_blank">[新闻][译稿]微软将 jQuery IntelliSense整合到Visual Studio</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>结构型模式 适配器模式</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668564.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Thu, 08 Mar 2007 12:39:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668564.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/668564.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668564.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/668564.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/668564.html</trackback:ping><description><![CDATA[<strong><font color=#aa0000>Adapter <br></font><font size=2>Definition</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image6 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" height=1 src="http://www.cnblogs.com/Images/pixel.gif" width=1> </td>
            <td><font face="arial, helvetica" size=2>Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. </font></td>
        </tr>
    </tbody>
</table>
<p><strong><font size=2>UML class diagram</font></strong> <br><img height=249 alt=适配器模式 hspace=5 src="http://www.cnblogs.com/images/cnblogs_com/lw/adapter.gif" width=428 align=baseline longDesc=适配器模式> </p>
<p>&#160;</p>
<p><strong><font size=2>Participants</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image44 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" height=1 src="http://www.cnblogs.com/Images/pixel.gif" width=1> </td>
            <td><font face="arial, helvetica" size=2>&nbsp;&nbsp;&nbsp;The classes and/or objects participating in the Adapter pattern are: <br><br>
            <ul>
                <li><font color=#aa0000><strong>Target </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(ChemicalCompound)</strong></font>
                <ul>
                    <li>defines the domain-specific interface that Client uses. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Adapter </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Compound)</strong></font>
                <ul>
                    <li>adapts the interface Adaptee to the Target interface. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Adaptee </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(ChemicalDatabank)</strong></font>
                <ul>
                    <li>defines an existing interface that needs adapting. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Client </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(AdapterApp)</strong></font>
                <ul>
                    <li>collaborates with objects conforming to the Target interface. </li>
                </ul>
                </li>
            </ul>
            </font></td>
        </tr>
    </tbody>
</table>
<br></p>
<p>This <font color=#bb0000>structural</font> code demonstrates the Adapter pattern which maps the interface of one class onto another so that they can work together. These incompatible classes may come from different libraries or frameworks. </p>
<p><br><br><font face="Courier New"><code><font color=#0000ff>using</font> System;</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Target"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Target</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; virtual</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Request()</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Normal implementation goes here</code></font> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Adapter"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Adapter : Target</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> Adaptee adaptee = <font color=#0000ff>new</font> Adaptee();</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Request()</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Possibly do some data manipulation</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// and then call SpecificRequest</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>adaptee.SpecificRequest();</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Adaptee"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Adaptee</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>void</font> SpecificRequest()</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Called SpecificRequest()" );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><code><font color=#808080><br><font face="Courier New">///</font></font><font face="Courier New"><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></font></code> <br><font face="Courier New"><code><font color=#808080>///</font><font color=#008000> Client test</font></code> <br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code> <br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> Client</code> <br><code>{</code> <br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main(<font color=#0000ff>string</font>[] args)</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create adapter and place a request</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Target t = <font color=#0000ff>new</font> Adapter();</code> <br></font><font face="Courier New"><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>t.Request(); <br></code><br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code></font> <br><br>This <font color=#00aa55>real-world</font> code demonstrates the use of a legacy chemical databank. Chemical compound objects access the databank through an Adapter interface. <br><br><br><font face="Courier New"><code><font color=#0000ff>using</font> System; <br></code><br></font><font face="Courier New"><font color=#008000><code>// "Target" <br></code></font><br><code><font color=#0000ff>class</font> ChemicalCompound</code> <br><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>string</font> name;</code> <br><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>float</font> boilingPoint;</code> <br><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>float</font> meltingPoint;</code> <br><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>double</font> molecularWeight;</code> <br><code><font color=#0000ff>&nbsp; protected</font> <font color=#0000ff>string</font> molecularFormula;</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Constructors</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> ChemicalCompound( <font color=#0000ff>string</font> name )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; this</font>.name = name;</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Properties</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>float</font> BoilingPoint</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; get</font>{ <font color=#0000ff>return</font> boilingPoint; }</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>float</font> MeltingPoint</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; get</font>{ <font color=#0000ff>return</font> meltingPoint; }</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>double</font> MolecularWeight</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; get</font>{ <font color=#0000ff>return</font> molecularWeight; }</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>string</font> MolecularFormula</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; get</font>{ <font color=#0000ff>return</font> molecularFormula; }</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Adapter"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> Compound : ChemicalCompound</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; private</font> ChemicalDatabank bank;</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Constructors</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> Compound( <font color=#0000ff>string</font> name ) : <font color=#0000ff>base</font>( name )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Adaptee</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>bank = <font color=#0000ff>new</font> ChemicalDatabank();</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Adaptee request methods</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>boilingPoint = bank.GetCriticalPoint( name, "B" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>meltingPoint = bank.GetCriticalPoint( name, "M" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>molecularWeight = bank.GetMolecularWeight( name );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>molecularFormula = bank.GetMolecularStructure( name );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Methods</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>void</font> Display()</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("\nCompound: {0} ------ ",name );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine(" Formula: {0}",MolecularFormula);</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine(" Weight : {0}",MolecularWeight );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine(" Melting Pt: {0}",MeltingPoint );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine(" Boiling Pt: {0}",BoilingPoint );</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><font color=#008000><code><br><font face="Courier New">// "Adaptee"</font></code></font> <br><code><font color=#0000ff><br><font face="Courier New">class</font></font><font face="Courier New"> ChemicalDatabank</font></code> <br><font face="Courier New"><code>{</code> <br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods -- the Databank 'legacy API'</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>float</font> GetCriticalPoint( <font color=#0000ff>string</font> compound, <font color=#0000ff>string</font> point )</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; float</font> temperature = 0.0F;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Melting Point</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; if</font>( point == "M" )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch</font>( compound.ToLower() )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "water": temperature = 0.0F; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "benzene" : temperature = 5.5F; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "alcohol": temperature = -114.1F; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Boiling Point</code></font> <br><font size=2></font><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#0000ff><code>else</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch</font>( compound.ToLower() )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "water": temperature = 100.0F;<font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "benzene" : temperature = 80.1F; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "alcohol": temperature = 78.3F; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> temperature;</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>string</font> GetMolecularStructure( <font color=#0000ff>string</font> compound )</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; string</font> structure = "";</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; switch</font>( compound.ToLower() )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "water": structure = "H20"; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "benzene" : structure = "C6H6"; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "alcohol": structure = "C2H6O2"; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> structure;</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp; public</font></font><font face="Courier New"> <font color=#0000ff>double</font> GetMolecularWeight( <font color=#0000ff>string</font> compound )</font></code> <br><font face="Courier New"><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; double</font> weight = 0.0;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; switch</font>( compound.ToLower() )</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "water": weight = 18.015; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "benzene" : weight = 78.1134; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case</font> "alcohol": weight = 46.0688; <font color=#0000ff>break</font>;</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>}</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> weight;</code> <br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code> <br></font><code><font color=#808080><br><font face="Courier New">///</font></font><font face="Courier New"><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></font></code> <br><font face="Courier New"><code><font color=#808080>///</font><font color=#008000> AdapterApp test application</font></code> <br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code> <br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> AdapterApp</code> <br><code>{</code> <br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main(<font color=#0000ff>string</font>[] args)</code> <br><code><font color=#0000ff>&nbsp; </font>{</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Retrieve and display water characteristics</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Compound water = <font color=#0000ff>new</font> Compound( "Water" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>water.Display();</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Retrieve and display benzene characteristics</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Compound benzene = <font color=#0000ff>new</font> Compound( "Benzene" );</code> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>benzene.Display();</code> <br></font><code><font color=#0000ff><br><font face="Courier New">&nbsp;&nbsp;&nbsp; </font></font></code><font face="Courier New"><font color=#008000><code>// Retrieve and display alcohol characteristics</code></font> <br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Compound alcohol = <font color=#0000ff>new</font> Compound( "Alcohol" );</code> <br></font><font face="Courier New"><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>alcohol.Display(); <br></code><br><code><font color=#0000ff>&nbsp; </font>}</code> <br><code>}</code></font> <br></p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/668564.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43628/" target="_blank">[新闻]微软：不裁员也不削减研发开支</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>结构型模式 代理模式</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668560.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Thu, 08 Mar 2007 12:37:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668560.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/668560.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668560.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/668560.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/668560.html</trackback:ping><description><![CDATA[<strong><font face=Arial size=2>Definition</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image10 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>Provide a surrogate or placeholder for another object to control access to it. </font></td>
        </tr>
    </tbody>
</table>
<strong><font face=Arial size=2>UML class diagram</font></strong> <br><img height=252 alt=proxy[1].gif src="http://www.cnblogs.com/images/cnblogs_com/lw/proxy[1].gif" width=405 border=0><br><strong><font face=Arial size=2>Participants</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image11 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>&nbsp;&nbsp;&nbsp;The classes and/or objects participating in the Proxy pattern are:<br><br>
            <ul>
                <li><font color=#aa0000><strong>Proxy </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(MathProxy)</strong></font>
                <ul>
                    <li>maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same. </li>
                    <li>provides an interface identical to Subject's so that a proxy can be substituted for for the real subject. </li>
                    <li>controls access to the real subject and may be responsible for creating and deleting it. </li>
                    <li>other responsibilites depend on the kind of proxy:
                    <ul>
                        <li><font color=#aa0000><em>remote proxies</em></font> are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space. </li>
                        <li><font color=#aa0000><em>virtual proxies</em></font> may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent. </li>
                        <li><font color=#aa0000><em>protection proxies</em></font> check that the caller has the access permissions required to perform a request. </li>
                    </ul>
                    </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>Subject </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(IMath)</strong></font>
                <ul>
                    <li>defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>RealSubject </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Math)</strong></font>
                <ul>
                    <li>defines the real object that the proxy represents. </li>
                </ul>
                </li>
            </ul>
            </font></td>
        </tr>
    </tbody>
</table>
<br><font size=2><font face=Arial><strong>Sample code in C#</strong> <br>
<p>This <font color=#bb0000>structural</font> code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.&nbsp;<br>&nbsp;&nbsp;&nbsp;
<table cellSpacing=0 cellPadding=0 width="100%" bgColor=#fffaef border=0>
    <tbody>
        <tr>
            <td><code><font color=green>// Proxy pattern -- Structural example </font></code></td>
        </tr>
        <tbody id=Structure>
            <tr>
                <td><br><code><font color=#0000ff>using</font> System;</code><br><font color=#008000><code><br>// "Subject"</code></font><br><code><font color=#0000ff><br>abstract</font> <font color=#0000ff>class</font> Subject</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; abstract</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Request();</code><br><code>}</code><br><font color=#008000><code><br>// "RealSubject"</code></font><br><code><font color=#0000ff><br>class</font> RealSubject : Subject</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Request()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine("Called RealSubject.Request()");</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><font color=#008000><code><br>// "Proxy"</code></font><br><code><font color=#0000ff><br>class</font> Proxy : Subject</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><code><font color=#0000ff>&nbsp; </font>RealSubject realSubject;</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; override</font> <font color=#0000ff>public</font> <font color=#0000ff>void</font> Request()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Uses "lazy initialization"</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; if</font>( realSubject == <font color=#0000ff>null</font> )</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>realSubject = <font color=#0000ff>new</font> RealSubject();</code><br><code><font color=#0000ff><br>&nbsp;&nbsp;&nbsp; </font>realSubject.Request();</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><code><font color=#808080><br>///</font><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></code><br><code><font color=#808080>///</font><font color=#008000> Client test</font></code><br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code><br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> Client</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create proxy and request a service</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Proxy p = <font color=#0000ff>new</font> Proxy();</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp;</font> p.Request();</code><br><code><font color=#0000ff><br>&nbsp; </font>}</code><br><code>}</code> </td>
            </tr>
        </tbody>
    </table>
    </p>
    </font></font>
    <p>This <font color=#00aa55>real-world</font> code demonstrates the Remote Proxy pattern which provides a representative object that controls access to another object in a different AppDomain.&nbsp;<br><br>&nbsp;&nbsp;&nbsp;
    <table cellSpacing=0 cellPadding=0 width="100%" bgColor=#fffaef border=0>
        <tbody>
            <tr>
                <td><code><font color=green>// Proxy pattern -- Real World example </font></code></td>
            </tr>
            <tbody id=RealWorld>
                <tr>
                    <td><br><code><font color=#0000ff>using</font> System;</code><br><code><font color=#0000ff>using</font> System.Runtime.Remoting;</code><br><font color=#008000><code><br>// "Subject"</code></font><br><code><font color=#0000ff><br>public</font> <font color=#0000ff>interface</font> IMath</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; double</font> Add( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y );</code><br><code><font color=#0000ff>&nbsp; double</font> Sub( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y );</code><br><code><font color=#0000ff>&nbsp; double</font> Mul( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y );</code><br><code><font color=#0000ff>&nbsp; double</font> Div( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y );</code><br><code>}</code><br><font color=#008000><code><br>// "RealSubject"</code></font><br><code><font color=#0000ff><br>class</font> Math : MarshalByRefObject, IMath</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>double</font> Add( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y ){ <font color=#0000ff>return</font> x + y; }</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>double</font> Sub( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y ){ <font color=#0000ff>return</font> x - y; }</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>double</font> Mul( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y ){ <font color=#0000ff>return</font> x * y; }</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>double</font> Div( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y ){ <font color=#0000ff>return</font> x / y; }</code><br><code>}</code><br><font color=#008000><code><br>// Remote "Proxy Object"</code></font><br><code><font color=#0000ff><br>class</font> MathProxy : IMath</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Fields</code></font><br><code><font color=#0000ff>&nbsp; </font>Math math;</code><br><code><font color=#0000ff>&nbsp; </font></code><font color=#008000><code>// Constructors</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> MathProxy()</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create Math instance in a different AppDomain</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>AppDomain ad = System.AppDomain.CreateDomain(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "MathDomain",<font color=#0000ff>null</font>, <font color=#0000ff>null</font> );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>ObjectHandle o =&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ad.CreateInstance("Proxy_RealWorld", "Math", <font color=#0000ff>false</font>,</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Reflection.BindingFlags.CreateInstance,&nbsp; <font color=#0000ff><br></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color=#0000ff>null</font>, <font color=#0000ff>null</font>, <font color=#0000ff>null</font>,<font color=#0000ff>null</font>,<font color=#0000ff>null</font> );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>math = (Math) o.Unwrap();</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; </font></code><font color=#008000><code>// Methods</code></font><br><font size=2></font><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>double</font> Add( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> math.Add(x,y);</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>double</font> Sub( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> math.Sub(x,y);</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>double</font> Mul( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> math.Mul(x,y);</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code><font color=#0000ff><br>&nbsp; public</font> <font color=#0000ff>double</font> Div( <font color=#0000ff>double</font> x, <font color=#0000ff>double</font> y )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; return</font> math.Div(x,y);</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code><br><code><font color=#808080><br>///</font><font color=#008000> </font><font color=#808080>&lt;summary&gt;</font></code><br><code><font color=#808080>///</font><font color=#008000> ProxyApp test</font></code><br><code><font color=#808080>///</font><font color=#008000> </font><font color=#808080>&lt;/summary&gt;</font></code><br><code><font color=#0000ff>public</font> <font color=#0000ff>class</font> ProxyApp</code><br><code>{</code><br><code><font color=#0000ff>&nbsp; public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> Main( <font color=#0000ff>string</font>[] args )</code><br><code><font color=#0000ff>&nbsp; </font>{</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Create math proxy</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>MathProxy p = <font color=#0000ff>new</font> MathProxy();</code><br><code><font color=#0000ff><br>&nbsp;&nbsp;&nbsp; </font></code><font color=#008000><code>// Do the math</code></font><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "4 + 2 = {0}", p.Add( 4, 2 ) );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "4 - 2 = {0}", p.Sub( 4, 2 ) );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "4 * 2 = {0}", p.Mul( 4, 2 ) );</code><br><code><font color=#0000ff>&nbsp;&nbsp;&nbsp; </font>Console.WriteLine( "4 / 2 = {0}", p.Div( 4, 2 ) );</code><br><code><font color=#0000ff>&nbsp; </font>}</code><br><code>}</code> </td>
                </tr>
            </tbody>
        </table>
        </p>
<img src ="http://www.cnblogs.com/SoulEdge/aggbug/668560.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/43628/" target="_blank">[新闻]微软：不裁员也不削减研发开支</a><br/><a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻频道</a>&nbsp;<a href="http://space.cnblogs.com/group.htm" target="_blank">小组</a>&nbsp;<a href="http://space.cnblogs.com/q" target="_blank">博问</a>&nbsp;<a href="http://wz.cnblogs.com/" target="_blank">网摘</a>&nbsp;<a href="http://space.cnblogs.com/ing" target="_blank">闪存</a>]]></description></item><item><title>结构型模式 享元模式</title><link>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668551.html</link><dc:creator>灵魂边缘</dc:creator><author>灵魂边缘</author><pubDate>Thu, 08 Mar 2007 12:31:00 GMT</pubDate><guid>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668551.html</guid><wfw:comment>http://www.cnblogs.com/SoulEdge/comments/668551.html</wfw:comment><comments>http://www.cnblogs.com/SoulEdge/archive/2007/03/08/668551.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/SoulEdge/comments/commentRss/668551.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/SoulEdge/services/trackbacks/668551.html</trackback:ping><description><![CDATA[<strong><font face=Arial size=2>Definition</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image10 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>Use sharing to support large numbers of fine-grained objects efficiently. </font></td>
        </tr>
    </tbody>
</table>
<strong><font face=Arial size=2>UML class diagram</font></strong> <br><img height=289 alt=flyweight[1].gif src="http://www.cnblogs.com/images/cnblogs_com/lw/flyweight[1].gif" width=467 border=0><br>&nbsp;&nbsp;&nbsp;<strong><font face=Arial size=2>Participants</font></strong> <br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td width=1><img id=Image11 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; HEIGHT: 1px; BORDER-RIGHT-WIDTH: 0px" src="http://www.dofactory.com/Images/pixel.gif"> </td>
            <td><font face="arial, helvetica" size=2>&nbsp;&nbsp;&nbsp;The classes and/or objects participating in the Flyweight pattern are:<br><br>
            <ul>
                <li><font color=#aa0000><strong>Flyweight </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(Character)</strong></font>
                <ul>
                    <li>declares an interface through which flyweights can receive and act on extrinsic state. </li>
                </ul>
                </li>
                <li><font color=#aa0000><strong>ConcreteFlyweight </strong></font>&nbsp;&nbsp;<font color=#007733><strong>(CharacterA, CharacterB, ..., CharacterZ)</strong></font>
                <ul>
                    <li>implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object's context. </li>
                </ul>
                </li>
                <li><f