<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Vietnamese Developers' Blog &#187; Design Pattern</title>
	<atom:link href="http://openandfree.wordpress.com/category/design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://openandfree.wordpress.com</link>
	<description>There are 10 types of people in the world: Those who know the binary system and those who don't</description>
	<lastBuildDate>Sat, 22 Nov 2008 00:47:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='openandfree.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/caa4770bbad32f97d95cfd98b414dfc1?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Vietnamese Developers' Blog &#187; Design Pattern</title>
		<link>http://openandfree.wordpress.com</link>
	</image>
			<item>
		<title>Prototype Pattern</title>
		<link>http://openandfree.wordpress.com/2007/11/05/prototype-pattern/</link>
		<comments>http://openandfree.wordpress.com/2007/11/05/prototype-pattern/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 03:08:32 +0000</pubDate>
		<dc:creator>Hoang Tran</dc:creator>
				<category><![CDATA[Design Pattern]]></category>

		<guid isPermaLink="false">http://openandfree.wordpress.com/2007/11/05/prototype-pattern/</guid>
		<description><![CDATA[Trong quyển GoF Design Pattern có 23 mẫu thiết kế và khi được phân chia theo mục đích sử dụng thì có 5 mẫu kiến tạo (creational pattern) bao gồm: Abstract factory, Builder, Factory method, Prototype và Singleton. Creational Pattern trừu tượng hóa quá trình khởi tạo object. Các mẫu creational giúp hệ thống không [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=openandfree.wordpress.com&blog=1590880&post=29&subd=openandfree&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Trong quyển <a href="http://www.informit.com/store/product.aspx?isbn=0201633612&amp;rl=1">GoF</a> <a href="http://en.wikipedia.org/wiki/Design_Patterns">Design Pattern</a> có 23 mẫu thiết kế và khi được phân chia theo mục đích sử dụng thì có 5 mẫu kiến tạo (creational pattern) bao gồm: Abstract factory, Builder, Factory method, Prototype và Singleton. Creational Pattern trừu tượng hóa quá trình khởi tạo object. Các mẫu creational giúp hệ thống không phải phụ thuộc vào cách một object được tạo ra, xây dựng và thể hiện.</span></p>
<p class="MsoNormal" style="text-align:justify;"><strong><span style="font-size:10pt;font-family:Arial;">1. Mục đích và ý nghĩa</span></strong></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;"><a href="http://en.wikipedia.org/wiki/Prototype_pattern">Prototype Pattern</a> giúp cho việc khởi tạo object bằng một object nguyên mẫu (prototype), là copy của object “mẫu” đó. Ý tưởng của mẫu là chúng ta được cung cấp một object và sẽ dùng chính object này để như là một hình mẫu (template) khi cần tạo lập một đối tượng mới. Việc tạo lập object mới sẽ dựa trên object mẫu mà không sử dụng toán tử new hoặc constructor … được cụng cấp bởi ngôn ngữ lập trình. Lý do là chúng ta không biết được thông tin nội tại chi tiết bên trong object và object có thể che dấu và chỉ cung cấp ra bên ngoài một lượng thông tin giới hạn. Do vậy ta không thể dùng toán tử new và sao chép những dữ liệu được object cung cấp (vốn không đầy đủ) cho một object mới. Cách tốt nhất là để cho chính object “mẫu” tự xác định thông tin và dữ liệu sao chép.</span></p>
<p class="MsoNormal" style="text-align:justify;"><strong><span style="font-size:10pt;font-family:Arial;">2. Cấu trúc</span></strong></p>
<p class="MsoNormal" style="text-align:justify;"><img src="http://img.photobucket.com/albums/v297/Zosojp/prototype.jpg" /></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">- Prototype: Cung cấp interface để copy chính bản thân nó (phương thức clone())</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">- ConcreatePrototype: Implement interface được cung cấp bởi Prototype để copy chính bản thân nó. (thể hiện cụ thể phương thức clone())</span></p>
<p><span style="font-size:10pt;font-family:Arial;">- Client: Tạo một object mới bằng cách yêu cầu prototype copy chính bản thân nó.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span id="more-29"></span><strong><span style="font-size:10pt;font-family:Arial;">3. Ưu và nhược điểm</span></strong></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Prototype ẩn đi những lớp con rời rạc từ phía client, do vậy làm giảm đi số lớp con mà client cần biết. Hơn thế nữa pattern này làm cho client hoạt động với những lớp con cụ thể mà không phải thay đổi gì. </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Sau đây là một vài lợi ích khác của prototype pattern:</span></p>
<p class="MsoNormal" style="text-align:justify;"><em><span style="font-size:10pt;font-family:Arial;">- Thêm và loại bỏ lớp concreate lúc run-time:</span></em><span style="font-size:10pt;font-family:Arial;"> Prototype hợp nhất quá trình khởi tạo một object mới vào trong hệ thống đơn giản chỉ bằng cách đăng ký một thực thể nguyên mẫu với client. Điều đó linh động hơn một chút so với các mẫu kiến tạo khác bởi vì client có thể cài đặt và loại bỏ các nguyên mẫu tại thời điểm run-time.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">- <em>Khởi tạo object mới bằng cách thay đổi một vài attribute của object (các object có ít điểm khác biệt nhau):</em> Một hệ thống linh động sẽ để cho chúng ta tự define một hành động nào đó thông qua sự kết hợp với một object (nghĩa là một phương thức của một class) hơn là define một class mới. Client có thể thể hiện một tác động khác bằng cách ủy quyền cho lớp prototype. Đồng thời cách thức khởi tạo này cũng giúp cho chúng ta khai báo một “lớp mới” mà không phải lập trình gì cả. Thực tế thì việc copy một nguyên mẫu giống như việc khởi tạo một object từ một class mới. Prototype pattern giúp giảm số lớp mà hệ thống cần dùng. </span></p>
<p class="MsoNormal" style="text-align:justify;"><em><span style="font-size:10pt;font-family:Arial;">- Khởi tạo object mới bằng cách thay đổi cấu trúc:</span></em><span style="font-size:10pt;font-family:Arial;"> Rất nhiều ứng dụng xây dựng hệ thống từ nhiều phần và các phần con. Các phần con lại khởi tạo từ nhiều phần con khác (chia nhỏ bài toán). Prototype pattern cũng hỗ trợ điều này. Nghĩa là các phần có đó có thể được khởi tạo từ việc copy một nguyên mẫu từ một “cấu trúc” khác. Miễn là các phần kết hợp đều thể hiện Clone() và được sử dụng với cấu trúc khác nhau làm nguyên mẫu. </span></p>
<p class="MsoNormal" style="text-align:justify;"><em><span style="font-size:10pt;font-family:Arial;">- Giảm việc phân lớp</span></em><span style="font-size:10pt;font-family:Arial;">: Đôi khi hệ thống quá phức tạp vì có quá nhiều class, và cây thừa kế của lớp khởi tạo có quá nhiều lớp song song cùng mức. Prototype pattern rõ ràng làm giảm số lớp và sự phức tạp của cây thừa kế (class hierarchy) </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;"> </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Một điểm hạn chế của prototype pattern là đôi khi việc phải implement mỗi phương thức clone() của mỗi lớp con của Prototype sẽ gặp khó khăn. Ví dụ như việc implement clone() sẽ khó khăn khi mà những object nội hàm của chúng không hỗ trợ việc copy hoặc có một reference (không copy được reference. Xem bài phân biệt pointer và reference).</span></p>
<p class="MsoNormal" style="text-align:justify;"><strong><span style="font-size:10pt;font-family:Arial;">4. Implementation</span></strong></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Trong C++ cài đặt rất đơn giản sử dụng copy constructor hoặc assignment operator. </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Ví dụ mẫu:</span></p>
<pre><font size="2"><tt><strong><font color="#000080">#include</font></strong> <font color="#ff0000">&lt;iostream&gt;</font>
<strong><font color="#000080">#include</font></strong> <font color="#ff0000">&lt;map&gt;</font>

<strong><font color="#0000ff">using</font></strong> <strong><font color="#0000ff">namespace</font></strong> std<font color="#990000">;</font>

<strong><font color="#0000ff">enum</font></strong> RECORD_TYPE_en
<font color="#ff0000">{</font>
    CAR<font color="#990000">,</font>
    BIKE<font color="#990000">,</font>
    PERSON
<font color="#ff0000">}</font><font color="#990000">;</font>

<em><font color="#9a1900">/**</font></em>
<em><font color="#9a1900"> * Record is the Prototype</font></em>
<em><font color="#9a1900"> */</font></em>

<strong><font color="#0000ff">class</font></strong> Record
<font color="#ff0000">{</font>
  <strong><font color="#0000ff">public</font></strong> <font color="#990000">:</font>
    <strong><font color="#000000">Record</font></strong><font color="#990000">()</font> <font color="#ff0000">{}</font>
    <font color="#990000">~</font><strong><font color="#000000">Record</font></strong><font color="#990000">()</font> <font color="#ff0000">{}</font>

    Record<font color="#990000">*</font> <strong><font color="#000000">Clone</font></strong><font color="#990000">()=</font><font color="#993399">0</font><font color="#990000">;</font>
    <strong><font color="#0000ff">virtual</font></strong> <font color="#009900">void</font> <strong><font color="#000000">Print</font></strong><font color="#990000">()=</font><font color="#993399">0</font><font color="#990000">;</font>
<font color="#ff0000">}</font><font color="#990000">;</font>

<em><font color="#9a1900">/**</font></em>
<em><font color="#9a1900"> * CarRecord is Concrete Prototype</font></em>
<em><font color="#9a1900"> */</font></em>

<strong><font color="#0000ff">class</font></strong> CarRecord <font color="#990000">:</font> <strong><font color="#0000ff">public</font></strong> Record
<font color="#ff0000">{</font>
  <strong><font color="#0000ff">private</font></strong> <font color="#990000">:</font>
    string m_oStrCarName<font color="#990000">;</font>
    u_int32_t m_ui32ID<font color="#990000">;</font>

  <strong><font color="#0000ff">public</font></strong> <font color="#990000">:</font>
    <strong><font color="#000000">CarRecord</font></strong><font color="#990000">(</font>string _oStrCarName<font color="#990000">,</font>u_int32_t _ui32ID<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">(),</font> <strong><font color="#000000">m_oStrCarName</font></strong><font color="#990000">(</font>_oStrCarName<font color="#990000">),</font>
      <strong><font color="#000000">m_ui32ID</font></strong><font color="#990000">(</font>_ui32ID<font color="#990000">)</font>
    <font color="#ff0000">{</font>
    <font color="#ff0000">}</font>

    <strong><font color="#000000">CarRecord</font></strong><font color="#990000">(</font>CarRecord<font color="#990000">&amp;</font> _oCarRecord<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        m_oStrCarName <font color="#990000">=</font> _oCarRecord<font color="#990000">.</font>m_oStrCarName<font color="#990000">;</font>
        m_ui32ID <font color="#990000">=</font> _oCarRecord<font color="#990000">.</font>m_ui32ID<font color="#990000">;</font>
    <font color="#ff0000">}</font>

    <font color="#990000">~</font><strong><font color="#000000">CarRecord</font></strong><font color="#990000">()</font> <font color="#ff0000">{}</font>

    Record<font color="#990000">*</font> <strong><font color="#000000">Clone</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        <strong><font color="#0000ff">return</font></strong> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">CarRecord</font></strong><font color="#990000">(*</font><strong><font color="#0000ff">this</font></strong><font color="#990000">);</font>
    <font color="#ff0000">}</font>

    <font color="#009900">void</font> <strong><font color="#000000">Print</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        cout <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Car Record"</font> <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Name  : "</font> <font color="#990000">&lt;&lt;</font> m_oStrCarName <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Number: "</font> <font color="#990000">&lt;&lt;</font> m_ui32ID <font color="#990000">&lt;&lt;</font> endl <font color="#990000">&lt;&lt;</font> endl<font color="#990000">;</font>
    <font color="#ff0000">}</font>
<font color="#ff0000">}</font><font color="#990000">;</font>

<em><font color="#9a1900">/**</font></em>
<em><font color="#9a1900"> * BikeRecord is the Concrete Prototype</font></em>
<em><font color="#9a1900"> */</font></em>

<strong><font color="#0000ff">class</font></strong> BikeRecord <font color="#990000">:</font> <strong><font color="#0000ff">public</font></strong> Record
<font color="#ff0000">{</font>
  <strong><font color="#0000ff">private</font></strong> <font color="#990000">:</font>
    string m_oStrBikeName<font color="#990000">;</font>
    u_int32_t m_ui32ID<font color="#990000">;</font>

  <strong><font color="#0000ff">public</font></strong> <font color="#990000">:</font>
    <strong><font color="#000000">BikeRecord</font></strong><font color="#990000">(</font>string _oStrBikeName<font color="#990000">,</font>u_int32_t _ui32ID<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">(),</font> <strong><font color="#000000">m_oStrBikeName</font></strong><font color="#990000">(</font>_oStrBikeName<font color="#990000">),</font>
      <strong><font color="#000000">m_ui32ID</font></strong><font color="#990000">(</font>_ui32ID<font color="#990000">)</font>
    <font color="#ff0000">{</font>
    <font color="#ff0000">}</font>

    <strong><font color="#000000">BikeRecord</font></strong><font color="#990000">(</font>BikeRecord<font color="#990000">&amp;</font> _oBikeRecord<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        m_oStrBikeName <font color="#990000">=</font> _oBikeRecord<font color="#990000">.</font>m_oStrBikeName<font color="#990000">;</font>
        m_ui32ID <font color="#990000">=</font> _oBikeRecord<font color="#990000">.</font>m_ui32ID<font color="#990000">;</font>
    <font color="#ff0000">}</font>

    <font color="#990000">~</font><strong><font color="#000000">BikeRecord</font></strong><font color="#990000">()</font> <font color="#ff0000">{}</font>

    Record<font color="#990000">*</font> <strong><font color="#000000">Clone</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        <strong><font color="#0000ff">return</font></strong> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">BikeRecord</font></strong><font color="#990000">(*</font><strong><font color="#0000ff">this</font></strong><font color="#990000">);</font>
    <font color="#ff0000">}</font>

    <font color="#009900">void</font> <strong><font color="#000000">Print</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        cout <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Bike Record"</font> <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Name  : "</font> <font color="#990000">&lt;&lt;</font> m_oStrBikeName <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Number: "</font> <font color="#990000">&lt;&lt;</font> m_ui32ID <font color="#990000">&lt;&lt;</font> endl <font color="#990000">&lt;&lt;</font> endl<font color="#990000">;</font>
    <font color="#ff0000">}</font>
<font color="#ff0000">}</font><font color="#990000">;</font>

<em><font color="#9a1900">/**</font></em>
<em><font color="#9a1900"> * PersonRecord is the Concrete Prototype</font></em>
<em><font color="#9a1900"> */</font></em>

<strong><font color="#0000ff">class</font></strong> PersonRecord <font color="#990000">:</font> <strong><font color="#0000ff">public</font></strong> Record
<font color="#ff0000">{</font>
  <strong><font color="#0000ff">private</font></strong> <font color="#990000">:</font>
    string m_oStrPersonName<font color="#990000">;</font>

    u_int32_t m_ui32Age<font color="#990000">;</font>

  <strong><font color="#0000ff">public</font></strong> <font color="#990000">:</font>
    <strong><font color="#000000">PersonRecord</font></strong><font color="#990000">(</font>string _oStrPersonName<font color="#990000">,</font> u_int32_t _ui32Age<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">(),</font> <strong><font color="#000000">m_oStrPersonName</font></strong><font color="#990000">(</font>_oStrPersonName<font color="#990000">),</font>
      <strong><font color="#000000">m_ui32Age</font></strong><font color="#990000">(</font>_ui32Age<font color="#990000">)</font>
    <font color="#ff0000">{</font>
    <font color="#ff0000">}</font>

    <strong><font color="#000000">PersonRecord</font></strong><font color="#990000">(</font>PersonRecord<font color="#990000">&amp;</font> _oPersonRecord<font color="#990000">)</font>
    <font color="#990000">:</font> <strong><font color="#000000">Record</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        m_oStrPersonName <font color="#990000">=</font> _oPersonRecord<font color="#990000">.</font>m_oStrPersonName<font color="#990000">;</font>
        m_ui32Age <font color="#990000">=</font> _oPersonRecord<font color="#990000">.</font>m_ui32Age<font color="#990000">;</font>
    <font color="#ff0000">}</font>

    <font color="#990000">~</font><strong><font color="#000000">PersonRecord</font></strong><font color="#990000">()</font> <font color="#ff0000">{}</font>

    Record<font color="#990000">*</font> <strong><font color="#000000">Clone</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        <strong><font color="#0000ff">return</font></strong> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">PersonRecord</font></strong><font color="#990000">(*</font><strong><font color="#0000ff">this</font></strong><font color="#990000">);</font>
    <font color="#ff0000">}</font>

    <font color="#009900">void</font> <strong><font color="#000000">Print</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        cout <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Person Record"</font> <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Name : "</font> <font color="#990000">&lt;&lt;</font> m_oStrPersonName <font color="#990000">&lt;&lt;</font> endl
             <font color="#990000">&lt;&lt;</font> <font color="#ff0000">"Age  : "</font> <font color="#990000">&lt;&lt;</font> m_ui32Age <font color="#990000">&lt;&lt;</font> endl <font color="#990000">&lt;&lt;</font> endl <font color="#990000">;</font>
    <font color="#ff0000">}</font>
