<?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/"
	>

<channel>
	<title>Coder-Studio &#187; design pattern</title>
	<atom:link href="http://www.coder-studio.com/blog/tag/design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coder-studio.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 02 Mar 2011 22:17:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Complément du tuto sur la classe Factory</title>
		<link>http://www.coder-studio.com/blog/complement-du-tuto-sur-la-classe-factory/</link>
		<comments>http://www.coder-studio.com/blog/complement-du-tuto-sur-la-classe-factory/#comments</comments>
		<pubDate>Tue, 08 Feb 2005 17:58:05 +0000</pubDate>
		<dc:creator>twxs</dc:creator>
				<category><![CDATA[Non classé]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[factory]]></category>

		<guid isPermaLink="false">http://r19649.ovh.net/cs/blog/?p=181</guid>
		<description><![CDATA[J'avais laissé en exercice à la fin du dernier tutorial sur les factories un petit exercice... face au nombreux mail de votre par je publie la réponse. Donc pour rappel, nous avons a notre disposition une classe Factory qui nous permet de créer des instances d'objets héritant tous d'une interface de base. i.e. Shape était [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">J'avais laissé en exercice à la fin du dernier tutorial sur les factories un petit exercice... face au nombreux mail de votre par je publie la réponse.</p>
<p><span id="more-181"></span></p>
<p>Donc pour rappel, nous avons a notre disposition une classe Factory qui nous permet de créer des instances d'objets héritant tous d'une interface de base.<br />
i.e. Shape était notre interface Cylinder, Sphere nos instance a créer.</p>
<p>Le désavantage principal de l'implémentation était l'obligation d'écrire dans chaque classe une méthode statique créant l'instance :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code7'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1817"><td class="code" id="p181code7"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> Shape <span style="color: #000040;">*</span>Sphere<span style="color: #008080;">::</span><span style="color: #007788;">create</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Heureusement, les template vont encore une fois nous aider a nous abstraire de cette contrainte.</p>
<p style="text-align: justify;">La factory retourne des objets de type Object (paramètre de la classe templatée) elle stocke pour chaque clef une fonction dont le prototype est :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code8'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1818"><td class="code" id="p181code8"><pre class="cpp" style="font-family:monospace;">Object <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">L'idée va être de ne plus stocker une fonction mais un objet au sens C++ jouant le rôle de cette fonction.</p>
<p style="text-align: justify;">Dans notre cas, le foncteur a donc une méthode CreateInstance(void) retournant un Object</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code9'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1819"><td class="code" id="p181code9"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">struct</span> InterfaceCreator<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">virtual</span> Object <span style="color: #000040;">*</span>CreateInstance<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>on modifie en conséquence la classe Factory :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code10'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18110"><td class="code" id="p181code10"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">class</span> Factory<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
&nbsp;
<span style="color: #666666;">// on redéfinit notre nouveau type comme</span>
<span style="color: #666666;">// etant un InterfaceCreator&lt;object width=&quot;100&quot; height=&quot;100&quot; type=&quot;application/x-shockwave-flash&quot;&gt;&lt;/object&gt;</span></pre></td></tr></table></div>

<p>Maintenant, il nous faut specialiser des Creator pour notre Sphere, Shape, etc...</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code11'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18111"><td class="code" id="p181code11"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>
<span style="color: #0000ff;">class</span> Creator <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> InterfaceCreator
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  BaseClass <span style="color: #000040;">*</span>CreateInstance<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
     RealClass instance <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> RealClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">static_cast</span><span style="color: #008000;">&#40;</span>instance<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Petite explication de cette double paramétrisation. Nous avons besoin du type réel de l'objet (Sphere) pour le créer et le type abstrait(Shape) pour pouvoir heriter de InterfaceCreator</p>
<p>Passons a l'utilisation :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p181code12'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18112"><td class="code" id="p181code12"><pre class="cpp" style="font-family:monospace;">Factory shapeFactory<span style="color: #008080;">;</span>
...
&nbsp;
<span style="color: #0000ff;">class</span> Sphere <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape<span style="color: #008000;">&#123;</span>
  ....
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> registered<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> Sphere<span style="color: #008080;">::</span><span style="color: #007788;">registered</span> <span style="color: #000080;">=</span> shapeFactory.<span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Sphere&quot;</span>, <span style="color: #0000dd;">new</span> Creator <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> Cylinder<span style="color: #008080;">::</span><span style="color: #007788;">registered</span> <span style="color: #000080;">=</span> shapeFactory.<span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Cylinder&quot;</span>, <span style="color: #0000dd;">new</span> Creator <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>j'espère que ca vous aura plu un minimum <img src='http://www.coder-studio.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
a+<br />
Twxs</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coder-studio.com/blog/complement-du-tuto-sur-la-classe-factory/feed/</wfw:commentRss>
		<slash:comments>1486</slash:comments>
		</item>
		<item>
		<title>La factory</title>
		<link>http://www.coder-studio.com/blog/la-factory/</link>
		<comments>http://www.coder-studio.com/blog/la-factory/#comments</comments>
		<pubDate>Tue, 21 Dec 2004 17:41:22 +0000</pubDate>
		<dc:creator>twxs</dc:creator>
				<category><![CDATA[C & C++]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[factory]]></category>

		<guid isPermaLink="false">http://r19649.ovh.net/cs/blog/?p=164</guid>
		<description><![CDATA[La Factory, brrr Dans ce tutorial, je vais tenter d'aborder le design pattern factory (fabrique pour les anglophobes). A mon habitude, nous allons retrouver la même ligne directrice que pour les précédents tutoriaux. Je vais commencer par expliquer brièvement le principe de la factory, ensuite en découlera une première implémentation en C++ et enfin on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>La Factory, brrr</strong></p>
<p style="text-align: justify;">Dans ce tutorial, je vais tenter d'aborder le design pattern factory (fabrique pour les anglophobes).<br />
A mon habitude, nous allons retrouver la même ligne directrice que pour les précédents tutoriaux.<br />
Je vais commencer par expliquer brièvement le principe de la factory, ensuite en découlera une première implémentation en C++ et enfin on essayera de rendre ça le plus générique possible.</p>
<p style="text-align: justify;"><span id="more-164"></span></p>
<h2>Definition</h2>
<p style="text-align: justify;">La factory est en fait un objet dont le rôle est de créer des instances d'autres objets.<br />
Dans un modeleur par exemple, on pourrait avoir une factory d'objets géométriques qui servirait à créer des sphères, cônes, etc...</p>
<h2>Première Implémentation :</h2>
<p style="text-align: justify;">Nous allons prendre le cas d'une factory d'objets géométriques dont le but est de fournir des objets héritant de l'interface Shape.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code26'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16426"><td class="code" id="p164code26"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Shape<span style="color: #008000;">&#123;</span>...<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">De plus, nous avons dans l'application des objets implémentant l'interface shape comme Sphere, Cone, ....</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code27'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16427"><td class="code" id="p164code27"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Sphere <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape<span style="color: #008000;">&#123;</span>...<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> Cone   <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape<span style="color: #008000;">&#123;</span>...<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Une premiere implémentation de la factory pourrait donner ceci :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code28'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16428"><td class="code" id="p164code28"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> ShapeFactory<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  Shape <span style="color: #000040;">*</span> createSphere<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> Sphere<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  Shape <span style="color: #000040;">*</span> createCone<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> Cone<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Ce qui donne à l'utilisation :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code29'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16429"><td class="code" id="p164code29"><pre class="cpp" style="font-family:monospace;">ShapeFactory factory<span style="color: #008080;">;</span>
&nbsp;
Shape <span style="color: #000040;">*</span> sphere <span style="color: #000080;">=</span> factory.<span style="color: #007788;">createSphere</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
sphere<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>display<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Cette première approche fonctionne, mais a le désavantage de rendre le code de la factory dépendant de l'application.<br />
Nous allons tenter de définir la factory de manière générique (template <img src='http://www.coder-studio.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) afin de pouvoir la réutiliser dans d'autres cas.</p>
<h2>On templatise un peu ?</h2>
<p style="text-align: justify;">On peut voir la factory comme une map : à une clef donnée, on associe un type d'objet a créer.</p>
<p style="text-align: justify;">On devrait donc avoir au niveau des paramètres de la factory générique, le type de la clef et le type d'objet que la factory retourne. Ceci nous donne le prototype suivant :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code30'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16430"><td class="code" id="p164code30"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">class</span> Factory<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Et à  l'utilisation :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code31'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16431"><td class="code" id="p164code31"><pre class="cpp" style="font-family:monospace;">Factory shapeFactory<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">De plus, on s'attend à pouvoir écrire quelque chose du genre :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code32'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16432"><td class="code" id="p164code32"><pre class="cpp" style="font-family:monospace;">Shape <span style="color: #000040;">*</span>sphere <span style="color: #000080;">=</span> shapeFactory.<span style="color: #007788;">create</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Sphere&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">On enrichit donc notre esquisse de la classe  :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code33'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16433"><td class="code" id="p164code33"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">class</span> Factory<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  Object <span style="color: #000040;">*</span> create<span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Pour le corps de la méthode create, nous avons besoin de réellement créer une instance d'un objet que la factory ne connait pas. Comme ceci ne peut pas se faire par magie, il va falloir enregistrer dans la factory une fonction capable de créer chaque objet et les associer à une clef.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code34'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16434"><td class="code" id="p164code34"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">class</span> Factory<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
<span style="color: #666666;">// un pointeur de fonction qui retourne un Object*</span>
  <span style="color: #0000ff;">typedef</span> Object <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>Creator<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  map _registeredCreator<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">bool</span> <span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span>Key key, Creator creator<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>_registeredCreator.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> _registeredCreator.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">// la clef est deja utilisé</span>
    _registeredCreator.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>pair<span style="color: #008000;">&#40;</span>key, creator<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  Object <span style="color: #000040;">*</span> create<span style="color: #008000;">&#40;</span>Key key<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    Object <span style="color: #000040;">*</span>object<span style="color: #008080;">;</span>
    Creator creator<span style="color: #008080;">;</span>
    map<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// on cherche le pointeur de fonction associé a la clef</span>
    it <span style="color: #000080;">=</span> _registeredCreator.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>it <span style="color: #000080;">==</span> _registeredCreator.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span> <span style="color: #666666;">// on ne l'a pas trouvé</span>
&nbsp;
    <span style="color: #666666;">// on récupère le pointeur de fonction</span>
    creator <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">second</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// on appelle la fonction pour créer un nouvel objet</span>
    object <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>creator<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// on retourne l'objet</span>
    <span style="color: #0000ff;">return</span> object<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Regardons maintenant comment utiliser tout ça :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code35'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16435"><td class="code" id="p164code35"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// des fonctions de création d'objets</span>
Shape <span style="color: #000040;">*</span>createSphere<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> Sphere<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
Shape <span style="color: #000040;">*</span>createCone<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> Cone<span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
&nbsp;
Factory shapeFactory<span style="color: #008080;">;</span>
shapeFactory.<span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Sphere&quot;</span>, createSphere<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
shapeFactory.<span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Cone&quot;</span>, createCone<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
Shape <span style="color: #000040;">*</span>sphere <span style="color: #000080;">=</span> shapeFactory.<span style="color: #007788;">create</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Sphere&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Ici le problème reste la nécessité d'avoir des fonctions "C" à mettre dans le code, et de devoir enregistrer ces fonctions à la main. Donc à chaque ajout d'objet géométrique devoir aller remodifier ce code d'initialisation de la factory.</p>
<p style="text-align: justify;">Pour rendre ceci un peu plus propre, nous allons définir ces fonctions en statique dans chaque objet géométrique et se servir de l'initialisation des CRT pour enregistrer les fonctions automatiquement.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code36'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16436"><td class="code" id="p164code36"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// sphere.h</span>
<span style="color: #0000ff;">class</span> Sphere <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">static</span> Shape<span style="color: #000040;">*</span> create<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> <span style="color: #0000ff;">register</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code37'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16437"><td class="code" id="p164code37"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// sphere.pp</span>
<span style="color: #339900;">#include &quot;ShapeFactory.h&quot; // on considère que la shapeFactory est une variable globale accessible par ce fichier</span>
&nbsp;
Shape <span style="color: #000040;">*</span> Shpere<span style="color: #008080;">::</span><span style="color: #007788;">create</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> Sphere<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #666666;">// auto enregistrement :</span>
<span style="color: #0000ff;">bool</span> Sphere<span style="color: #008080;">::</span><span style="color: #0000ff;">register</span> <span style="color: #000080;">=</span> shapeFactory.<span style="color: #0000ff;">register</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Sphere&quot;</span>, Sphere<span style="color: #008080;">::</span><span style="color: #007788;">create</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Il est possible d'utiliser des macros pour encore réduire la taille du code à produire.</p>
<p style="text-align: justify;">Petite explication : au lancement du programme, la variable statique register est initialisée. Cette initialisation passe par l'appel de la methode shapeFactory::register qui enregistre le "create".</p>
<h2>Conlusion:</h2>
<p style="text-align: justify;">J'ai ici présenté l'implémentation "générique" d'une factory; pour pousser un peu plus l'exemple du modeleur, on peut imaginer que l'interface Shape permette de décrire des attributs (association nom / valeur). Une sphère par exemple aura un paramètre de type float nommé "radius".<br />
Notre petite application graphique aura à parcourir les entrées de la factory pour y découvrir tous les objets "instanciables" et fournir à l'utilisateur un panel de boutons pour créer des Shapes.<br />
A partir de là, à chaque Shape créé, elle pourra construire une boîte de dialogue d'édition adaptée à l'objet (à la mode 3DS), SANS en connaitre le type réel.</p>
<h2>Pour aller plus loin :</h2>
<p style="text-align: justify;">Il est possible de s'abstraire de l'écriture de la fonction create statique dans les classes en utilisant les templates pour créer des objets dont le rôle est de remplacer la fonction statique.</p>
<p style="text-align: justify;">A l'utilisation cela donne quelque chose comme :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p164code38'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16438"><td class="code" id="p164code38"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> Shape<span style="color: #008080;">::</span><span style="color: #0000ff;">register</span> <span style="color: #000080;">=</span> shapeFactory.<span style="color: #0000ff;">register</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Shape&quot;</span>, Creator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Il faut pour cela modifier un peu la classe factory, mais je vous le laisse en exercice <img src='http://www.coder-studio.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  (j'ai toujours révé de dire ça un jour...)</p>
<p style="text-align: justify;">Dernier point, on peut utiliser les singleton pour ne pas avoir à stocker la factory dans une variable globale.</p>
<p><span style="color: #ff0000;"><strong>joyeuses fetes <img src='http://www.coder-studio.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </strong></span></p>
<p><strong>Twxs</strong></p>
<blockquote><p>Cet article provient du tutorial publié par Twxs le 24/12/2004 sur l'ancien site de Coder-Studio.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.coder-studio.com/blog/la-factory/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Le design pattern Observer/Observable en C++</title>
		<link>http://www.coder-studio.com/blog/le-design-pattern-observer-observable-en-c/</link>
		<comments>http://www.coder-studio.com/blog/le-design-pattern-observer-observable-en-c/#comments</comments>
		<pubDate>Wed, 03 Nov 2004 17:32:25 +0000</pubDate>
		<dc:creator>twxs</dc:creator>
				<category><![CDATA[C & C++]]></category>
		<category><![CDATA[design pattern]]></category>

		<guid isPermaLink="false">http://r19649.ovh.net/cs/blog/?p=156</guid>
		<description><![CDATA[Ce tutorial a pour but de proposer une implementation en C++ du patron de conception Observer / Observable. Comme il s'agit a la base d'un patron de conception, il en existe par definition beaucoup d'implementations specifiques au domaine. Ici nous allons proposer une implementation "generique" qui sera utilisable dans la majorite des cas. Introduction La [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Ce tutorial a pour but de proposer une implementation en C++ du patron de conception Observer / Observable. Comme il s'agit a la base d'un patron de conception, il en existe par definition beaucoup d'implementations specifiques au domaine.<br />
Ici nous allons proposer une implementation "generique" qui sera utilisable dans la majorite des cas.</p>
<p><span id="more-156"></span></p>
<h2>Introduction</h2>
<p style="text-align: justify;">La relation Observer/Observable est un concept assez simple, le principe repose sur le fait de pouvoir informer automatiquement des objets (Observeurs) des modification d'un autre (l'Observable). Nous prendrons comme cas "d'école" l'exemple d'un VU mètre system. On supposera avoir une classe <span style="color: #ff0000;">CPU </span>permettant de connaitre la charge cpu, une classe <span style="color: #ff0000;">FileSystem</span> donnant accès aux informations concernant les disques et enfin, une classe charger d'afficher l'état de notre machine i.e. la charge CPU et l'occupation des disques.</p>
<p style="text-align: justify;"><strong>La classe Observer</strong><br />
L'observer est un objet "simple", être observeur defini uniquement le fait de pouvoir être mis a jour par un objet extérieur. Ceci se traduit par avoir une méthode de type <span style="color: #ff0000;">update</span>.<br />
Ceci nous mène à une premiere définition de l'interface Observer :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code50'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15650"><td class="code" id="p156code50"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Observer <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Rendre un objet observateur d'un sujet, se traduira alors par une implémentation de cette interface. ex :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code51'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15651"><td class="code" id="p156code51"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> VUMeter <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Observer <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// mis a jour car un sujet a changé d'etat</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">La première limitation de cette proposition vient du fait que l'on ne prend pas en compte le type du sujet observe dans l'interface. Ceci a l'utilisation, obligera en plus d'hériter de l'interface, de stocker dans ses attributs une référence au sujet. ex :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code52'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15652"><td class="code" id="p156code52"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> VUMeter <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Observer<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span> <span style="color: #008080;">:</span>
  Subject <span style="color: #000040;">*</span>subjet<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span>
  <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// subject a change</span>
    subject<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getValues<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>...
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">une première solution, est de passer en paramètre la classe Observable dans la méthode update.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code53'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15653"><td class="code" id="p156code53"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>Observable <span style="color: #000040;">*</span>subject<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">//</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">cette solution est presque viable, mais va obliger :</p>
<ul>
<li> soit a implementer dans l'observable, une interface generique pour récuperer les information d'un sujet "abstrait". Il nous faudra récuperer la charge CPU, l'occupation du disque...</li>
<li> soit a "dynamic_caster" le sujet de type Observable en son reel type</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code54'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15654"><td class="code" id="p156code54"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>Observable <span style="color: #000040;">*</span>subjet<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>CPU <span style="color: #000040;">*</span>cpu <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #008000;">&#40;</span>subject<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
     cpu<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getLoad<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>...
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>FileSystem<span style="color: #000040;">*</span> fs <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #008000;">&#40;</span>subject<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    fs<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFreeSpace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Cette solution n'est pas des plus élegantes. Une derniere alternative plus "propre" serait de posseder une méthode update prenant en paremetre un CPU* et une autre prenant un FileSystem*</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code55'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15655"><td class="code" id="p156code55"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>FileSystem<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>CPU<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Cette solution nous garantie une separation propre du code. Pour l'implementer, nous allons devoir passer par l'utilisation de template. Le principe : spécifier le type reel observé dans l'interface <span style="color: #ff0000;">Observer</span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code56'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15656"><td class="code" id="p156code56"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>
<span style="color: #0000ff;">struct</span>  Observer<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>Subject <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Et voici maintenant l'utilisation :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code57'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15657"><td class="code" id="p156code57"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> VUMeter <span style="color: #008080;">:</span>
  <span style="color: #0000ff;">public</span> Observer,
  <span style="color: #0000ff;">public</span> Observer<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>FileSystem<span style="color: #000040;">*</span> fs<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    fs<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFreeSpace<span style="color: #008000;">&#40;</span>...
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">void</span> update<span style="color: #008000;">&#40;</span>CPU<span style="color: #000040;">*</span> cpu<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    cpu<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>...
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Et voila le travail, nous avons un observeur capable d'observer plusieurs sujets. Ils nous reste a implémenter l'autre partie : l'<strong>Observable</strong></p>
<p style="text-align: justify;"><strong>La classe Observable</strong><br />
Nous devons ici definir la classe de laquelle vont deriver CPU et FileSystem pour être observable. Les besoins d'un Observable sont :</p>
<ul>
<li> lui attacher un observateur</li>
<li> le détacher</li>
<li> notifier tous les observateurs</li>
<li> stocker ses observateurs</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code58'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15658"><td class="code" id="p156code58"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Observable<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">set</span> _observers<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> attach<span style="color: #008000;">&#40;</span>Observer <span style="color: #000040;">*</span>o<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// insertion dans le set</span>
  <span style="color: #0000ff;">void</span> detach<span style="color: #008000;">&#40;</span>Observer <span style="color: #000040;">*</span>o<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// suppression du set</span>
  <span style="color: #0000ff;">void</span> notify<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #666666;">// updater tous les observers dans le set</span>
    ...
      <span style="color: #007788;">obs</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>update<span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Cette implémentation ne fonctionne pas a cause de la "templatisation" de la class Observer qui prend, déjà, pour la méthode update le type réel du sujet (ex : FileSystem) mais aussi par le fait que les observeurs n'héritent pas de Observer, mais Observer.<br />
Dans la classe Obserable, nous devons donc connaitre le type reel du sujet.<br />
Encore une fois les template vont permettre de nous en sortir :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code59'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15659"><td class="code" id="p156code59"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span>  <span style="color: #0000ff;">class</span> Observable
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  ~Observable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> attach<span style="color: #008000;">&#40;</span>Observer <span style="color: #000040;">*</span>o<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> detach<span style="color: #008000;">&#40;</span>Observer <span style="color: #000040;">*</span>o<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> notify<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">set</span><span style="color: #000080;">&lt;</span>Observer <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> _observers<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>A l'utilisation, ceci donne :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p156code60'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15660"><td class="code" id="p156code60"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> FileSystem <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Observable<span style="color: #008000;">&#123;</span>
   ...
   <span style="color: #0000ff;">void</span> some_func<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
     ...
     <span style="color: #007788;">notify</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// on change d'etat, informer les observateurs</span>
   <span style="color: #008000;">&#125;</span>
   ...
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
VUMeter vum<span style="color: #008080;">;</span>
&nbsp;
FileSystem fs<span style="color: #008080;">;</span>
CPU cpu<span style="color: #008080;">;</span>
&nbsp;
cpu.<span style="color: #007788;">attach</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>vum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
fs.<span style="color: #007788;">attach</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>vum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Et Voilà, c'est fini!</p>
<blockquote><p>Cet article provient du tutorial publié par Twxs le 03/11/2004 sur l'ancien site de Coder-Studio.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.coder-studio.com/blog/le-design-pattern-observer-observable-en-c/feed/</wfw:commentRss>
		<slash:comments>1746</slash:comments>
		</item>
	</channel>
</rss>

