<?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>Three Wise Men &#187; Programming</title> <atom:link href="http://www.twm-kd.com/category/computers/programming-computers/feed/" rel="self" type="application/rss+xml" /><link>http://www.twm-kd.com</link> <description>In desperate need of a punchline.</description> <lastBuildDate>Sun, 22 Jan 2012 13:21:51 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Generating sample XML from XSD or DTD in Eclipse</title><link>http://www.twm-kd.com/programming-computers/generating-sample-xml-from-xsd-or-dtd-in-eclipse/</link> <comments>http://www.twm-kd.com/programming-computers/generating-sample-xml-from-xsd-or-dtd-in-eclipse/#comments</comments> <pubDate>Fri, 04 Jun 2010 11:55:26 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[DTD]]></category> <category><![CDATA[Eclipse]]></category> <category><![CDATA[Howto]]></category> <category><![CDATA[sample]]></category> <category><![CDATA[XML]]></category> <category><![CDATA[XSD]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=3793</guid> <description><![CDATA[Generating sample XML's from XSD or DTD files in Eclipse couldn't be easier. With one mouse right click you can be on your way to XML sample heaven.]]></description> <content:encoded><![CDATA[<div
class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright"><dt
class="wp-caption-dt"><a
href="http://www.flickr.com/photos/15464638@N06/1920146627"><img
title="XMarks XML schema diagram v1.1" src="http://static.twm-kd.com/twm-uploads/1920146627_514b442824_m.jpg" alt="XMarks XML schema diagram v1.1" /></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a
href="http://www.flickr.com/photos/15464638@N06/1920146627">carol shergold</a> via Flickr</dd></dl></div></div><p>Feature is useful if you are writing your <a
class="zem_slink" title="XML Schema (W3C)" rel="wikipedia" href="http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29">XSD</a> schema files or you have XSD schema files and want to create sample <a
class="zem_slink" title="XML" rel="wikipedia" href="http://en.wikipedia.org/wiki/XML">XML</a> files for schema elements. Process is the same for generating sample XML files from <a
class="zem_slink" title="Document Type Definition" rel="wikipedia" href="http://en.wikipedia.org/wiki/Document_Type_Definition">DTD</a> files.</p><p>How to do it. Simple.</p><ol><li>Create a project and add, import or somehow see created, generated XSD files in <em>Project Explorer</em>.</li><li>Select your desired XSD file.</li><li><em>Right-click</em> and choose <em>Generate-&gt;XML File</em></li><li><em>New XML File wizard will pop-up</em> where you define where generated file should be saved and what should be it&#8217;s name. When desired information is input-ed press <em>Next</em>.</li><li>In <em>Select Root Element window</em> choose <a
class="zem_slink" title="Root element" rel="wikipedia" href="http://en.wikipedia.org/wiki/Root_element">root element</a> of your sample XML file. Root element can be any element defined in XSD file you choose to use for generation of the sample XML.</li><li>Set content options and pay special attention to optional attributes &amp; optional elements. Check those if you would like a complete XML sample.</li><li>Name-space information is populated with name-spaces defined in chosen XSD file. You can manipulate the existing name-space information by changing prefix, adding additional name-space or create new name-space.</li><li>Once you are satisfied click <em>Finish</em>.</li></ol><p>And presto you have your blank sample XML. Edit it with the information you wish to present and your sample XML is ready. No XMLSpy or any other commercial tools required.</p><div
class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img
class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=e9a3321b-db81-47bf-b1d3-e10c46211233" alt="" /><span
class="zem-script pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div> ]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/generating-sample-xml-from-xsd-or-dtd-in-eclipse/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>XMLbeans scomp fails with &#8220;java.io.IOException: CreateProcess error=2, The system cannot find the file specified&#8221;</title><link>http://www.twm-kd.com/programming-computers/xmlbeans-scomp-fails-with-java-io-ioexception-createprocess-error2-the-system-cannot-find-the-file-specified/</link> <comments>http://www.twm-kd.com/programming-computers/xmlbeans-scomp-fails-with-java-io-ioexception-createprocess-error2-the-system-cannot-find-the-file-specified/#comments</comments> <pubDate>Mon, 25 Jan 2010 13:53:07 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[scomp]]></category> <category><![CDATA[Windows]]></category> <category><![CDATA[XMLbeans]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=3368</guid> <description><![CDATA[Solving the mysterious xmlbeans scomp javac issue by simply reading scomp documentation.]]></description> <content:encoded><![CDATA[<div
class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright" style="width: 161px;"><dt
class="wp-caption-dt"><a
href="http://commons.wikipedia.org/wiki/Image:Alubia_pinta_alavesa2.jpg"><img
title="Beans Alubia pinta alavesa. Álava, Spain." src="http://static.twm-kd.com/twm-uploads/300px-Alubia_pinta_alavesa2.jpg" alt="Beans Alubia pinta alavesa. Álava, Spain." width="151" height="113" /></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a
href="http://commons.wikipedia.org/wiki/Image:Alubia_pinta_alavesa2.jpg">Wikipedia</a></dd></dl></div></div><p>If you are using <a
href="http://xmlbeans.apache.org/">xmlbeans</a> on Windows to bind your schema to Java types then you are probably using a tool bundled with the distribution called <em>scomp</em> or Schema Compiler. For schema compiler to work you will need to correctly setup computer environment by installing JDK. You can follow through the following check-list to detect if any additional installation steps are required on your machine setup:</p><ul><li><em>Start-&gt;Run-&gt;type cmd</em> and type <em>java</em> in the command line. If no error then your setup is correct; otherwise setup <em>JAVA_HOME</em> and add <em>%JAVA_HOME%/bin</em> to your <em>Path</em> environment variable and try again.</li><li>S<em>tart-&gt;Run-&gt;type cmd</em> and type <em>scomp</em> in the command line. If no error then your setup is correct; otherwise setup <em>XMLBEANS_HOME</em> and add <em>%XMLBEANS_HOME%/bin</em> to your <em>Path</em> environment variable and try again.</li></ul><p>Then run scomp. Depending on your wishes you will probably wan&#8217;t to create a .jar file of the Java types scomp compiles from schema files. I used the following command line:</p><div
class="wp_syntax"><div
class="code"><pre class="dos" style="font-family:monospace;">scomp -d schema -src src -out my_schema_1_0_0.jar ./schema/mySchema.xsd</pre></div></div><p>where mySchema.xsd file was stored in ./schema folder.</p><p>If you get my_schema_1_0_0.jar, by some coincidence then kudos to you, but most likely you will end up with something similar to this error:</p><div
class="wp_syntax"><div
class="code"><pre class="dos" style="font-family:monospace;">Time to build schema type system: 1.282 seconds
Time to generate code: 18.687 seconds
java.io.IOException: Cannot run program &quot;C:\your current path\javac&quot;: CreateProcess error=2, The system cannot find the file specified
java.io.IOException: CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program &quot;C:\your current path\javac&quot;: CreateProcess error=2, The system cannot find the file specified
 at java.lang.ProcessBuilder.start<span style="color: #33cc33;">(</span>Unknown Source<span style="color: #33cc33;">)</span>
 at java.lang.Runtime.exec<span style="color: #33cc33;">(</span>Unknown Source<span style="color: #33cc33;">)</span>
 at java.lang.Runtime.exec<span style="color: #33cc33;">(</span>Unknown Source<span style="color: #33cc33;">)</span>
 ... 6 more