<font color="#ff0000">}</font><font color="#990000">;</font>

<em><font color="#9a1900">/**</font></em>
<em><font color="#9a1900"> * RecordFactory is the client</font></em>
<em><font color="#9a1900"> */</font></em>

<strong><font color="#0000ff">class</font></strong> RecordFactory
<font color="#ff0000">{</font>
  <strong><font color="#0000ff">private</font></strong> <font color="#990000">:</font>
    map<font color="#990000">&lt;</font>RECORD_TYPE_en<font color="#990000">,</font> Record<font color="#990000">*</font> <font color="#990000">&gt;</font> m_oMapRecordReference<font color="#990000">;</font>

  <strong><font color="#0000ff">public</font></strong> <font color="#990000">:</font>
    <strong><font color="#000000">RecordFactory</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        m_oMapRecordReference<font color="#990000">[</font>CAR<font color="#990000">]</font>  <font color="#990000">=</font> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">CarRecord</font></strong><font color="#990000">(</font><font color="#ff0000">"Ferrari"</font><font color="#990000">,</font> <font color="#993399">5050</font><font color="#990000">);</font>
        m_oMapRecordReference<font color="#990000">[</font>BIKE<font color="#990000">]</font> <font color="#990000">=</font> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">BikeRecord</font></strong><font color="#990000">(</font><font color="#ff0000">"Yamaha"</font><font color="#990000">,</font> <font color="#993399">2525</font><font color="#990000">);</font>
        m_oMapRecordReference<font color="#990000">[</font>PERSON<font color="#990000">]</font> <font color="#990000">=</font> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">PersonRecord</font></strong><font color="#990000">(</font><font color="#ff0000">"Tom"</font><font color="#990000">,</font> <font color="#993399">25</font><font color="#990000">);</font>
    <font color="#ff0000">}</font>

    <font color="#990000">~</font><strong><font color="#000000">RecordFactory</font></strong><font color="#990000">()</font>
    <font color="#ff0000">{</font>
        <strong><font color="#0000ff">delete</font></strong> m_oMapRecordReference<font color="#990000">[</font>CAR<font color="#990000">];</font>
        <strong><font color="#0000ff">delete</font></strong> m_oMapRecordReference<font color="#990000">[</font>BIKE<font color="#990000">];</font>
        <strong><font color="#0000ff">delete</font></strong> m_oMapRecordReference<font color="#990000">[</font>PERSON<font color="#990000">];</font>
    <font color="#ff0000">}</font>

    Record<font color="#990000">*</font> <strong><font color="#000000">CreateRecord</font></strong><font color="#990000">(</font>RECORD_TYPE_en enType<font color="#990000">)</font>
    <font color="#ff0000">{</font>
        <strong><font color="#0000ff">return</font></strong> m_oMapRecordReference<font color="#990000">[</font>enType<font color="#990000">]-&gt;</font><strong><font color="#000000">Clone</font></strong><font color="#990000">();</font>
    <font color="#ff0000">}</font>
