﻿<?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++博客-子弹的VISIONS-随笔分类-1.2 闲人杂语</title><link>http://www.cppblog.com/ztwaker/category/7242.html</link><description>&lt;font color=#FF0000&gt;/* 掌握未来最大的趋势：信息化、自动化、一体化、人性化 */  &lt;/font&gt;&lt;font color=#00FF00&gt;[终需有日龙穿凤 唔使日日裤穿窿]&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Fri, 08 Aug 2008 05:06:07 GMT</lastBuildDate><pubDate>Fri, 08 Aug 2008 05:06:07 GMT</pubDate><ttl>60</ttl><item><title>[设计模式] 上班路上突然想到的builder模式</title><link>http://www.cppblog.com/ztwaker/archive/2008/08/08/58306.html</link><dc:creator>子弹</dc:creator><author>子弹</author><pubDate>Fri, 08 Aug 2008 01:45:00 GMT</pubDate><guid>http://www.cppblog.com/ztwaker/archive/2008/08/08/58306.html</guid><wfw:comment>http://www.cppblog.com/ztwaker/comments/58306.html</wfw:comment><comments>http://www.cppblog.com/ztwaker/archive/2008/08/08/58306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ztwaker/comments/commentRss/58306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ztwaker/services/trackbacks/58306.html</trackback:ping><description><![CDATA[<span style="FONT-FAMILY: courier new"><br>Only some thinking to help to think about the Builder Pattern, but it's not so intuitive.<br>Suppose that we want to save data to files.<br><br>class File { <span style="COLOR: #808080">/*abstract file*/</span> };<br>class FileBuilder<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual bool buildFileHeader() =0;<br>&nbsp;&nbsp;&nbsp; virtual bool buildFileBody()&nbsp;&nbsp; =0;<br>&nbsp;&nbsp;&nbsp; virtual bool buildFileTail()&nbsp;&nbsp;&nbsp;=0;<br>&nbsp;&nbsp;&nbsp; virtual File* getFile()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =0;<br>};<br>&nbsp;<br>class BmpFile&nbsp; : public File { .... };<br>class GifFile&nbsp;&nbsp;: public File { .... };<br>class JpegFile : public File { .... };<br><span style="COLOR: #808080">//other kinds of files</span> <br>&nbsp;<br>class BmpFileBuilder : public FileBuilder<br>{<br>};<br>&nbsp;<br>class GifFileBuilder : public FileBuilder<br>{<br>};<br>&nbsp;<br>class JpegFileBuilder : public FileBuilder<br>{<br>};<br><span style="COLOR: #808080">//builders above implement those interfaces from FileBuilder<br>//other kinds of file builders</span> <br>&nbsp;<br><span style="COLOR: #808080">//usage</span><br>File* buildFile(FileBuilder* fb)<br>{<span style="COLOR: #808080"><br></span>&nbsp;&nbsp;&nbsp; fb.buildFileHeader();<br>&nbsp;&nbsp;&nbsp; fb.buildFileBody();<br>&nbsp;&nbsp;&nbsp; fb.buildFileTail();<br>&nbsp;&nbsp;&nbsp; return fb.GetFile();<br>}<br>void func()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;FileBuilder* fb = new SomeFileBuilder();<br>&nbsp;&nbsp;&nbsp; File* f = buildFile(fb);<br>&nbsp;&nbsp;&nbsp; <span style="COLOR: #808080">//use f</span><br>&nbsp;&nbsp;&nbsp; ....<br>}<br>&nbsp;<br><em><strong>Key Points:</strong><br>1. Each builder is responsible for its buildXXX()<br>2. Each builder builds one kind of File<br>3. the File would be created in builder's constructor</em></span> <br>
<img src ="http://www.cppblog.com/ztwaker/aggbug/58306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ztwaker/" target="_blank">子弹</a> 2008-08-08 09:45 <a href="http://www.cppblog.com/ztwaker/archive/2008/08/08/58306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[设计模式] Understand Design Patterns -- Factory Method</title><link>http://www.cppblog.com/ztwaker/archive/2008/07/17/56419.html</link><dc:creator>子弹</dc:creator><author>子弹</author><pubDate>Thu, 17 Jul 2008 04:43:00 GMT</pubDate><guid>http://www.cppblog.com/ztwaker/archive/2008/07/17/56419.html</guid><wfw:comment>http://www.cppblog.com/ztwaker/comments/56419.html</wfw:comment><comments>http://www.cppblog.com/ztwaker/archive/2008/07/17/56419.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ztwaker/comments/commentRss/56419.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ztwaker/services/trackbacks/56419.html</trackback:ping><description><![CDATA[<p><strong>Factory Method</strong></p>
<p><span style="COLOR: #808080">// interface</span><br>class People<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual void doWhat() =0;<br>&nbsp;&nbsp;&nbsp; virtual string type() const =0;<br>};</p>
<p>class Male : public People<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual void doWhat();<br>&nbsp;&nbsp;&nbsp; virtual string type() const;<br>&nbsp;&nbsp;&nbsp; static&nbsp; People* creator();<br>};</p>
<p>class Female : public People<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; virtual void doWhat();<br>&nbsp;&nbsp;&nbsp; virtual string type() const;<br>&nbsp;&nbsp;&nbsp; static&nbsp; People* creator();<br>};</p>
<p><br><span style="COLOR: #808080">// interface</span><br>typedef People* (*CreateFcn)();<br>class PeopleFactory<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; static People* producePeople(const string&amp; type);<br>&nbsp;&nbsp;&nbsp; static addCreateFcn(const string&amp; type, CreateFcn creator);</p>
<p>private:<br>&nbsp;&nbsp;&nbsp; static map&lt;string, CreateFcn&gt; createFcns;<br>};</p>
<p>People* PeopleFactory::producePeople(const string&amp; type)<br>{<br>&nbsp;&nbsp;&nbsp; //***1<br>&nbsp;&nbsp;&nbsp; CreateFcn creator= createFcns[type];<br>&nbsp;&nbsp;&nbsp; return creator();<br>}</p>
<p><span style="COLOR: #008000">---- test ----<br></span>void f()<br>{<br>&nbsp;&nbsp;&nbsp; //***2<br>&nbsp;&nbsp;&nbsp; const string t("Male"); //or "Female"<br>&nbsp;&nbsp;&nbsp; People* p = PeopleFactory::producePeople(t);<br>&nbsp;&nbsp;&nbsp; p-&gt;doWhat();&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; delete p;<br>}</p>
<p><span style="COLOR: #008000">---- extension ----</span><br>class Hemophrodite : public People<br>{<br>&nbsp;&nbsp;&nbsp; virtual void doWhat();<br>&nbsp;&nbsp;&nbsp; virtual string type() const;<br>&nbsp;&nbsp;&nbsp; static&nbsp; People* creator();<br>};</p>
<p>void g()<br>{<br>&nbsp;&nbsp;&nbsp; //***3<br>&nbsp;&nbsp;&nbsp; const string newType = "Hemophrodite";<br>&nbsp;&nbsp;&nbsp; PeopleFactory::addCreateFcn(newType, Hemophrodite::creator);</p>
<p>&nbsp;&nbsp;&nbsp; // usage<br>&nbsp;&nbsp;&nbsp; const string t("Hemophrodite");<br>&nbsp;&nbsp;&nbsp; People* p = PeopleFactory::producePeople(t);<br>&nbsp;&nbsp;&nbsp; p-&gt;doWhat();&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; delete p;<br>}</p>
<p>Cool!! OCP!!</p>
<p>---------<br><strong>Key point:</strong></p>
<p>1. How does the Factory Method create the instance<br>&nbsp;&nbsp;&nbsp; of classes through their type names?<br>&nbsp;&nbsp; How could this method follow the OCP?</p>
<p>2. Where does the type name come from?<br>&nbsp;-- config file<br>&nbsp;-- registry<br>&nbsp;-- other method<br><br></p>
<p>3. About extension<br>&nbsp;&nbsp; <br>----------<br><strong>Interface classes:</strong><br>&nbsp;&nbsp;&nbsp; People &amp; PeopleFactory</p>
<br>enjoy it!! 
<img src ="http://www.cppblog.com/ztwaker/aggbug/56419.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ztwaker/" target="_blank">子弹</a> 2008-07-17 12:43 <a href="http://www.cppblog.com/ztwaker/archive/2008/07/17/56419.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[经验教训] 设计要点</title><link>http://www.cppblog.com/ztwaker/archive/2008/07/04/55358.html</link><dc:creator>子弹</dc:creator><author>子弹</author><pubDate>Fri, 04 Jul 2008 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/ztwaker/archive/2008/07/04/55358.html</guid><wfw:comment>http://www.cppblog.com/ztwaker/comments/55358.html</wfw:comment><comments>http://www.cppblog.com/ztwaker/archive/2008/07/04/55358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/ztwaker/comments/commentRss/55358.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/ztwaker/services/trackbacks/55358.html</trackback:ping><description><![CDATA[&nbsp;软件设计要点<br><br><strong>Key point: 大局思想 分层思想</strong><br><br>设计，有很多方法。不管哪种方法，都需要对待设计的对象的深入的了解。<br><br>要有大局思想，不能管中窥豹，见木不见林。<br><br>设计时，不能过多考虑细节。如果陷在细节里，要及时中止和退出。<br><br>自上而下，层层细化是一种不错的方法。<br><br>可以借用一下图形学中的&#8220;分层次细节模型&#8221;。<br><br>不同的层次应该有不同的细节。<br><br>结构化有助于思路清晰化。树型结构是一种不错的选择。<br><br>每个阶段只做好那个阶段的事，其他的事一律不能做（尽管能做极好也不行）。<br><br>多和人讨论心中的想法。多注意别人的思路，与已比较，取长补短。<br><br>通常，对于问题，越讨论越清晰。 
<img src ="http://www.cppblog.com/ztwaker/aggbug/55358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/ztwaker/" target="_blank">子弹</a> 2008-07-04 17:36 <a href="http://www.cppblog.com/ztwaker/archive/2008/07/04/55358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>