﻿<?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>C++博客-(define (cuigang) (coding))-随笔分类-设计模式</title><link>http://www.cppblog.com/cuigang/category/5812.html</link><description>(define (coding) (coding))
</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 22:49:59 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 22:49:59 GMT</pubDate><ttl>60</ttl><item><title>空对象模式</title><link>http://www.cppblog.com/cuigang/archive/2007/12/17/38804.html</link><dc:creator>cuigang</dc:creator><author>cuigang</author><pubDate>Mon, 17 Dec 2007 13:34:00 GMT</pubDate><guid>http://www.cppblog.com/cuigang/archive/2007/12/17/38804.html</guid><wfw:comment>http://www.cppblog.com/cuigang/comments/38804.html</wfw:comment><comments>http://www.cppblog.com/cuigang/archive/2007/12/17/38804.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/cuigang/comments/commentRss/38804.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cuigang/services/trackbacks/38804.html</trackback:ping><description><![CDATA[<span style="font-size: 8pt;"><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">Martin Flower在《重构》里提到空对象模式，对于从容器中查找失败需要返回引用时，可以使用，同时我觉的对于空指针调用也有一定作用，试举一例：</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;">
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%; font-family: courier new;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Mail{<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;print()<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{std::cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">"</span><span style="color: #000000;">Hello!</span><span style="color: #000000;">"</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">std::endl;};<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">operator</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">(size_t);<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">};<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;NullMail{<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NullMail(){};<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;NullMail&nbsp;inst;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;print(){<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">"</span><span style="color: #000000;">error!&nbsp;You&nbsp;can't&nbsp;access&nbsp;a&nbsp;NULL&nbsp;object!</span><span style="color: #000000;">"</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;NullMail</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;&nbsp;GetInst(){</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;inst;};<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">};<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">NullMail&nbsp;NullMail::inst;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;Mail::</span><span style="color: #0000ff;">operator</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">(size_t){<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">NullMail::GetInst();<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">Mail</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;GetMailPtr(){<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;NULL;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">Mail</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;GetMailRef(){<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(Mail</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">)NullMail::GetInst();<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">//</span><span style="color: #008000;"><img src="http://www.cppblog.com/Images/dot.gif"><img src="http://www.cppblog.com/Images/dot.gif"></span><span style="color: #008000;"><br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mail</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;ptr_Mail&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;GetMailPtr();<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mail</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;ref_Mail&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;GetMailRef();<br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">ptr_Mail-&gt;print();</span><span style="color: #008000;"><br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref_Mail.print();</span></div>
<br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">当我们返回一个空指针给调用者，如果他没有判断，那就会发生系统崩溃，而我们返回一个空对象的引用就可以避免这一点。</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">另外一个附加的有趣现象，你认为下面的调用会输出什么信息？</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;
</span>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%; font-family: courier new;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptr_Mail&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Mail;<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptr_Mail&nbsp;</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">print();<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref_Mail.print();&nbsp;&nbsp;&nbsp;</span></div>
<br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">正确的答案是：</span><br style="font-family: courier new;">
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%; font-family: courier new;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello!<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello!</span></div>
<br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">因为Mail的缺省构造函数将自己的vptr填给了NullMail单例。</span><br style="font-family: courier new;"><span style="font-family: courier new;">＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝</span><br style="font-family: courier new;"><span style="font-family: courier new;">一个问题，是不是Mail的每个公共接口，NullMail都要去实现一下？&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; －－th</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">//除非实在没有必要，的确应当实现一下所有的公有虚方法。cuigang，2007-11-08</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;"><span style="font-family: courier new;">＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝</span><br style="font-family: courier new;"><span style="font-family: courier new;">&#8220;对于从容器中查找失败需要返回引用时&#8221;，下午刚刚碰到一个这样的问题。&nbsp;&nbsp;&nbsp;&nbsp; jb</span><br style="font-family: courier new;"><span style="font-family: courier new;">&nbsp;</span><br style="font-family: courier new;">  </span><img src ="http://www.cppblog.com/cuigang/aggbug/38804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cuigang/" target="_blank">cuigang</a> 2007-12-17 21:34 <a href="http://www.cppblog.com/cuigang/archive/2007/12/17/38804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>20070331备忘——单件模式</title><link>http://www.cppblog.com/cuigang/archive/2007/12/17/38791.html</link><dc:creator>cuigang</dc:creator><author>cuigang</author><pubDate>Mon, 17 Dec 2007 13:23:00 GMT</pubDate><guid>http://www.cppblog.com/cuigang/archive/2007/12/17/38791.html</guid><wfw:comment>http://www.cppblog.com/cuigang/comments/38791.html</wfw:comment><comments>http://www.cppblog.com/cuigang/archive/2007/12/17/38791.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cuigang/comments/commentRss/38791.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cuigang/services/trackbacks/38791.html</trackback:ping><description><![CDATA[1、<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">pCh&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">(</span><span style="color: #000000;">5</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">构造一个char变量，并赋值为5</span><span style="color: #008000;"><br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">pCh_a&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">[</span><span style="color: #000000;">5</span><span style="color: #000000;">];&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">构造一个char数组，大小为5</span></div>
<br>
&nbsp;<br>
2、C++的异常处理是不可恢复的。<br>
&nbsp;<br>
3、用懒汉式的单件如下，可能产生线程不安全。<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;singleton{<br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;singleton</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;_inst;<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;singleton(){}<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;singleton</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;get_inst(){<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(_inst&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_inst&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;singleton;<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_inst;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">}</span></div>
<br>
首先，在判空时重入会被new两次，其次，返回实例的指针，可能会被会被delete掉。 <img src ="http://www.cppblog.com/cuigang/aggbug/38791.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cuigang/" target="_blank">cuigang</a> 2007-12-17 21:23 <a href="http://www.cppblog.com/cuigang/archive/2007/12/17/38791.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>