<font color="#ff0000">}</font><font color="#990000">;</font>

<font color="#009900">int</font> <strong><font color="#000000">main</font></strong><font color="#990000">()</font>
<font color="#ff0000">{</font>
    RecordFactory<font color="#990000">*</font> poRecordFactory <font color="#990000">=</font> <strong><font color="#0000ff">new</font></strong> <strong><font color="#000000">RecordFactory</font></strong><font color="#990000">();</font>

    Record<font color="#990000">*</font> poRecord<font color="#990000">;</font>
    poRecord <font color="#990000">=</font> poRecordFactory<font color="#990000">-&gt;</font><strong><font color="#000000">CreateRecord</font></strong><font color="#990000">(</font>CAR<font color="#990000">);</font>
    poRecord<font color="#990000">-&gt;</font><strong><font color="#000000">Print</font></strong><font color="#990000">();</font>
    <strong><font color="#0000ff">delete</font></strong> poRecord<font color="#990000">;</font>

    poRecord <font color="#990000">=</font> poRecordFactory<font color="#990000">-&gt;</font><strong><font color="#000000">CreateRecord</font></strong><font color="#990000">(</font>BIKE<font color="#990000">);</font>
    poRecord<font color="#990000">-&gt;</font><strong><font color="#000000">Print</font></strong><font color="#990000">();</font>
    <strong><font color="#0000ff">delete</font></strong> poRecord<font color="#990000">;</font>

    poRecord <font color="#990000">=</font> poRecordFactory<font color="#990000">-&gt;</font><strong><font color="#000000">CreateRecord</font></strong><font color="#990000">(</font>PERSON<font color="#990000">);</font>
    poRecord<font color="#990000">-&gt;</font><strong><font color="#000000">Print</font></strong><font color="#990000">();</font>
    <strong><font color="#0000ff">delete</font></strong> poRecord<font color="#990000">;</font>

    <strong><font color="#0000ff">delete</font></strong> poRecordFactory<font color="#990000">;</font>
    <strong><font color="#0000ff">return</font></strong> <font color="#993399">0</font><font color="#990000">;</font>
<font color="#ff0000">}</font></tt></pre>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Arial;">Tham khảo thêm: http://www.vincehuston.org/dp/prototype.html</span></font></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/openandfree.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/openandfree.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/openandfree.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/openandfree.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/openandfree.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/openandfree.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/openandfree.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/openandfree.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/openandfree.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/openandfree.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/openandfree.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/openandfree.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=openandfree.wordpress.com&blog=1590880&post=29&subd=openandfree&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://openandfree.wordpress.com/2007/11/05/prototype-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f6f88159a87ae5eb23d369e775bf0e4a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hoangtran</media:title>
		</media:content>

		<media:content url="http://img.photobucket.com/albums/v297/Zosojp/prototype.jpg" medium="image" />
	</item>
		<item>
		<title>Singleton Pattern &#8211; Fundamental</title>
		<link>http://openandfree.wordpress.com/2007/10/08/singleton-pattern-fundamental/</link>
		<comments>http://openandfree.wordpress.com/2007/10/08/singleton-pattern-fundamental/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 08:54:01 +0000</pubDate>
		<dc:creator>Hoang Tran</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://openandfree.wordpress.com/2007/10/08/singleton-pattern-fundamental/</guid>
		<description><![CDATA[Singleton có lẽ là một trong những pattern có tính trừu tượng dễ hiểu nhất mà người học design pattern nào cũng biết. Trong dự án vừa rồi tôi có phải implement một cái database connection pool và thread pool mà nó chính xác là một singleton object. Tôi đã chọn Singleton để implement những [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=openandfree.wordpress.com&blog=1590880&post=16&subd=openandfree&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;">Singleton có lẽ là một trong những pattern có tính trừu tượng dễ hiểu nhất mà người học design pattern nào cũng biết. Trong dự án vừa rồi tôi có phải implement một cái database connection pool và thread pool mà nó chính xác là một singleton object. Tôi đã chọn Singleton để implement những class đó. Tất nhiên singleton chỉ là một phần nhỏ, không phải phần cốt lõi trong những cái pool đó, nhưng trong bài viết này tôi sẽ viết về singleton pattern như là bài viết đầu tiên về loạt bài design pattern tiếp theo. Những bài viết của tôi thường lấy ý tưởng từ những quyển sách kinh điển (nhiều khi là dịch lại &#8211; vì họ viết quá xuất sắc và không có gì cần thêm hay bớt). Tôi chỉ cố gắng hiểu một cách sâu sắc nhất và ghi lại theo cách hiểu. Trong ví dụ về design pattern, tôi chọn quyển Gang Of Four &#8211; Design Patterns &#8211; Elements of Reusable Object-Oriented Sofware, một quyển sách kinh điển nhất về design pattern. Nếu bạn có thời gian thì hãy dành thời gian đọc nó (bằng tiếng Anh để hiểu sâu sắc hơn).</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;"><strong>1. Mục đính và ý nghĩa.</strong></span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;">- Đảm bảo rằng một class có duy nhất một thực thể (instance) hay đối tượng (object) và cung cấp một cách thức truy nhập toàn cục để có thể từ bất kỳ đâu cũng lấy được instance duy nhất đó. Chúng ta cũng gọi thực thể duy nhất đó là thực thể chính (solely) của class đó. </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;">Đối với một vài class thì việc chỉ có duy nhất một instance (object) là rất quan trọng. Mặc dù có rất nhiều máy in trong hệ thống nhưng chỉ có một printer spooler. Cũng chỉ có một file system và một windows manager. Cũng như trong thiết kế connection pool thì chỉ có một connection pool quản lý nhiều connection. </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;">Làm thế nào để chúng ta đảm bảo một class chỉ có một thể hiện duy nhất và dễ dàng truy nhập được? Một object toàn cục có thể làm cho nó dễ dàng truy nhập được nhưng không ngăn cấm bạn tạo thêm nhiều object khác.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;">Một giải pháp tốt hơn là làm cho class đó tự bản thân nó có thể điều khiển được thực thể chính của nó. Class đó có thể đảm bảo rằng không có thực thể khác được tạo ra (bằng cách chặn đứng các yêu cầu tạo object mới) và cung cấp cách thức để truy nhập được thực thể chính của nó. Đó là singleton pattern. </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;"><strong>2. Cấu trúc</strong></span></p>
<p><img src="http://docs.google.com/File?id=ddnfqkcn_194dsgbjrs7" /><span id="more-16"></span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:10pt;font-family:Verdana;"><strong>3. Implementation</strong></span></p>
<p align="justify"><span style="font-size:10pt;font-family:Verdana;"> Phương pháp chung nhất để </span><span style="font-size:10pt;font-family:Verdana;">đảm bảo có một thực thể duy nhất</span><span style="font-size:10pt;font-family:Verdana;"> là giấu đi cách thức tạo ra thực thể ở bên trong lớp đó (hoặc là static member function hoặc một member function của class đó) và trong những hàm này thì đã đảm bảo rằng chỉ một thực thể được tạo ra. Có nghĩa là người sử dụng sẽ không thể gọi hàm constructor và destructor mà phải gọi thông qua một member function của lớp. Member function sẽ truy nhập vào biến mà lưu giữ thực thể duy nhất và đảm bảo rằng biến này được khởi tạo với thực thể duy nhất trước khi trả về giá trị. Cách tiếp cận này đảm bảo rằng singleton được tạo ra và khởi tạo trước khi nó được sử dụng lần đầu tiên. Bạn có thể định nghĩa hàm này trong C++ như một static member function Instance() của lớp Singleton. Singleton cũng định nghĩa biến static member variable _instance là con trỏ đến thực thể duy nhất.</span></p>
<pre><font size="2"><tt><strong><font color="#0000ff">class</font></strong> <font color="#009900">Singleton</font>
<font color="#990000">{</font>
public<font color="#990000">:</font>
    static <font color="#009900">Singleton</font><font color="#990000">*</font> <strong><font color="#000000">Instance</font></strong><font color="#990000">();</font>
protected<font color="#990000">:</font>
    <strong><font color="#000000">Singleton</font></strong><font color="#990000">();</font>
private<font color="#990000">:</font>
    static <font color="#009900">Singleton</font><font color="#990000">*</font> <font color="#009900">_</font>instance<font color="#990000">;</font>
<font color="#990000">};</font>

<font color="#009900">Singleton</font><font color="#990000">*</font> <font color="#009900">Singleton</font><font color="#990000">::</font><font color="#009900">_</font>instance <font color="#990000">=</font> <font color="#993399">0</font><font color="#990000">;</font>

<font color="#009900">Singleton</font><font color="#990000">*</font> <font color="#009900">Singleton</font><font color="#990000">::</font><strong><font color="#000000">Instance </font></strong><font color="#990000">()</font>
<font color="#990000">{</font>
    <strong><font color="#0000ff">if</font></strong> <font color="#990000">(</font><font color="#009900">_</font>instance <font color="#990000">==</font> <font color="#993399">0</font><font color="#990000">)</font> <font color="#990000">{</font>
        <font color="#009900">_</font>instance <font color="#990000">=</font> new <font color="#009900">Singleton</font><font color="#990000">;</font>
    <font color="#990000">}</font>
    <strong><font color="#0000ff">return</font></strong> <font color="#009900">_</font>instance<font color="#990000">;</font>
<font color="#990000">}</font>

 </tt></font></pre>
<p style="margin-bottom:0;" align="justify"><font face="Courier New, monospace"><font size="2"><font face="Verdana, sans-serif">Người sử dụng hoàn toàn truy nhập vào singleton thông qua member function Instance(). Biến _instance được khởi tạo là 0 và Instance() sẽ trả về giả trị của biến đó, khởi tạo nó bởi một thực thể duy nhất nếu nó là 0. Hàm này sử dụng cách thức khởi tạo lười biếng, nghĩa là thực thể không được tạo ra và lưu trữ cho đến khi nó lần đầu được truy nhập. </font></font></font></p>
<p style="margin-bottom:0;" align="justify"><font face="Verdana, sans-serif"><font size="2">Chú ý rằng hàm constructor là protected. Người sử dụng cố gắng khởi tạo một thực thể mới Singleton trực tiếp (bằng constructor) sẽ gặp lỗi lúc biên dịch. Điều này đảm bảo rằng chỉ có một thực thể có thể được tạo ra. </font></font></p>
<p style="margin-bottom:0;" align="justify"><font face="Verdana, sans-serif"><font size="2">Hơn thế nữa, vì _instance là pointer đến đối tượng Singleton, nên member function Instance() có thể gán con trỏ tới một subclass của Singleton vào biến _instance. </font></font></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/openandfree.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/openandfree.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/openandfree.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/openandfree.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/openandfree.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/openandfree.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/openandfree.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/openandfree.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/openandfree.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/openandfree.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/openandfree.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/openandfree.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=openandfree.wordpress.com&blog=1590880&post=16&subd=openandfree&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://openandfree.wordpress.com/2007/10/08/singleton-pattern-fundamental/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f6f88159a87ae5eb23d369e775bf0e4a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hoangtran</media:title>
		</media:content>

		<media:content url="http://docs.google.com/File?id=ddnfqkcn_194dsgbjrs7" medium="image" />
	</item>
	</channel>
</rss>