BUILD FAILED</pre></div></div><p>Read on to solve this problem.</p><p><span
id="more-3368"></span>Yay!!!  There is a problem in your Path environment variable. Let&#8217;s try to troubleshoot. Did you install JDK? If you did not then install it. If you did you probably installed it by accepting default installation values. It was installed to <em>C:\Program files\Java\jdk&lt;version&gt;</em>.</p><p>The problem is &#8220;Program files&#8221;. See the space? Java does not like spaces in the command line. As in any other programming language, space represents additional argument to the program. You could try to replace the <em>C:\Program files</em> with classical 8.3 old MS-DOS format, which 1980 users love so much. Execute dir /x in C:\ and replace your &#8220;Program files&#8221; with <em>PROGRA~2</em> or <em>PROGRA~1</em> or whatever Windows thinks &#8220;Program files&#8221; maps to in your <em>JAVA_HOME</em> and <em>Path</em> environment variables. I am guessing that this will not win me a gold star and you are still reading the next paragraph. And I promise you that if you click one of the Support Us links on the right side you will have three less starving wise men in the world, and you will feel content with your self by clicking on it. But your Path environment variables are now setup correctly.</p><p>Now you could search the web and find the answer. Use 2&#8217;5 hours of your precious time and crack this. Or you could read documentation for scomp. By being smart and reading documentation for scomp you will discover that scomp can solve this Windows nightmare for you. By using the -compiler parameter in command line you get something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="dos" style="font-family:monospace;">scomp -d schema -src src -out my_schema_1_0_0.jar ./schema/mySchema.xsd -compiler C:\Program Files\Java\jdk1.6.0_03\bin\javac.exe</pre></div></div><p>Problem solved.</p><p>Note: If you have huge .xsd schemas then you will have to boost memory external Java compiler can use. Use <em>-xm 512m</em> or <em>-xm 1024m</em> to successfully compile your schema.</p><div
class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a
class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/362b4757-2359-415b-a203-23c8c7cfe6ec/"><img
class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=362b4757-2359-415b-a203-23c8c7cfe6ec" alt="Reblog this post [with Zemanta]" /></a><span
class="zem-script pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div> ]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/xmlbeans-scomp-fails-with-java-io-ioexception-createprocess-error2-the-system-cannot-find-the-file-specified/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Workaround &#8211; IE6+ local script restrictions</title><link>http://www.twm-kd.com/programming-computers/workaround-ie6-local-script-restrictions/</link> <comments>http://www.twm-kd.com/programming-computers/workaround-ie6-local-script-restrictions/#comments</comments> <pubDate>Mon, 14 Dec 2009 08:18:25 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[HTTP]]></category> <category><![CDATA[Internet Explorer]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Microsoft]]></category> <category><![CDATA[MOTW]]></category> <category><![CDATA[Tip]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=2881</guid> <description><![CDATA[Web development just got harder with for IE browser due to non-standard extensions. ]]></description> <content:encoded><![CDATA[<div
class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright" style="width: 103px;"><dt
class="wp-caption-dt"><a
href="http://en.wikipedia.org/wiki/Image:Internet_Explorer_7_Logo.png"><img
title="Internet Explorer Mobile" src="http://static.twm-kd.com/twm-uploads/Internet_Explorer_7_Logo.png" alt="Internet Explorer Mobile" width="93" height="93" /></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a
href="http://en.wikipedia.org/wiki/Image:Internet_Explorer_7_Logo.png">Wikipedia</a></dd></dl></div></div><p>In the early years of internet growth I had the opportunity to work as a web designer. As web designer I designed a web page. Simple not too complicated. And it rocked. Literally. Admired by many. By today&#8217;s standards web page was LAME. Totally. But it used an interesting JavaScript. As now I was using Linux at the time and I replaced the default mouse cursor with cool animated Linux penguin. The penguin was fabulous. The animation was interesting. The script was flawless. It did not bother me that the script was not working in IE because at that time netscape ruled the web and Microsoft did not bother to make it work on Linux.<br
/> <span
id="more-2881"></span><br
/> To fully appreciate what happened next look at one of the high budget horror movies that the industry is feeding you. Dark times came over web development. World wide web &#8211; that was once brewing in the black cauldron; broiled over and  development became gruesome nightmare. Wars broke out. Major player corporations prayed on the unsuspected developers and lured them to their cause.  There were some casualties; but mainly in the management area and web site development became an art. Most of the wars ended in status quo or with legislation disputes and web developers got standardization. Or did we?</p><p>Once you get a task of developing a web page you surely think about which technology you will be using. You weigh between Adobe Flash and Silverlight, client side or server side scripting. But in the end you still have to write an HTML document. Usually you will want to include some kind of client side script. Probably JavaScript so that you are platform independent. For example you will want to do simple background image replacement triggered on HTML elements onMouseOver event. Thinking like this will force you to  create a document that is similar to this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td
class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;JavaScript Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;square&quot; style=&quot;margin: 0pt auto; background-color: green; width: 100px; height: 100px;&quot; onmouseover=&quot;document.getElementById('square').style.backgroundColor='red';&quot; onmouseout=&quot;document.getElementById('square').style.backgroundColor='green';&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div><div
id="attachment_2934" class="wp-caption alignright" style="width: 310px"><img
class="size-medium wp-image-2934" title="IE-SecurityWarning" src="http://static.twm-kd.com/twm-uploads/IE-SecurityWarning-300x32.png" alt="Security warning without MOTW in IE6+" height="32" width="300"><p
class="wp-caption-text">Security warning without MOTW in IE6+</p></div><p>Result will look promising in every browser. Except IE. Specially if you are developing for 6+ version of the IE.&nbsp; Not knowing that Microsoft expertly decided that their web browser MUST improve security of your computer.&nbsp; And here is where a developer stops with development and starts searching for a solution.</p><p>Solution is quite simple if you know where to look for it. The web page running in web browser is run locally and is not hosted on any web server. As a result IE forces web page to run in the security zone of the location the page was saved from. Since the page was created on your local computer this is pretty stupid but for the sake of our computer security let&#8217;s go with it. Microsoft also states that this is true as long as that security zone is more restrictive than the Local Machine zone, otherwise Local Machine zone is used. So we opened the file and it got bumped to Local Machine Zone which does not allow executing any script without blocking it. Now I am clapping as mad to the ingenuity of IE developers and on the other hand seeping madly through tons of useless internet garbage until I discovered MOTW. MOTW &#8211; is an acronym meaning &#8220;Mark of the Web&#8221;.</p><p>Actually it is an HTML comment which is added to the markup of the page right after &lt;!DOCTYPE/&gt; element. When a user opens the Web page from their local machine, Internet Explorer references this comment to determine the security zone in which it should run the page. For local domain it will look like this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
</pre></td><td
class="code"><pre class="html" style="font-family:monospace;">&lt;!-- saved from url=(0014)about:internet --&gt;</pre></td></tr></table></div><p>By adding this comment to my HTML page JavaScript miraculously started to work. Bravo Microsoft. Here is just another example of the standardization of the web:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre class="html" style="font-family:monospace;">&lt;!-- saved from url=(0014)about:internet --&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;JavaScript Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;square&quot; style=&quot;margin: 0pt auto; background-color: green; width: 100px; height: 100px;&quot; onmouseover=&quot;document.getElementById('square').style.backgroundColor='red';&quot; onmouseout=&quot;document.getElementById('square').style.backgroundColor='green';&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div><div
style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a
class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/27f9e4c2-f04b-4822-bfcc-3502e1fa1ee8/" title="Reblog this post [with Zemanta]"><img
style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=27f9e4c2-f04b-4822-bfcc-3502e1fa1ee8" alt="Reblog this post [with Zemanta]"></a><span
class="zem-script pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div> ]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/workaround-ie6-local-script-restrictions/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Sketchy musical factory</title><link>http://www.twm-kd.com/programming-computers/sketchy-musical-factory/</link> <comments>http://www.twm-kd.com/programming-computers/sketchy-musical-factory/#comments</comments> <pubDate>Mon, 20 Jul 2009 09:00:55 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Abstract Factory]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[Computer programming]]></category> <category><![CDATA[Design pattern]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[programming]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=570</guid> <description><![CDATA[Image via Wikipedia Today we will take a look at the abstract factory design pattern. This design pattern continues the series of creational patterns. In previous article we have talked about factory pattern. We have learned that factory pattern deals with creation of other objects. Now we will extend that knowledge with the abstract factory [...]<hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/' rel='bookmark'>Fungi Factory without abstraction</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/' rel='bookmark'>Singing a singular design with Singleton</a></li></ol></div>]]></description> <content:encoded><![CDATA[<div
class="zemanta-img" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright" style="width: 310px;"><dt
class="wp-caption-dt"><a
href="http://commons.wikipedia.org/wiki/Image:Abstract_Factory_design_pattern.png"><img
title="The Abstract Factory pattern illustration, UML..." src="http://static.twm-kd.com/twm-uploads/300px-Abstract_Factory_design_pattern.png" alt="The Abstract Factory pattern illustration, UML..." width="300" height="167" /></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a
href="http://commons.wikipedia.org/wiki/Image:Abstract_Factory_design_pattern.png">Wikipedia</a></dd></dl></div></div><p>Today we will take a look at the abstract factory design pattern. This design pattern continues the series of creational patterns.</p><p>In <a
href="http://www.twm-kd.com/computers/fungi-factory-without-abstraction/">previous article </a>we have talked about factory pattern. We have learned that factory pattern deals with creation of other objects. Now we will extend that knowledge with the abstract factory pattern which provides a way to group together or rather encapsulate a group of individual factories that create objects with common theme.</p><p>Normally we would code a client software to create a concrete implementation of the abstract factory and then use generic interfaces to create concrete objects that are tuned to specific theme but, the client does not know about which concrete object will it get from each of the internal factories since it uses generic interfaces of the factory product in question. Details of the implementation of concrete objects are hidden by the internal factories.</p><p><span
id="more-570"></span></p><p>Now lets look at an example.</p><p>In <a
class="zem_slink" title="Java (software platform)" rel="homepage" href="http://java.sun.com">Java</a>:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MusicalFactory <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> MusicMedia media<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MusicalFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MusicalFactory<span style="color: #009900;">&#40;</span>MusicMedia _media<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    media <span style="color: #339933;">=</span> _media<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Music createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> media.<span style="color: #006633;">createMusic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Music <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MusicMedia <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Music createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Queen <span style="color: #000000; font-weight: bold;">implements</span> Music <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Queen&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TheBeatles <span style="color: #000000; font-weight: bold;">implements</span> Music <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;The Beatles&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CompactDisc <span style="color: #000000; font-weight: bold;">implements</span> MusicMedia <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Music createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Queen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LongPlay <span style="color: #000000; font-weight: bold;">implements</span> MusicMedia <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Music createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> TheBeatles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MusicMedia createMediaBasedOnEra<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> _year<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_year <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1982</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MusicalFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000; font-weight: bold;">new</span> LongPlay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MusicalFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000; font-weight: bold;">new</span> CompactDisc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>argv.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> year <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      MusicalFactory factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MusicalFactory<span style="color: #009900;">&#40;</span>createMediaBasedOnEra<span style="color: #009900;">&#40;</span>year<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You love: &quot;</span> <span style="color: #339933;">+</span> factory.<span style="color: #006633;">createMusic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Usage: java MusicalFactory &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>In <a
class="zem_slink" title="PHP" rel="homepage" href="http://php.net/">PHP</a>:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Music <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> MusicMedia <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Queen implements Music <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Queen&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TheBeatles implements Music <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;The Beatles&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CompactDisc implements MusicMedia <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Queen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LongPlay implements MusicMedia <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> TheBeatles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MusicalFactory <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$media</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">function</span> MusicalFactory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$media</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$media</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$media</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$media</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> createMediaBasedOnEra<span style="color: #009900;">&#40;</span><span style="color: #000088;">$year</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$year</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1982</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> CompactDisc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> LongPlay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$musicalFactory</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MusicalFactory<span style="color: #009900;">&#40;</span>createMediaBasedOnEra<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1980</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You like: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$musicalFactory</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>createMusic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>In <a
class="zem_slink" title="Python (programming language)" rel="homepage" href="http://www.python.org/">Python</a>:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td
class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Music:
    <span style="color: #ff7700;font-weight:bold;">def</span> getName<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">NotImplementedError</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MusicMedia:
    <span style="color: #ff7700;font-weight:bold;">def</span> createMusic<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">NotImplementedError</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Queen<span style="color: black;">&#40;</span>Music<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> getName<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Queen&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TheBeetles<span style="color: black;">&#40;</span>Music<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> getName<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;The Beetles&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CompactDisc<span style="color: black;">&#40;</span>MusicMedia<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> createMusic<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> Queen<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> LongPlay<span style="color: black;">&#40;</span>MusicMedia<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> createMusic<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> TheBeetles<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MusicalFactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    musicalFactory = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, musicalFactory<span style="color: black;">&#41;</span>:
        MusicalFactory.<span style="color: black;">musicalFactory</span> = musicalFactory
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> createMusic<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> MusicalFactory.<span style="color: black;">musicalFactory</span>.<span style="color: black;">createMusic</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> createMediaBasedOnEra<span style="color: black;">&#40;</span>year<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> year <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1982</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> LongPlay<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> CompactDisc<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    factory = MusicalFactory<span style="color: black;">&#40;</span>createMediaBasedOnEra<span style="color: black;">&#40;</span><span style="color: #ff4500;">1980</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You like: %s&quot;</span> <span style="color: #66cc66;">%</span> factory.<span style="color: black;">createMusic</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">getName</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div><p>In C++:<br
/> Header file:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #0000ff;">class</span> Music
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> getName<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>
&nbsp;
<span style="color: #0000ff;">class</span> MusicMedia <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> Music<span style="color: #000040;">*</span> createMusic<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>
&nbsp;
<span style="color: #0000ff;">class</span> Queen <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Music
 <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> getName<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: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> TheBeetles <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Music <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> getName<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: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> CompactDisc <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MusicMedia <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	Music<span style="color: #000040;">*</span> createMusic<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: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> LongPlay <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> MusicMedia <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	Music<span style="color: #000040;">*</span> createMusic<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: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> MusicFactory <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	MusicFactory<span style="color: #008000;">&#40;</span>MusicMedia<span style="color: #000040;">*</span> factory<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	Music<span style="color: #000040;">*</span> createMusic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	MusicMedia<span style="color: #000040;">*</span> m_factory<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>And source file:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;abstractfactory.hpp&quot;</span>
&nbsp;
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> Queen<span style="color: #008080;">::</span><span style="color: #007788;">getName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Queen&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> TheBeetles<span style="color: #008080;">::</span><span style="color: #007788;">getName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The Beatles&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Music<span style="color: #000040;">*</span> CompactDisc<span style="color: #008080;">::</span><span style="color: #007788;">createMusic</span><span style="color: #008000;">&#40;</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> Queen<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Music<span style="color: #000040;">*</span> LongPlay<span style="color: #008080;">::</span><span style="color: #007788;">createMusic</span><span style="color: #008000;">&#40;</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> TheBeetles<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
MusicFactory<span style="color: #008080;">::</span><span style="color: #007788;">MusicFactory</span><span style="color: #008000;">&#40;</span>MusicMedia<span style="color: #000040;">*</span> factory<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	m_factory <span style="color: #000080;">=</span> factory<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Music<span style="color: #000040;">*</span> MusicFactory<span style="color: #008080;">::</span><span style="color: #007788;">createMusic</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>m_factory<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>createMusic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
MusicMedia<span style="color: #000040;">*</span> createMediaBasedOnEra<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> _year<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>_year <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #0000dd;">1982</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> LongPlay<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">new</span> CompactDisc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	MusicFactory factory <span style="color: #000080;">=</span> MusicFactory<span style="color: #008000;">&#40;</span>createMediaBasedOnEra<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1980</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;You like: &quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> factory.<span style="color: #007788;">createMusic</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>getName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The main idea of the example is that <em>createMediaBasedOnEra()</em> method creates a <em>MusicMedia</em>. MusicMedia is an abstract factory that can produce <em>Music</em> based on that era. Now the example is intended to show how we can hide implementation details.</p><p>As already shown factory is the location where objects are constructed. Pattern&#8217;s intent is to isolate the creation of objects from their usage. New derived types can be introduced in the code without significant code change to the base class. Downside of the implementation of this patten is additional complexity to the code. It can also mean additional initial effort for developers. You should weigh cost &#8211; benefit before implementation.</p><div
class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a
class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/c730b9a3-ea54-4299-919c-9ca75e3d58fb/"><img
class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=c730b9a3-ea54-4299-919c-9ca75e3d58fb" alt="Reblog this post [with Zemanta]" /></a><span
class="zem-script more-related more-info pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div> <br
/><hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/' rel='bookmark'>Fungi Factory without abstraction</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/' rel='bookmark'>Singing a singular design with Singleton</a></li></ol></div>]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/sketchy-musical-factory/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Fungi Factory without abstraction</title><link>http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/</link> <comments>http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/#comments</comments> <pubDate>Tue, 12 May 2009 08:56:47 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[Computer programming]]></category> <category><![CDATA[Design pattern]]></category> <category><![CDATA[Factory]]></category> <category><![CDATA[Factory method]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[programming]]></category> <category><![CDATA[python]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=418</guid> <description><![CDATA[Next creational pattern that we will have a closer look at &#8211; is Factory pattern. There are at least two different Factory patterns, Factory method and Abstract Factory. In this installment we will be looking at the Factory method pattern. Image via Wikipedia The Factory method pattern deals with creating objects without specifying the exact [...]<hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/sketchy-musical-factory/' rel='bookmark'>Sketchy musical factory</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/' rel='bookmark'>Singing a singular design with Singleton</a></li></ol></div>]]></description> <content:encoded><![CDATA[<p>Next <a
class="zem_slink" title="Creational pattern" rel="wikipedia" href="http://en.wikipedia.org/wiki/Creational_pattern">creational pattern</a> that we will have a closer look at &#8211; is Factory pattern. There are at least two different Factory patterns, Factory method and Abstract Factory. In this installment we will be looking at the <a
class="zem_slink" title="Factory method pattern" rel="wikipedia" href="http://en.wikipedia.org/wiki/Factory_method_pattern">Factory method pattern</a>.</p><p><span
id="more-418"></span></p><div
class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright" style="width: 310px;"><dt
class="wp-caption-dt"><a
href="http://commons.wikipedia.org/wiki/Image:FactoryMethod.svg"><img
title="UML class diagram for the factory method pattern" src="http://static.twm-kd.com/twm-uploads/300px-factorymethodsvg.png" alt="UML class diagram for the factory method pattern" width="300" height="177"></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a
href="http://commons.wikipedia.org/wiki/Image:FactoryMethod.svg">Wikipedia</a></dd></dl></div></div><p>The Factory method pattern deals with creating objects without specifying the exact class of the <a
class="zem_slink" title="Object (computer science)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Object_%28computer_science%29">object</a> that will be created. Pattern defines separate methods for creating objects, whose subclasses can be overridden to specify the derived type of the product that factory will create. It is basically any method whose purpose is creation of the objects.</p><p>Factory method pattern is used to encapsulate the creation of objects.&nbsp; When this process is complex or is not simple to understand, than it is prudent to use factory method pattern in your design. Pattern is useful if you are writing a toolkit of framework code where you want to encapsulate the creation of toolkit or framework specific objects. You will definitely benefit from factory methods when you have parallel <a
class="zem_slink" title="Class hierarchy" rel="wikipedia" href="http://en.wikipedia.org/wiki/Class_hierarchy">class hierarchy</a> between objects, because one type of objects will have to be able to create objects from other type.&nbsp; There is where encapsulated factory method becomes handy. You will come in contact with this pattern with the applications that are unit tested. More precisely in applications that use test driven development as a base for their development cycle.</p><p>Well I think it is time for some examples. As the name of our post tells us we will create a factory method which will produce fungi. If we look at the <a
class="zem_slink" title="Unified Modeling Language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unified_Modeling_Language">UML</a> our Product will be replaced with Fungus. Our ConcreteCreator and Creator will be replaced with FungusFactory. Method called factoryMethod will be replaced with create method. Now let&#8217;s create some fungus in <a
class="zem_slink" title="Java (software platform)" rel="homepage" href="http://java.sun.com">Java</a>.</p><pre file="FungusFactory.java" colla="+" lang="Java">public class FungusFactory {
	private static FungusFactory instance;
	public enum FungusFamily {
		Microsporidia,
		Chytridiomycota,
		Zoopagomycotina
	}
	public interface Fungus {
		public String getFamily();
	}
	public class MicrospordiaFungus implements Fungus {
		private String family = "Microsporidia";
		public String getFamily() { return family; }
	}
	public class ChytridiomycotaFungus implements Fungus {
		private String family = "Chytridiomycota";
		public String getFamily() { return family; }
	}
	public class ZoopagomycotinaFungus implements Fungus {
		private String family = "Zoopagomycotina";
		public String getFamily() { return family; }
	}
	public static synchronized Fungus create(FungusFamily _family) {
		if (instance == null)
			instance = new FungusFactory();
		switch (_family) {
			case Microsporidia:
				return instance.new MicrospordiaFungus();
			case Chytridiomycota:
				return instance.new ChytridiomycotaFungus();
			case Zoopagomycotina:
				return instance.new ZoopagomycotinaFungus();
		}
		throw new IllegalArgumentException("This factory can't " +
                             "make fungus of unknown family [" + _family + "]") ;
	}
	public static void main(String [] argv) {
		Fungus microsporidiaFungus =
                    FungusFactory.create(FungusFactory.FungusFamily.Microsporidia);
		System.out.println(microsporidiaFungus.getFamily());
	}
}
</pre><p>In <a
class="zem_slink" title="PHP" rel="homepage" href="http://php.net/">PHP</a>:</p><pre file="factory.php" lang="PHP">class FungusFactory {
  static function create($family) { // As of PHP 5.3.0
    switch ($family) {
     case 'Microsporidia': return new MicrosporidiaFungus();
     case 'Chytridiomycota': return new ChytridiomycotaFungus();
     case 'Zoopagomycotina': return new ZoopagomycotinaFungus();
  }
}
interface Fungus {
  function getFamily();
}
class MicrosporidiaFungus implements Fungus {
  function getFamily() { return 'Microsporidia'; }
}
class ChytridiomycotaFungus implements Fungus {
  function getFamily() { return 'Chytridiomycota'; }
}
class ZoopagomycotinaFungus implements Fungus {
  function getFamily() { return 'Zoopagomycotina'; }
}
function test() {
  echo FungusFactory::create('Microsporidia')->getFamily();
}</pre><p>In python:</p><pre file="factory.py" lang="Python">class FungusFactory:
    def create( self, family ):
        if family == "Microsporidia":
            return MicrospordiaFungus()
        elif family == "Chytridiomycota":
            return ChytridiomycotaFungus()
        elif family == "Zoopagomycotina":
            return ZoopagomycotinaFungus()
        else:
            return Fungus()
   create = staticmethod(create)
class Fungus:
    def getFamily( self ): raise NotImplementedError
class MicrospordiaFungus( Fungus ):
    def getFamily( self ): return "Microsporidia"
class ChytridiomycotaFungus ( Fungus ):
    def getFamily( self ): return "Chytridiomycota"
class ZoopagomycotinaFungus ( Fungus ):
    def getFamily( self ): return "Zoopagomycotina"
if __name__ == "__main__":
    print FungusFactory.create("Microsporidia").getFamily()
</pre><p>In C++:</p><p>Header:</p><pre file="factory.hpp" lang="cpp">class Fungus {
public:
  virtual std::string getFamily() = 0;
};
class MicrosporidiaFungus: public Fungus {
public:
  std::string getFamily();
};
class ChytridiomycotaFungus: public Fungus {
public:
  std::string getFamily();
};
class ZoopagomycotinaFungus: public Fungus {
public:
  std::string getFamily();
};
class FungusFactory {
public:
  static const int MICROSPORIDIA = 1;
  static const int CHYTRIDIOMYCOTA = 2;
  static const int ZOOPAGOMYCOTINA = 3;
  static Fungus* create(int type);
};
</pre><p>Source:</p><pre file="factory.cpp" lang="cpp">#include "factory.hpp"
Fungus* FungusFactory::create(int family)
{
  switch (family)
  {
    case FungusFactory::MICROSPORIDIA:
      return new MicrosporidiaFungus();
      break;
    case FungusFactory::CHYTRIDIOMYCOTA:
      return new ChytridiomycotaFungus();
      break;
    case FungusFactory::ZOOPAGOMYCOTINA:
      return new ZoopagomycotinaFungus();
      break;
  }
  throw new std::string("This factory can't make fungus of unknown family");
}
std::string MicrosporidiaFungus::getFamily()
{
  return std::string("Microsporidia");
}
std::string ChytridiomycotaFungus::getFamily()
{
  return std::string("Chytridiomycota");
}
std::string ZoopagomycotinaFungus::getFamily()
{
  return std::string("Zoopagomycotina");
}
int main(int argc, char *argv[])
{
  std::cout << FungusFactory::create(FungusFactory::MICROSPORIDIA)->getFamily() << std::endl;
}
</pre><p>Now as with singleton there are some drawbacks from using the factory method pattern. If you are re-factoring existing code you should watch out for your client classes which you will break in process. All instances of object creation that you will replace by factory methods must be replaced with factory method calls. Now if you have an application that creates a lot of objects in the code you have your work cut out for you. The factory method relays on private constructor, hence it can not be extended. If we by any chance make the constructor protected or even good forbid public and somehow are able to extend the existing factory, the extended factory must re-implement all the factory methods defined in base factory.</p><p>This is all the juice we have for today. Next week we will look at another factory pattern. That one will have more abstraction in it.</p><div
style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a
class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/ee3e6dd9-1efd-4dcd-832f-0802e2da9384/" title="Reblog this post [with Zemanta]"><img
style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=ee3e6dd9-1efd-4dcd-832f-0802e2da9384" alt="Reblog this post [with Zemanta]"></a><span
class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div> <br
/><hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/sketchy-musical-factory/' rel='bookmark'>Sketchy musical factory</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/' rel='bookmark'>Singing a singular design with Singleton</a></li></ol></div>]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Singing a singular design with Singleton</title><link>http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/</link> <comments>http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/#comments</comments> <pubDate>Wed, 06 May 2009 07:36:22 +0000</pubDate> <dc:creator>RRunner</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[Computer programming]]></category> <category><![CDATA[Design pattern]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[programming]]></category> <category><![CDATA[python]]></category> <category><![CDATA[Singleton]]></category> <category><![CDATA[Singleton pattern]]></category> <guid
isPermaLink="false">http://www.twm-kd.com/?p=219</guid> <description><![CDATA[Now from my perspective there are a lot of programmers that finish school, get a degree, get a good job and then are presented with a problem. For example program designers design applications that include one of most simple design patterns. Now I am not the best programmer in the world and also not the [...]<hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/' rel='bookmark'>Fungi Factory without abstraction</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/sketchy-musical-factory/' rel='bookmark'>Sketchy musical factory</a></li></ol></div>]]></description> <content:encoded><![CDATA[<p>Now from my perspective there are a lot of programmers that finish school, get a degree, get a good job and then are presented with a problem. For example program designers design applications that include one of most simple design patterns.</p><p>Now I am not the best programmer in the world and also not the worst. Maybe I was lucky to be born in the era in which pattern design sprouted and started to grow. Also I know that most of you dear readers don&#8217;t need to reread this simple stuff, but if you are unsure of something &#8230; go ahead and take a peek. We will not tell ;)</p><p>This should be a first of <a
class="zem_slink" title="Design pattern (computer science)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29">design pattern</a> series. So come back and check out the one of which you are unsure of.<span
id="more-219"></span><strong></strong></p><p><strong>The illusive Singleton</strong></p><p>It is a design pattern that is used to control and restrict instantiation of a class to one and only one <a
class="zem_slink" title="Object (computer science)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Object_%28computer_science%29">object</a>. Simple, ehh? Why for God&#8217;s sake would you want to do that? Since this pattern is a <a
class="zem_slink" title="Creational pattern" rel="wikipedia" href="http://en.wikipedia.org/wiki/Creational_pattern">creational pattern</a> we use it when we need exactly one object to coordinate some events or actions across the whole system.</p><div
class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;"><div><dl
class="wp-caption alignright" style="width: 210px;"><dt
class="wp-caption-dt"><a
href="http://commons.wikipedia.org/wiki/Image:Singleton_UML_class_diagram.svg"><img
title="UML class diagram for Singleton software desig..." src="http://static.twm-kd.com/twm-uploads/200px-singleton_uml_class_diagramsvg.png" alt="UML class diagram for Singleton software desig..." width="200" height="120"></a></dt><dd
class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a
href="http://commons.wikipedia.org/wiki/Image:Singleton_UML_class_diagram.svg">Wikipedia</a></dd></dl></div></div><p>On the right we have a diagram in <a
class="zem_slink" title="Unified Modeling Language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unified_Modeling_Language">UML</a> notation which shows how our singleton class should look like.</p><p>We can use this design pattern for instantiation of other design pattens, to describe objects that describe states of the system, use them as a global reference to the instances of specific classes, because they don&#8217;t pollute the global namespace with unnecessary variables and they allow lazy allocation and initialization.</p><p>When implementing singleton programmers must bare in mind that we need only one instance of the class and that instance requires global access principle. It is implemented by creating a class with a method that creates a new instance of the singleton class if one does not exist. If the instance already exists a reference to that object is returned. Implementation needs to make sure that no other classes can instantiate the class so usually constructor is hidden. When programming in <a
class="zem_slink" title="Thread (computer science)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Thread_%28computer_science%29">multi-threaded</a> applications and two threads access the creation method at the same time and the singleton instance does not yet exist, they should both check for an instance and only one should create the new instance. This is usually solved by using mutual exclusion on the class that indicated that the object is being instantiated.</p><p>For this example I will use <a
class="zem_slink" title="Java (software platform)" rel="homepage" href="http://java.sun.com">Java</a></p><pre line="1" file="Singleton.java" colla="+" lang="Java">public class Singleton
{
   private static Singleton singleton;
   private Singleton() { }
   public static synchronized Singleton getInstance()
   {
     if (singleton == null)
     {
       singleton = new Singleton();
     }
     return singleton;
    }
}</pre><p>And there you have it &#8230; The Java Singleton. Java not your thing, how about <a
class="zem_slink" href="http://php.net/" title="PHP" rel="homepage">PHP</a>?</p><pre line="1" file="singleton.php" colla="-" lang="PHP">class Singleton
{
  protected static $instance;
  private function __construct() { }
  private function __clone() { }
  public static function getInstance()
  {
    if (null === self::$_instance) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }
}
</pre><p>Still not satisfied ? How about python ?</p><pre line="1" file="Singleton.py" colla="-" lang="Python">class Singleton(object):
    def __new__(type):
        if not '_the_instance' in type.__dict__:
            type._the_instance = object.__new__(type)
        return type._the_instance
</pre><p> Maybe C++ ? The header file.</p><pre line="1" file="singleton.hpp" colla="-" lang="C">using namespace std;
class Singleton
{
public:
  static Singleton&#038; GetInstance();
  ~Singleton();
private:
  Singleton();
  static Singleton * s_instance;
}
</pre><p> The source file.</p><pre line="1" file="singleton.cpp" colla="-" lang="C">Singleton * Singleton::s_instance = 0;
Singleton::Singleton() { }
Singleton::~Singleton()
{
  if (s_instance != 0)
  {
    delete s_instance;
  }
}
Singleton&#038; Singleton::GetInstance()
{
  if (s_instance == 0)
  {
    s_instance = new Singleton();
  }
  return s_instance;
}
</pre><p>An that is all I can tell you about singing with Singletons. Hmm &#8230; wait not quite.</p><p>It should be noted that the singleton pattern, although simple to understand, does not get much love from the &#8220;gurus&#8221;. First of all it will make all the code depend on that class, meaning if a singleton class is changed all of the code that uses that class will be broken and has to be mended. Also since it uses private and <a
class="zem_slink" title="Method (computer science)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Method_%28computer_science%29">static methods</a> this pattern is sometimes seen as an anti pattern. Code can be difficult to unit test since the pattern introduces global state and unit tests need to be aware of that.</p><p>Next we will tackle the Factory patterns and see what we can manufacture with them. Till next time.</p><div
style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a
class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/5bcdfc22-0583-441c-990c-7ab60572ec56/" title="Reblog this post [with Zemanta]"><img
style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=5bcdfc22-0583-441c-990c-7ab60572ec56" alt="Reblog this post [with Zemanta]"></a><span
class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div> <br
/><hr
/><div
class='related-posts'><h5>Related Posts:</h5><ol><li><a
href=' http://www.twm-kd.com/programming-computers/fungi-factory-without-abstraction/' rel='bookmark'>Fungi Factory without abstraction</a></li><li><a
href=' http://www.twm-kd.com/programming-computers/sketchy-musical-factory/' rel='bookmark'>Sketchy musical factory</a></li></ol></div>]]></content:encoded> <wfw:commentRss>http://www.twm-kd.com/programming-computers/singing-a-singular-design-with-singleton/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
