Commits

Anonymous committed 128587e

add src/_texinputs for making w/ custom page sizes (change the sphinx.sty in there); make generate latex / pdf on Windows w/ MikTeX.

Comments (0)

Files changed (74)

Python3PatternsIdioms.pdf

Binary file modified.

html/AppFrameworks.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Building Application Frameworks &mdash; Python 3 Patterns, Recipes and Idioms</title>
+    <link rel="stylesheet" href="_static/default.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+          URL_ROOT:    '',
+          VERSION:     '1.0',
+          COLLAPSE_MODINDEX: false,
+          FILE_SUFFIX: '.html'
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="index" title="Index" href="genindex.html" />
+    <link rel="search" title="Search" href="search.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Fronting for an Implementation" href="Fronting.html" />
+    <link rel="prev" title="The Singleton" href="Singleton.html" /> 
+  </head>
+  <body>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="Fronting.html" title="Fronting for an Implementation"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Singleton.html" title="The Singleton"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>  
+    <div class="document">
+      <div class="documentwrapper">
+        <div class="bodywrapper">
+          <div class="body">
+            
+  
+  <div class="section" id="building-application-frameworks">
+<h1>Building Application Frameworks<a class="headerlink" href="#building-application-frameworks" title="Permalink to this headline">¶</a></h1>
+<p>An application framework allows you to inherit from a class or set of classes
+and create a new application, reusing most of the code in the existing classes
+and overriding one or more methods in order to customize the application to your
+needs. A fundamental concept in the application framework is the <em>Template
+Method</em> which is typically hidden beneath the covers and drives the application
+by calling the various methods in the base class (some of which you have
+overridden in order to create the application).</p>
+<p>For example, whenever you create an applet you&#8217;re using an application
+framework: you inherit from <strong>JApplet</strong> and then override <strong>init( )</strong>. The
+applet mechanism (which is a <em>Template Method</em>) does the rest by drawing the
+screen, handling the event loop, resizing, etc.</p>
+<div class="section" id="template-method">
+<h2>Template Method<a class="headerlink" href="#template-method" title="Permalink to this headline">¶</a></h2>
+<p>An important characteristic of the <em>Template Method</em> is that it is defined in
+the base class and cannot be changed. It&#8217;s sometimes a <strong>private</strong> method but
+it&#8217;s virtually always <strong>final</strong>. It calls other base-class methods (the ones you
+override) in order to do its job, but it is usually called only as part of an
+initialization process (and thus the client programmer isn&#8217;t necessarily able to
+call it directly):</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># AppFrameworks/TemplateMethod.py</span>
+<span class="c"># Simple demonstration of Template Method.</span>
+
+<span class="k">class</span> <span class="nc">ApplicationFramework</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">__templateMethod</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">__templateMethod</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">5</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">customize1</span><span class="p">()</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">customize2</span><span class="p">()</span>
+
+<span class="c"># Create an &quot;application&quot;:</span>
+<span class="k">class</span> <span class="nc">MyApp</span><span class="p">(</span><span class="n">ApplicationFramework</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">customize1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Nudge, nudge, wink, wink! &quot;</span><span class="p">,)</span>
+    <span class="k">def</span> <span class="nf">customize2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Say no more, Say no more!&quot;</span><span class="p">)</span>
+
+<span class="n">MyApp</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>The base-class constructor is responsible for performing the necessary
+initialization and then starting the &#8220;engine&#8221; (the template method) that runs
+the application (in a GUI application, this &#8220;engine&#8221; would be the main event
+loop). The client programmer simply provides definitions for <strong>customize1( )</strong>
+and <strong>customize2( )</strong> and the &#8220;application&#8221; is ready to run.</p>
+<p>We&#8217;ll see <em>Template Method</em> numerous other times throughout the book.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic">
+<li><p class="first">Create a framework that takes a list of file names on the command line. It
+opens each file except the last for reading, and the last for writing. The
+framework will process each input file using an undetermined policy and
+write the output to the last file. Inherit to customize this framework to
+create two separate applications:</p>
+<blockquote>
+<ol class="arabic simple">
+<li>Converts all the letters in each file to uppercase.</li>
+<li>Searches the files for words given in the first file.</li>
+</ol>
+</blockquote>
+</li>
+</ol>
+</div>
+</div>
+
+
+          </div>
+        </div>
+      </div>
+      <div class="sphinxsidebar">
+        <div class="sphinxsidebarwrapper">
+            <p class="logo"><a href="index.html">
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
+            </a></p>
+    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
+    
+            <h3><a href="index.html">Table Of Contents</a></h3>
+            <ul>
+<li><a class="reference external" href="">Building Application Frameworks</a><ul>
+<li><a class="reference external" href="#template-method">Template Method</a></li>
+<li><a class="reference external" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+            <h4>Previous topic</h4>
+            <p class="topless"><a href="Singleton.html" title="previous chapter">The Singleton</a></p>
+            <h4>Next topic</h4>
+            <p class="topless"><a href="Fronting.html" title="next chapter">Fronting for an Implementation</a></p>
+            <h3>This Page</h3>
+            <ul class="this-page-menu">
+              <li><a href="_sources/AppFrameworks.txt">Show Source</a></li>
+            </ul>
+    
+          <h3>Quick search</h3>
+            <form class="search" action="search.html" method="get">
+              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
+              <input type="hidden" name="check_keywords" value="yes" />
+              <input type="hidden" name="area" value="default" />
+            </form>
+    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
+    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
+    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>
+
+        </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="Fronting.html" title="Fronting for an Implementation"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Singleton.html" title="The Singleton"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>
+    <div class="footer">
+      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
+    </div>
+  </body>
+</html>

html/CanonicalScript.html

   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
-    <title>A Canonical Form for Command-Line Programs &mdash; Python 3 Patterns &amp; Idioms</title>
+    <title>A Canonical Form for Command-Line Programs &mdash; Python 3 Patterns, Recipes and Idioms</title>
     <link rel="stylesheet" href="_static/default.css" type="text/css" />
     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
     <script type="text/javascript">
     <link rel="shortcut icon" href="_static/favicon.ico"/>
     <link rel="index" title="Index" href="genindex.html" />
     <link rel="search" title="Search" href="search.html" />
-    <link rel="top" title="Python 3 Patterns &amp; Idioms" href="index.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
     <link rel="next" title="Messenger/Data Transfer Object" href="Messenger.html" />
     <link rel="prev" title="Discovering the Details About Your Platform" href="MachineDiscovery.html" /> 
   </head>
         <li class="right" >
           <a href="MachineDiscovery.html" title="Discovering the Details About Your Platform"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>  
     <div class="document">
       <div class="sphinxsidebar">
         <div class="sphinxsidebarwrapper">
             <p class="logo"><a href="index.html">
-              <img class="logo" src="_static/Logo.png" alt="Logo"/>
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
             </a></p>
     <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
     
         <li class="right" >
           <a href="MachineDiscovery.html" title="Discovering the Details About Your Platform"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>
     <div class="footer">
       &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
-      Last updated on Nov 25, 2008.
-      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
     </div>
   </body>
 </html>

html/ChangeInterface.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Changing the Interface &mdash; Python 3 Patterns, Recipes and Idioms</title>
+    <link rel="stylesheet" href="_static/default.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+          URL_ROOT:    '',
+          VERSION:     '1.0',
+          COLLAPSE_MODINDEX: false,
+          FILE_SUFFIX: '.html'
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="index" title="Index" href="genindex.html" />
+    <link rel="search" title="Search" href="search.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Table-Driven Code: Configuration Flexibility" href="TableDriven.html" />
+    <link rel="prev" title="Function Objects" href="FunctionObjects.html" /> 
+  </head>
+  <body>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="TableDriven.html" title="Table-Driven Code: Configuration Flexibility"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>  
+    <div class="document">
+      <div class="documentwrapper">
+        <div class="bodywrapper">
+          <div class="body">
+            
+  
+  <div class="section" id="changing-the-interface">
+<h1>Changing the Interface<a class="headerlink" href="#changing-the-interface" title="Permalink to this headline">¶</a></h1>
+<p>Sometimes the problem that you&#8217;re solving is as simple as &#8220;I don&#8217;t have the
+interface that I want.&#8221; Two of the patterns in <em>Design Patterns</em> solve this
+problem: <em>Adapter</em> takes one type and produces an interface to some other type.
+<em>Façade</em> creates an interface to a set of classes, simply to provide a more
+comfortable way to deal with a library or bundle of resources.</p>
+<div class="section" id="adapter">
+<h2>Adapter<a class="headerlink" href="#adapter" title="Permalink to this headline">¶</a></h2>
+<p>When you&#8217;ve got <em>this</em>, and you need <em>that</em>, <em>Adapter</em> solves the problem. The
+only requirement is to produce a <em>that</em>, and there are a number of ways you can
+accomplish this adaptation:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># ChangeInterface/Adapter.py</span>
+<span class="c"># Variations on the Adapter pattern.</span>
+
+<span class="k">class</span> <span class="nc">WhatIHave</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+    <span class="k">def</span> <span class="nf">h</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">WhatIWant</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">ProxyAdapter</span><span class="p">(</span><span class="n">WhatIWant</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whatIHave</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span> <span class="o">=</span> <span class="n">whatIHave</span>
+
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="c"># Implement behavior using</span>
+        <span class="c"># methods in WhatIHave:</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">WhatIUse</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">op</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whatIWant</span><span class="p">):</span>
+        <span class="n">whatIWant</span><span class="o">.</span><span class="n">f</span><span class="p">()</span>
+
+<span class="c"># Approach 2: build adapter use into op():</span>
+<span class="k">class</span> <span class="nc">WhatIUse2</span><span class="p">(</span><span class="n">WhatIUse</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">op</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whatIHave</span><span class="p">):</span>
+        <span class="n">ProxyAdapter</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span><span class="o">.</span><span class="n">f</span><span class="p">()</span>
+
+<span class="c"># Approach 3: build adapter into WhatIHave:</span>
+<span class="k">class</span> <span class="nc">WhatIHave2</span><span class="p">(</span><span class="n">WhatIHave</span><span class="p">,</span> <span class="n">WhatIWant</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+<span class="c"># Approach 4: use an inner class:</span>
+<span class="k">class</span> <span class="nc">WhatIHave3</span><span class="p">(</span><span class="n">WhatIHave</span><span class="p">):</span>
+    <span class="k">class</span> <span class="nc">InnerAdapter</span><span class="p">(</span><span class="n">WhatIWant</span><span class="p">):</span>
+        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">outer</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span> <span class="o">=</span> <span class="n">outer</span>
+        <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+    <span class="k">def</span> <span class="nf">whatIWant</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="n">WhatIHave3</span><span class="o">.</span><span class="n">InnerAdapter</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
+
+<span class="n">whatIUse</span> <span class="o">=</span> <span class="n">WhatIUse</span><span class="p">()</span>
+<span class="n">whatIHave</span> <span class="o">=</span> <span class="n">WhatIHave</span><span class="p">()</span>
+<span class="n">adapt</span><span class="o">=</span> <span class="n">ProxyAdapter</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span>
+<span class="n">whatIUse2</span> <span class="o">=</span> <span class="n">WhatIUse2</span><span class="p">()</span>
+<span class="n">whatIHave2</span> <span class="o">=</span> <span class="n">WhatIHave2</span><span class="p">()</span>
+<span class="n">whatIHave3</span> <span class="o">=</span> <span class="n">WhatIHave3</span><span class="p">()</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">adapt</span><span class="p">)</span>
+<span class="c"># Approach 2:</span>
+<span class="n">whatIUse2</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span>
+<span class="c"># Approach 3:</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave2</span><span class="p">)</span>
+<span class="c"># Approach 4:</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave3</span><span class="o">.</span><span class="n">whatIWant</span><span class="p">())</span>
+</pre></div>
+</div>
+<p>I&#8217;m taking liberties with the term &#8220;proxy&#8221; here, because in <em>Design Patterns</em>
+they assert that a proxy must have an identical interface with the object that
+it is a surrogate for. However, if you have the two words together: &#8220;proxy
+adapter,&#8221; it is perhaps more reasonable.</p>
+</div>
+<div class="section" id="facade">
+<h2>Façade<a class="headerlink" href="#facade" title="Permalink to this headline">¶</a></h2>
+<p>A general principle that I apply when I&#8217;m casting about trying to mold
+requirements into a first-cut object is &#8220;If something is ugly, hide it inside an
+object.&#8221; This is basically what <em>Façade</em> accomplishes. If you have a rather
+confusing collection of classes and interactions that the client programmer
+doesn&#8217;t really need to see, then you can create an interface that is useful for
+the client programmer and that only presents what&#8217;s necessary.</p>
+<p>Façade is often implemented as singleton abstract factory. Of course, you can
+easily get this effect by creating a class containing <strong>static</strong> factory
+methods:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># ChangeInterface/Facade.py</span>
+<span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">B</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="c"># Other classes that aren&#39;t exposed by the</span>
+<span class="c"># facade go here ...</span>
+
+<span class="k">class</span> <span class="nc">Facade</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">makeA</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">A</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeA</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeA</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">makeB</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">B</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeB</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeB</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">makeC</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeC</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeC</span><span class="p">)</span>
+
+<span class="c"># The client programmer gets the objects</span>
+<span class="c"># by calling the static methods:</span>
+<span class="n">a</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeA</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span>
+<span class="n">b</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeB</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span>
+<span class="n">c</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeC</span><span class="p">(</span><span class="mf">1.0</span><span class="p">);</span>
+</pre></div>
+</div>
+<p>[rewrite this section using research from Larman&#8217;s book]</p>
+<p>Example for Facade (?): my &#8220;nicer&#8221; version of the XML library.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic simple">
+<li>Create an adapter class that automatically loads a two-dimensional array of
+objects into a dictionary as key-value pairs.</li>
+</ol>
+</div>
+</div>
+
+
+          </div>
+        </div>
+      </div>
+      <div class="sphinxsidebar">
+        <div class="sphinxsidebarwrapper">
+            <p class="logo"><a href="index.html">
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
+            </a></p>
+    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
+    
+            <h3><a href="index.html">Table Of Contents</a></h3>
+            <ul>
+<li><a class="reference external" href="">Changing the Interface</a><ul>
+<li><a class="reference external" href="#adapter">Adapter</a></li>
+<li><a class="reference external" href="#facade">Façade</a></li>
+<li><a class="reference external" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+            <h4>Previous topic</h4>
+            <p class="topless"><a href="FunctionObjects.html" title="previous chapter">Function Objects</a></p>
+            <h4>Next topic</h4>
+            <p class="topless"><a href="TableDriven.html" title="next chapter">Table-Driven Code: Configuration Flexibility</a></p>
+            <h3>This Page</h3>
+            <ul class="this-page-menu">
+              <li><a href="_sources/ChangeInterface.txt">Show Source</a></li>
+            </ul>
+    
+          <h3>Quick search</h3>
+            <form class="search" action="search.html" method="get">
+              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
+              <input type="hidden" name="check_keywords" value="yes" />
+              <input type="hidden" name="area" value="default" />
+            </form>
+    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
+    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
+    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>
+
+        </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="TableDriven.html" title="Table-Driven Code: Configuration Flexibility"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>
+    <div class="footer">
+      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
+    </div>
+  </body>
+</html>

html/Comprehensions.html

   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
-    <title>Comprehensions &mdash; Python 3 Patterns &amp; Idioms</title>
+    <title>Comprehensions &mdash; Python 3 Patterns, Recipes and Idioms</title>
     <link rel="stylesheet" href="_static/default.css" type="text/css" />
     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
     <script type="text/javascript">
     <link rel="shortcut icon" href="_static/favicon.ico"/>
     <link rel="index" title="Index" href="genindex.html" />
     <link rel="search" title="Search" href="search.html" />
-    <link rel="top" title="Python 3 Patterns &amp; Idioms" href="index.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
     <link rel="next" title="Coroutines &amp; Concurrency" href="CoroutinesAndConcurrency.html" />
     <link rel="prev" title="Generators, Iterators, and Itertools" href="GeneratorsIterators.html" /> 
   </head>
         <li class="right" >
           <a href="GeneratorsIterators.html" title="Generators, Iterators, and Itertools"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>  
     <div class="document">
       <div class="sphinxsidebar">
         <div class="sphinxsidebarwrapper">
             <p class="logo"><a href="index.html">
-              <img class="logo" src="_static/Logo.png" alt="Logo"/>
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
             </a></p>
     <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
     
         <li class="right" >
           <a href="GeneratorsIterators.html" title="Generators, Iterators, and Itertools"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>
     <div class="footer">
       &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
-      Last updated on Nov 25, 2008.
-      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
     </div>
   </body>
 </html>

html/Contributors.html

   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
-    <title>Contributors &mdash; Python 3 Patterns &amp; Idioms</title>
+    <title>Contributors &mdash; Python 3 Patterns, Recipes and Idioms</title>
     <link rel="stylesheet" href="_static/default.css" type="text/css" />
     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
     <script type="text/javascript">
     <link rel="shortcut icon" href="_static/favicon.ico"/>
     <link rel="index" title="Index" href="genindex.html" />
     <link rel="search" title="Search" href="search.html" />
-    <link rel="top" title="Python 3 Patterns &amp; Idioms" href="index.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
     <link rel="next" title="ToDo List" href="ToDo.html" />
-    <link rel="prev" title="Python 3 Patterns &amp; Idioms" href="index.html" /> 
+    <link rel="prev" title="Python 3 Patterns, Recipes and Idioms" href="index.html" /> 
   </head>
   <body>
     <div class="related">
           <a href="ToDo.html" title="ToDo List"
              accesskey="N">next</a> |</li>
         <li class="right" >
-          <a href="index.html" title="Python 3 Patterns &amp; Idioms"
+          <a href="index.html" title="Python 3 Patterns, Recipes and Idioms"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>  
     <div class="document">
       <div class="sphinxsidebar">
         <div class="sphinxsidebarwrapper">
             <p class="logo"><a href="index.html">
-              <img class="logo" src="_static/Logo.png" alt="Logo"/>
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
             </a></p>
     <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
     
 
 
             <h4>Previous topic</h4>
-            <p class="topless"><a href="index.html" title="previous chapter">Python 3 Patterns &amp; Idioms</a></p>
+            <p class="topless"><a href="index.html" title="previous chapter">Python 3 Patterns, Recipes and Idioms</a></p>
             <h4>Next topic</h4>
             <p class="topless"><a href="ToDo.html" title="next chapter">ToDo List</a></p>
             <h3>This Page</h3>
           <a href="ToDo.html" title="ToDo List"
              accesskey="N">next</a> |</li>
         <li class="right" >
-          <a href="index.html" title="Python 3 Patterns &amp; Idioms"
+          <a href="index.html" title="Python 3 Patterns, Recipes and Idioms"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>
     <div class="footer">
       &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
-      Last updated on Nov 25, 2008.
-      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
     </div>
   </body>
 </html>

html/CoroutinesAndConcurrency.html

   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
-    <title>Coroutines &amp; Concurrency &mdash; Python 3 Patterns &amp; Idioms</title>
+    <title>Coroutines &amp; Concurrency &mdash; Python 3 Patterns, Recipes and Idioms</title>
     <link rel="stylesheet" href="_static/default.css" type="text/css" />
     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
     <script type="text/javascript">
     <link rel="shortcut icon" href="_static/favicon.ico"/>
     <link rel="index" title="Index" href="genindex.html" />
     <link rel="search" title="Search" href="search.html" />
-    <link rel="top" title="Python 3 Patterns &amp; Idioms" href="index.html" />
-    <link rel="next" title="Part II: Idioms" href="Part2.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Jython" href="Jython.html" />
     <link rel="prev" title="Comprehensions" href="Comprehensions.html" /> 
   </head>
   <body>
           <a href="genindex.html" title="General Index"
              accesskey="I">index</a></li>
         <li class="right" >
-          <a href="Part2.html" title="Part II: Idioms"
+          <a href="Jython.html" title="Jython"
              accesskey="N">next</a> |</li>
         <li class="right" >
           <a href="Comprehensions.html" title="Comprehensions"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>  
     <div class="document">
       <div class="sphinxsidebar">
         <div class="sphinxsidebarwrapper">
             <p class="logo"><a href="index.html">
-              <img class="logo" src="_static/Logo.png" alt="Logo"/>
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
             </a></p>
     <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
     
             <h4>Previous topic</h4>
             <p class="topless"><a href="Comprehensions.html" title="previous chapter">Comprehensions</a></p>
             <h4>Next topic</h4>
-            <p class="topless"><a href="Part2.html" title="next chapter">Part II: Idioms</a></p>
+            <p class="topless"><a href="Jython.html" title="next chapter">Jython</a></p>
             <h3>This Page</h3>
             <ul class="this-page-menu">
               <li><a href="_sources/CoroutinesAndConcurrency.txt">Show Source</a></li>
           <a href="genindex.html" title="General Index"
              accesskey="I">index</a></li>
         <li class="right" >
-          <a href="Part2.html" title="Part II: Idioms"
+          <a href="Jython.html" title="Jython"
              accesskey="N">next</a> |</li>
         <li class="right" >
           <a href="Comprehensions.html" title="Comprehensions"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>
     <div class="footer">
       &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
-      Last updated on Nov 25, 2008.
-      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
     </div>
   </body>
 </html>

html/Decorator.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Decorator: Dynamic Type Selection &mdash; Python 3 Patterns, Recipes and Idioms</title>
+    <link rel="stylesheet" href="_static/default.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+          URL_ROOT:    '',
+          VERSION:     '1.0',
+          COLLAPSE_MODINDEX: false,
+          FILE_SUFFIX: '.html'
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="index" title="Index" href="genindex.html" />
+    <link rel="search" title="Search" href="search.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Iterators: Decoupling Algorithms from Containers" href="Iterators.html" />
+    <link rel="prev" title="StateMachine" href="StateMachine.html" /> 
+  </head>
+  <body>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="Iterators.html" title="Iterators: Decoupling Algorithms from Containers"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="StateMachine.html" title="StateMachine"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>  
+    <div class="document">
+      <div class="documentwrapper">
+        <div class="bodywrapper">
+          <div class="body">
+            
+  
+  <div class="section" id="decorator-dynamic-type-selection">
+<h1>Decorator: Dynamic Type Selection<a class="headerlink" href="#decorator-dynamic-type-selection" title="Permalink to this headline">¶</a></h1>
+<div class="admonition note">
+<p class="first admonition-title">Note</p>
+<p class="last">I think we can rewrite this chapter to use Python decorators as
+implementation (thus the decorators chapter should precede this one).</p>
+</div>
+<p>The use of layered objects to dynamically and transparently add responsibilities
+to individual objects is referred to as the <em>decorator</em> pattern.</p>
+<p>Used when subclassing creates too many (&amp; inflexible) classes</p>
+<p>All decorators that wrap around the original object must have the same basic
+interface</p>
+<p>Dynamic proxy/surrogate?</p>
+<p>This accounts for the odd inheritance structure</p>
+<p>Tradeoff: coding is more complicated when using decorators</p>
+<div class="section" id="basic-decorator-structure">
+<h2>Basic Decorator Structure<a class="headerlink" href="#basic-decorator-structure" title="Permalink to this headline">¶</a></h2>
+<img alt="_images/decorator.gif" src="_images/decorator.gif" />
+</div>
+<div class="section" id="a-coffee-example">
+<h2>A Coffee Example<a class="headerlink" href="#a-coffee-example" title="Permalink to this headline">¶</a></h2>
+<p>Consider going down to the local coffee shop, <em>BeanMeUp</em>, for a coffee.  There
+are typically many different drinks on offer &#8211; espressos, lattes, teas, iced
+coffees, hot chocolate to name a few, as well as a number of extras (which cost
+extra too) such as whipped cream or an extra shot of espresso. You can also make
+certain changes to your drink at no extra cost, such as asking for decaf coffee
+instead of regular coffee.</p>
+<p>Quite clearly if we are going to model all these drinks and combinations, there
+will be sizeable class diagrams. So for clarity we will only consider a subset
+of the coffees: Espresso, Espresso Con Panna, Café Late, Cappuccino and Café
+Mocha. We&#8217;ll include 2 extras - whipped cream (&#8220;whipped&#8221;) and an extra shot of
+espresso; and three changes - decaf, steamed milk (&#8220;wet&#8221;) and foamed milk
+(&#8220;dry&#8221;).</p>
+</div>
+<div class="section" id="class-for-each-combination">
+<h2>Class for Each Combination<a class="headerlink" href="#class-for-each-combination" title="Permalink to this headline">¶</a></h2>
+<p>One solution is to create an individual class for every combination. Each class
+describes the drink and is responsible for the cost etc. The resulting menu is
+huge, and a part of the class diagram would look something like this:</p>
+<img alt="_images/coffeeExplosion.gif" src="_images/coffeeExplosion.gif" />
+<p>The key to using this method is to find the particular combination you want.
+So, once you&#8217;ve found the drink you would like, here is how you would use it, as
+shown in the <strong>CoffeeShop</strong> class in the following code:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Decorator/nodecorators/CoffeeShop.py</span>
+<span class="c"># Coffee example with no decorators</span>
+
+<span class="k">class</span> <span class="nc">Espresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">DoubleEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">EspressoConPanna</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">Cappuccino</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">cost</span> <span class="o">=</span> <span class="mf">1</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Cappucino&quot;</span>
+    <span class="k">def</span> <span class="nf">getCost</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost</span>
+    <span class="k">def</span> <span class="nf">getDescription</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
+
+<span class="k">class</span> <span class="nc">CappuccinoDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDecafWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDry</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDryWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">CafeMocha</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaDecafWhipped</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">cost</span> <span class="o">=</span> <span class="mf">1.25</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> \
+          <span class="s">&quot;Cafe Mocha decaf whipped cream&quot;</span>
+    <span class="k">def</span> <span class="nf">getCost</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost</span>
+    <span class="k">def</span> <span class="nf">getDescription</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
+
+<span class="k">class</span> <span class="nc">CafeMochaExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWet</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWetWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">CafeLatte</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteDecafWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWet</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWetWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="n">cappuccino</span> <span class="o">=</span> <span class="n">Cappuccino</span><span class="p">()</span>
+<span class="k">print</span><span class="p">((</span><span class="n">cappuccino</span><span class="o">.</span><span class="n">getDescription</span><span class="p">()</span> <span class="o">+</span> <span class="s">&quot;: $&quot;</span> <span class="o">+</span>
+  <span class="sb">`cappuccino.getCost()`</span><span class="p">))</span>
+
+<span class="n">cafeMocha</span> <span class="o">=</span> <span class="n">CafeMochaDecafWhipped</span><span class="p">()</span>
+<span class="k">print</span><span class="p">((</span><span class="n">cafeMocha</span><span class="o">.</span><span class="n">getDescription</span><span class="p">()</span>
+  <span class="o">+</span> <span class="s">&quot;: $&quot;</span> <span class="o">+</span> <span class="sb">`cafeMocha.getCost()`</span><span class="p">))</span>
+</pre></div>
+</div>
+<p>And here is the corresponding output:</p>
+<div class="highlight-python"><pre>Cappucino: $1.0
+Cafe Mocha decaf whipped cream: $1.25</pre>
+</div>
+<p>You can see that creating the particular combination you want is easy, since you
+are just creating an instance of a class. However, there are a number of
+problems with this approach. Firstly, the combinations are fixed statically so
+that any combination a customer may wish to order needs to be created up front.
+Secondly, the resulting menu is so huge that finding your particular combination
+is difficult and time consuming.</p>
+</div>
+<div class="section" id="the-decorator-approach">
+<h2>The Decorator Approach<a class="headerlink" href="#the-decorator-approach" title="Permalink to this headline">¶</a></h2>
+<p>Another approach would be to break the drinks down into the various components
+such as espresso and foamed milk, and then let the customer combine the
+components to describe a particular coffee.</p>
+<p>In order to do this programmatically, we use the Decorator pattern.  A Decorator
+adds responsibility to a component by wrapping it, but the Decorator conforms to
+the interface of the component it encloses, so the wrapping is transparent.
+Decorators can also be nested without the loss of this transparency.</p>
+<img alt="_images/decoratedCoffee.gif" src="_images/decoratedCoffee.gif" />
+<p>Methods invoked on the Decorator can in turn invoke methods in the component,
+and can of course perform processing before or after the invocation.</p>
+<p>So if we added <strong>getTotalCost()</strong> and <strong>getDescription()</strong> methods to the
+<strong>DrinkComponent</strong> interface, an Espresso looks like this:</p>
+<div class="highlight-python"><pre># Decorator/alldecorators/EspressoDecorator.py
+
+class Espresso(Decorator):
+    cost = 0.75f
+    description = " espresso"
+    def __init__(DrinkComponent):
+        Decorator.__init__(self, component)
+
+    def getTotalCost(self):
+        return self.component.getTotalCost() + cost
+
+    def getDescription(self):
+        return self.component.getDescription() +
+            description</pre>
+</div>
+<p>You combine the components to create a drink as follows, as shown in the code
+below:</p>
+<div class="highlight-python"><pre># Decorator/alldecorators/CoffeeShop.py
+# Coffee example using decorators
+
+class DrinkComponent:
+    def getDescription(self):
+        return self.__class__.__name__
+    def getTotalCost(self):
+        return self.__class__.cost
+
+class Mug(DrinkComponent):
+    cost = 0.0
+
+class Decorator(DrinkComponent):
+    def __init__(self, drinkComponent):
+        self.component = drinkComponent
+    def getTotalCost(self):
+        return self.component.getTotalCost() + \
+          DrinkComponent.getTotalCost(self)
+    def getDescription(self):
+        return self.component.getDescription() + \
+          ' ' + DrinkComponent.getDescription(self)
+
+class Espresso(Decorator):
+    cost = 0.75
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Decaf(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class FoamedMilk(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class SteamedMilk(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Whipped(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Chocolate(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+cappuccino = Espresso(FoamedMilk(Mug()))
+print(cappuccino.getDescription().strip() + \)
+  ": $" + `cappuccino.getTotalCost()`
+
+cafeMocha = Espresso(SteamedMilk(Chocolate(
+  Whipped(Decaf(Mug())))))
+
+print(cafeMocha.getDescription().strip() + \)
+  ": $" + `cafeMocha.getTotalCost()`</pre>
+</div>
+<p>This approach would certainly provide the most flexibility and the smallest
+menu. You have a small number of components to choose from, but assembling the
+description of the coffee then becomes rather arduous.</p>
+<p>If you want to describe a plain cappuccino, you create it with:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">plainCap</span> <span class="o">=</span> <span class="n">Espresso</span><span class="p">(</span><span class="n">FoamedMilk</span><span class="p">(</span><span class="n">Mug</span><span class="p">()))</span>
+</pre></div>
+</div>
+<p>Creating a decaf Cafe Mocha with whipped cream requires an even longer
+description.</p>
+</div>
+<div class="section" id="compromise">
+<h2>Compromise<a class="headerlink" href="#compromise" title="Permalink to this headline">¶</a></h2>
+<p>The previous approach takes too long to describe a coffee. There will also be
+certain combinations that you will describe regularly, and it would be
+convenient to have a quick way of describing them.</p>
+<p>The 3rd approach is a mixture of the first 2 approaches, and combines
+flexibility with ease of use. This compromise is achieved by creating a
+reasonably sized menu of basic selections, which would often work exactly as
+they are, but if you wanted to decorate them (whipped cream, decaf etc.) then
+you would use decorators to make the modifications. This is the type of menu you
+are presented with in most coffee shops.</p>
+<img alt="_images/compromiseDecoration.gif" src="_images/compromiseDecoration.gif" />
+<p>Here is how to create a basic selection, as well as a decorated selection:</p>
+<div class="highlight-python"><pre># Decorator/compromise/CoffeeShop.py
+# Coffee example with a compromise of basic
+# combinations and decorators
+
+class DrinkComponent:
+    def getDescription(self):
+        return self.__class__.__name__
+    def getTotalCost(self):
+        return self.__class__.cost
+
+class Espresso(DrinkComponent):
+    cost = 0.75
+
+class EspressoConPanna(DrinkComponent):
+    cost = 1.0
+
+class Cappuccino(DrinkComponent):
+    cost = 1.0
+
+class CafeLatte(DrinkComponent):
+    cost = 1.0
+
+class CafeMocha(DrinkComponent):
+    cost = 1.25
+
+class Decorator(DrinkComponent):
+    def __init__(self, drinkComponent):
+        self.component = drinkComponent
+    def getTotalCost(self):
+        return self.component.getTotalCost() + \
+          DrinkComponent.getTotalCost(self)
+    def getDescription(self):
+        return self.component.getDescription() + \
+          ' ' + DrinkComponent.getDescription(self)
+
+class ExtraEspresso(Decorator):
+    cost = 0.75
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Whipped(Decorator):
+    cost = 0.50
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Decaf(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Dry(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Wet(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+cappuccino = Cappuccino()
+print(cappuccino.getDescription() + ": $" + \)
+  `cappuccino.getTotalCost()`
+
+cafeMocha = Whipped(Decaf(CafeMocha()))
+print(cafeMocha.getDescription() + ": $" + \)
+  `cafeMocha.getTotalCost()`</pre>
+</div>
+<p>You can see that creating a basic selection is quick and easy, which makes sense
+since they will be described regularly.  Describing a decorated drink is more
+work than when using a class per combination, but clearly less work than when
+only using decorators.</p>
+<p>The final result is not too many classes, but not too many decorators either.
+Most of the time it&#8217;s possible to get away without using any decorators at all,
+so we have the benefits of both approaches.</p>
+</div>
+<div class="section" id="other-considerations">
+<h2>Other Considerations<a class="headerlink" href="#other-considerations" title="Permalink to this headline">¶</a></h2>
+<p>What happens if we decide to change the menu at a later stage, such as by adding
+a new type of drink? If we had used the class per combination approach, the
+effect of adding an extra such as syrup would be an exponential growth in the
+number of classes. However, the implications to the all decorator or compromise
+approaches are the same - one extra class is created.</p>
+<p>How about the effect of changing the cost of steamed milk and foamed milk, when
+the price of milk goes up? Having a class for each combination means that you
+need to change a method in each class, and thus maintain many classes. By using
+decorators, maintenance is reduced by defining the logic in one place.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic simple">
+<li>Add a Syrup class to the decorator approach described above. Then create a
+Café Latte (you&#8217;ll need to use steamed milk with an espresso) with syrup.</li>
+<li>Repeat Exercise 1 for the compromise approach.</li>
+<li>Implement the decorator pattern to create a Pizza restaurant, which has a
+set menu of choices as well as the option to design your own pizza.  Follow
+the compromise approach to create a menu consisting of a Margherita,
+Hawaiian, Regina, and Vegetarian pizzas, with toppings (decorators) of
+Garlic, Olives, Spinach, Avocado, Feta and Pepperdews. Create a Hawaiian
+pizza, as well as a Margherita decorated with Spinach, Feta, Pepperdews and
+Olives.</li>
+</ol>
+</div>
+</div>
+
+
+          </div>
+        </div>
+      </div>
+      <div class="sphinxsidebar">
+        <div class="sphinxsidebarwrapper">
+            <p class="logo"><a href="index.html">
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
+            </a></p>
+    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
+    
+            <h3><a href="index.html">Table Of Contents</a></h3>
+            <ul>
+<li><a class="reference external" href="">Decorator: Dynamic Type Selection</a><ul>
+<li><a class="reference external" href="#basic-decorator-structure">Basic Decorator Structure</a></li>
+<li><a class="reference external" href="#a-coffee-example">A Coffee Example</a></li>
+<li><a class="reference external" href="#class-for-each-combination">Class for Each Combination</a></li>
+<li><a class="reference external" href="#the-decorator-approach">The Decorator Approach</a></li>
+<li><a class="reference external" href="#compromise">Compromise</a></li>
+<li><a class="reference external" href="#other-considerations">Other Considerations</a></li>
+<li><a class="reference external" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+            <h4>Previous topic</h4>
+            <p class="topless"><a href="StateMachine.html" title="previous chapter">StateMachine</a></p>
+            <h4>Next topic</h4>
+            <p class="topless"><a href="Iterators.html" title="next chapter">Iterators: Decoupling Algorithms from Containers</a></p>
+            <h3>This Page</h3>
+            <ul class="this-page-menu">
+              <li><a href="_sources/Decorator.txt">Show Source</a></li>
+            </ul>
+    
+          <h3>Quick search</h3>
+            <form class="search" action="search.html" method="get">
+              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
+              <input type="hidden" name="check_keywords" value="yes" />
+              <input type="hidden" name="area" value="default" />
+            </form>
+    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
+    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
+    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>
+
+        </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="Iterators.html" title="Iterators: Decoupling Algorithms from Containers"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="StateMachine.html" title="StateMachine"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>
+    <div class="footer">
+      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
+    </div>
+  </body>
+</html>

html/DeveloperGuide.html

   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
-    <title>Developer Guide &mdash; Python 3 Patterns &amp; Idioms</title>
+    <title>Developer Guide &mdash; Python 3 Patterns, Recipes and Idioms</title>
     <link rel="stylesheet" href="_static/default.css" type="text/css" />
     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
     <script type="text/javascript">
     <link rel="shortcut icon" href="_static/favicon.ico"/>
     <link rel="index" title="Index" href="genindex.html" />
     <link rel="search" title="Search" href="search.html" />
-    <link rel="top" title="Python 3 Patterns &amp; Idioms" href="index.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
     <link rel="next" title="Part I: Foundations" href="Part1.html" />
     <link rel="prev" title="Book Development Rules" href="Rules.html" /> 
   </head>
         <li class="right" >
           <a href="Rules.html" title="Book Development Rules"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>  
     <div class="document">
 changed files - so you can comment individually.</li>
 </ol>
 </div>
+<div class="section" id="emacs-for-editing-restructured-text">
+<h2>Emacs for Editing Restructured Text<a class="headerlink" href="#emacs-for-editing-restructured-text" title="Permalink to this headline">¶</a></h2>
+<p>If you want an editing system with support for restructured text, one choice is
+the free text editor <em>emacs</em>, which has an add-on mode for restructured text.
+Emacs has a long and venerable history, and is an extremely powerful editor.
+Emacs also has versions that are customized for operating systems to make it
+much more familiar.</p>
+<p>Here&#8217;s a <a class="reference external" href="http://lowfatlinux.com/linux-editor-emacs.html">simple introduction to emacs</a>
+and a <a class="reference external" href="http://www.linuxhelp.net/guides/emacs/">useful introductory help guide</a>.
+For Windows, there&#8217;s <a class="reference external" href="http://www.gnu.org/software/emacs/windows/ntemacs.html">a special FAQ</a>.</p>
+<p><strong>Mac OSX</strong>: <a class="reference external" href="http://aquamacs.org/">Aquamacs</a> looks and feels like a native
+Mac application.</p>
+<p><strong>Windows</strong>: You can download the latest windows installer <a class="reference external" href="http://ftp.gnu.org/pub/gnu/emacs/windows/">here (choose the
+highest numbered zip file with &#8220;bin&#8221; in the name)</a>.
+<a class="reference external" href="http://www.arunrocks.com/blog/archives/2008/02/20/5-indespensible-tips-for-emacs-on-windows/">This blog</a>
+gives useful tips to make emacs on Windows even friendlier (in particular, it
+puts emacs on the right-click menu and improves the startup settings).</p>
+<p><strong>Linux</strong>: It&#8217;s virtually guaranteed that you already have emacs preinstalled
+on your Linux distribution, which you can start from a command prompt. However,
+there may also be more &#8220;windowy&#8221; versions that you can install separately.</p>
+<div class="admonition-todo admonition " id="todo-7">
+<p class="first admonition-title">Todo</p>
+<p class="last">Someone who knows more about emacs for Linux please add more specific information the windowed version(s).</p>
+</div>
+<p>Finally, <a class="reference external" href="http://docutils.sourceforge.net/docs/user/emacs.html">here&#8217;s the documentation for installing and using the emacs
+restructured-text mode</a>.
+The elisp code it refers to is in the file &#8216;rst.el &lt;<a class="reference external" href="http://docutils.sourceforge.net/tools/editors/emacs/rst.el">http://docutils.sourceforge.net/tools/editors/emacs/rst.el</a>&gt;`_.</p>
+<p>To customize your emacs, you need to open the <tt class="docutils literal"><span class="pre">.emacs</span></tt> file. The above Windows
+FAQ tells you how to put your <tt class="docutils literal"><span class="pre">.emacs</span></tt> file somewhere else, but the easiest
+thing to do is just open emacs and inside it type <tt class="docutils literal"><span class="pre">C-x</span> <span class="pre">C-f</span> <span class="pre">~/.emacs</span></tt>, which
+will open your default <tt class="docutils literal"><span class="pre">.emacs</span></tt> file.</p>
+</div>
 </div>
 
 
       <div class="sphinxsidebar">
         <div class="sphinxsidebarwrapper">
             <p class="logo"><a href="index.html">
-              <img class="logo" src="_static/Logo.png" alt="Logo"/>
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
             </a></p>
     <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
     
 <li><a class="reference external" href="#building-the-pdf">Building the PDF</a></li>
 <li><a class="reference external" href="#working-with-bitbucket-and-mercurial">Working with BitBucket and Mercurial</a></li>
 <li><a class="reference external" href="#a-simple-overview-of-editing-and-merging">A Simple Overview Of Editing and Merging</a></li>
+<li><a class="reference external" href="#emacs-for-editing-restructured-text">Emacs for Editing Restructured Text</a></li>
 </ul>
 </li>
 </ul>
         <li class="right" >
           <a href="Rules.html" title="Book Development Rules"
              accesskey="P">previous</a> |</li>
-        <li><a href="index.html">Python 3 Patterns &amp; Idioms</a> &raquo;</li> 
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
       </ul>
     </div>
     <div class="footer">
       &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
-      Last updated on Nov 25, 2008.
-      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
     </div>
   </body>
 </html>

html/Factory.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Factory: Encapsulating Object Creation &mdash; Python 3 Patterns, Recipes and Idioms</title>
+    <link rel="stylesheet" href="_static/default.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+          URL_ROOT:    '',
+          VERSION:     '1.0',
+          COLLAPSE_MODINDEX: false,
+          FILE_SUFFIX: '.html'
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="index" title="Index" href="genindex.html" />
+    <link rel="search" title="Search" href="search.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Function Objects" href="FunctionObjects.html" />
+    <link rel="prev" title="Iterators: Decoupling Algorithms from Containers" href="Iterators.html" /> 
+  </head>
+  <body>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Iterators.html" title="Iterators: Decoupling Algorithms from Containers"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>  
+    <div class="document">
+      <div class="documentwrapper">
+        <div class="bodywrapper">
+          <div class="body">
+            
+  
+  <div class="section" id="factory-encapsulating-object-creation">
+<h1>Factory: Encapsulating Object Creation<a class="headerlink" href="#factory-encapsulating-object-creation" title="Permalink to this headline">¶</a></h1>
+<p>When you discover that you need to add new types to a system, the most sensible
+first step is to use polymorphism to create a common interface to those new
+types. This separates the rest of the code in your system from the knowledge of
+the specific types that you are adding. New types may be added without
+disturbing existing code ... or so it seems. At first it would appear that the
+only place you need to change the code in such a design is the place where you
+inherit a new type, but this is not quite true. You must still create an object
+of your new type, and at the point of creation you must specify the exact
+constructor to use. Thus, if the code that creates objects is distributed
+throughout your application, you have the same problem when adding new types-you
+must still chase down all the points of your code where type matters. It happens
+to be the <em>creation</em> of the type that matters in this case rather than the <em>use</em>
+of the type (which is taken care of by polymorphism), but the effect is the
+same: adding a new type can cause problems.</p>
+<p>The solution is to force the creation of objects to occur through a common
+<em>factory</em> rather than to allow the creational code to be spread throughout your
+system. If all the code in your program must go through this factory whenever it
+needs to create one of your objects, then all you must do when you add a new
+object is to modify the factory.</p>
+<p>Since every object-oriented program creates objects, and since it&#8217;s very likely
+you will extend your program by adding new types, I suspect that factories may
+be the most universally useful kinds of design patterns.</p>
+<div class="section" id="simple-factory-method">
+<h2>Simple Factory Method<a class="headerlink" href="#simple-factory-method" title="Permalink to this headline">¶</a></h2>
+<p>As an example, let&#8217;s revisit the <strong>Shape</strong> system.</p>
+<p>One approach is to make the factory a <strong>static</strong> method of the base class:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Factory/shapefact1/ShapeFactory1.py</span>
+<span class="c"># A simple static factory method.</span>
+<span class="k">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">generators</span>
+<span class="k">import</span> <span class="nn">random</span>
+
+<span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
+    <span class="c"># Create based on class name:</span>
+    <span class="k">def</span> <span class="nf">factory</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
+        <span class="c">#return eval(type + &quot;()&quot;)</span>
+        <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s">&quot;Circle&quot;</span><span class="p">:</span> <span class="k">return</span> <span class="n">Circle</span><span class="p">()</span>
+        <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s">&quot;Square&quot;</span><span class="p">:</span> <span class="k">return</span> <span class="n">Square</span><span class="p">()</span>
+        <span class="k">assert</span> <span class="mf">0</span><span class="p">,</span> <span class="s">&quot;Bad shape creation: &quot;</span> <span class="o">+</span> <span class="nb">type</span>
+    <span class="n">factory</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">factory</span><span class="p">)</span>
+
+<span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.draw&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.erase&quot;</span><span class="p">)</span>
+
+<span class="k">class</span> <span class="nc">Square</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.draw&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.erase&quot;</span><span class="p">)</span>
+
+<span class="c"># Generate shape name strings:</span>
+<span class="k">def</span> <span class="nf">shapeNameGen</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+    <span class="n">types</span> <span class="o">=</span> <span class="n">Shape</span><span class="o">.</span><span class="n">__subclasses__</span><span class="p">()</span>
+    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+        <span class="k">yield</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span>
+
+<span class="n">shapes</span> <span class="o">=</span> \
+  <span class="p">[</span> <span class="n">Shape</span><span class="o">.</span><span class="n">factory</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">shapeNameGen</span><span class="p">(</span><span class="mf">7</span><span class="p">)]</span>
+
+<span class="k">for</span> <span class="n">shape</span> <span class="ow">in</span> <span class="n">shapes</span><span class="p">:</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">erase</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>The <strong>factory( )</strong> takes an argument that allows it to determine what type of
+<strong>Shape</strong> to create; it happens to be a <strong>String</strong> in this case but it could be
+any set of data. The <strong>factory( )</strong> is now the only other code in the system
+that needs to be changed when a new type of <strong>Shape</strong> is added (the
+initialization data for the objects will presumably come from somewhere outside
+the system, and not be a hard-coded array as in the above example).</p>
+<p>Note that this example also shows the new Python 2.2 <strong>staticmethod( )</strong>
+technique for creating static methods in a class.</p>
+<p>I have also used a tool which is new in Python 2.2 called a <em>generator</em>. A
+generator is a special case of a factory: it&#8217;s a factory that takes no arguments
+in order to create a new object. Normally you hand some information to a factory
+in order to tell it what kind of object to create and how to create it, but a
+generator has some kind of internal algorithm that tells it what and how to
+build. It &#8220;generates out of thin air&#8221; rather than being told what to create.</p>
+<p>Now, this may not look consistent with the code you see above:</p>
+<div class="highlight-python"><pre>for i in shapeNameGen(7)</pre>
+</div>
+<p>looks like there&#8217;s an initialization taking place. This is where a generator is
+a bit strange - when you call a function that contains a <strong>yield</strong> statement
+(<strong>yield</strong> is a new keyword that determines that a function is a generator),
+that function actually returns a generator object that has an iterator. This
+iterator is implicitly used in the <strong>for</strong> statement above, so it appears that
+you are iterating through the generator function, not what it returns. This was
+done for convenience of use.</p>
+<p>Thus, the code that you write is actually a kind of factory, that creates the
+generator objects that do the actual generation. You can use the generator
+explicitly if you want, for example:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">gen</span> <span class="o">=</span> <span class="n">shapeNameGen</span><span class="p">(</span><span class="mf">7</span><span class="p">)</span>
+<span class="k">print</span><span class="p">(</span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">())</span>
+</pre></div>
+</div>
+<p>So <strong>next( )</strong> is the iterator method that&#8217;s actually called to generate the
+next object, and it takes no arguments. <strong>shapeNameGen( )</strong> is the factory, and
+<strong>gen</strong> is the generator.</p>
+<p>Inside the generator-factory, you can see the call to <strong>__subclasses__( )</strong>,
+which produces a list of references to each of the subclasses of <strong>Shape</strong>
+(which must be inherited from <strong>object</strong> for this to work). You should be aware,
+however, that this only works for the first level of inheritance from <strong>Item</strong>,
+so if you were to inherit a new class from <strong>Circle</strong>, it wouldn&#8217;t show up in
+the list generated by <strong>__subclasses__( )</strong>. If you need to create a deeper
+hierarchy this way, you must recurse the <strong>__subclasses__( )</strong> list.</p>
+<p>Also note that in <strong>shapeNameGen( )</strong> the statement:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">types</span> <span class="o">=</span> <span class="n">Shape</span><span class="o">.</span><span class="n">__subclasses__</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>Is only executed when the generator object is produced; each time the <strong>next(
+)</strong> method of this generator object is called (which, as noted above, may happen
+implicitly), only the code in the <strong>for</strong> loop will be executed, so you don&#8217;t
+have wasteful execution (as you would if this were an ordinary function).</p>
+<div class="section" id="preventing-direct-creation">
+<h3>Preventing direct creation<a class="headerlink" href="#preventing-direct-creation" title="Permalink to this headline">¶</a></h3>
+<p>To disallow direct access to the classes, you can nest the classes within the
+factory method, like this:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Factory/shapefact1/NestedShapeFactory.py</span>
+<span class="k">import</span> <span class="nn">random</span>
+
+<span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
+    <span class="n">types</span> <span class="o">=</span> <span class="p">[]</span>
+
+<span class="k">def</span> <span class="nf">factory</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
+    <span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+        <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.draw&quot;</span><span class="p">)</span>
+        <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.erase&quot;</span><span class="p">)</span>
+
+    <span class="k">class</span> <span class="nc">Square</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+        <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.draw&quot;</span><span class="p">)</span>
+        <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.erase&quot;</span><span class="p">)</span>
+
+    <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s">&quot;Circle&quot;</span><span class="p">:</span> <span class="k">return</span> <span class="n">Circle</span><span class="p">()</span>
+    <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s">&quot;Square&quot;</span><span class="p">:</span> <span class="k">return</span> <span class="n">Square</span><span class="p">()</span>
+    <span class="k">assert</span> <span class="mf">0</span><span class="p">,</span> <span class="s">&quot;Bad shape creation: &quot;</span> <span class="o">+</span> <span class="nb">type</span>
+
+<span class="k">def</span> <span class="nf">shapeNameGen</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+        <span class="k">yield</span> <span class="n">factory</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="s">&quot;Circle&quot;</span><span class="p">,</span> <span class="s">&quot;Square&quot;</span><span class="p">]))</span>
+
+<span class="c"># Circle() # Not defined</span>
+
+<span class="k">for</span> <span class="n">shape</span> <span class="ow">in</span> <span class="n">shapeNameGen</span><span class="p">(</span><span class="mf">7</span><span class="p">):</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">erase</span><span class="p">()</span>
+</pre></div>
+</div>
+</div>
+</div>
+<div class="section" id="polymorphic-factories">
+<h2>Polymorphic Factories<a class="headerlink" href="#polymorphic-factories" title="Permalink to this headline">¶</a></h2>
+<p>The static <strong>factory( )</strong> method in the previous example forces all the creation
+operations to be focused in one spot, so that&#8217;s the only place you need to
+change the code. This is certainly a reasonable solution, as it throws a box
+around the process of creating objects. However, the <em>Design Patterns</em> book
+emphasizes that the reason for the <em>Factory Method</em> pattern is so that different
+types of factories can be subclassed from the basic factory (the above design is
+mentioned as a special case). However, the book does not provide an example, but
+instead just repeats the example used for the <em>Abstract Factory</em> (you&#8217;ll see an
+example of this in the next section). Here is <strong>ShapeFactory1.py</strong> modified so
+the factory methods are in a separate class as virtual functions. Notice also
+that the specific <strong>Shape</strong> classes are dynamically loaded on demand:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Factory/shapefact2/ShapeFactory2.py</span>
+<span class="c"># Polymorphic factory methods.</span>
+<span class="k">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">generators</span>
+<span class="k">import</span> <span class="nn">random</span>
+
+<span class="k">class</span> <span class="nc">ShapeFactory</span><span class="p">:</span>
+    <span class="n">factories</span> <span class="o">=</span> <span class="p">{}</span>
+    <span class="k">def</span> <span class="nf">addFactory</span><span class="p">(</span><span class="nb">id</span><span class="p">,</span> <span class="n">shapeFactory</span><span class="p">):</span>
+        <span class="n">ShapeFactory</span><span class="o">.</span><span class="n">factories</span><span class="o">.</span><span class="n">put</span><span class="p">[</span><span class="nb">id</span><span class="p">]</span> <span class="o">=</span> <span class="n">shapeFactory</span>
+    <span class="n">addFactory</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">addFactory</span><span class="p">)</span>
+    <span class="c"># A Template Method:</span>
+    <span class="k">def</span> <span class="nf">createShape</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
+        <span class="k">if</span> <span class="ow">not</span> <span class="n">ShapeFactory</span><span class="o">.</span><span class="n">factories</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
+            <span class="n">ShapeFactory</span><span class="o">.</span><span class="n">factories</span><span class="p">[</span><span class="nb">id</span><span class="p">]</span> <span class="o">=</span> \
+              <span class="nb">eval</span><span class="p">(</span><span class="nb">id</span> <span class="o">+</span> <span class="s">&#39;.Factory()&#39;</span><span class="p">)</span>
+        <span class="k">return</span> <span class="n">ShapeFactory</span><span class="o">.</span><span class="n">factories</span><span class="p">[</span><span class="nb">id</span><span class="p">]</span><span class="o">.</span><span class="n">create</span><span class="p">()</span>
+    <span class="n">createShape</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">createShape</span><span class="p">)</span>
+
+<span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.draw&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Circle.erase&quot;</span><span class="p">)</span>
+    <span class="k">class</span> <span class="nc">Factory</span><span class="p">:</span>
+        <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Circle</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">Square</span><span class="p">(</span><span class="n">Shape</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.draw&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">erase</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Square.erase&quot;</span><span class="p">)</span>
+    <span class="k">class</span> <span class="nc">Factory</span><span class="p">:</span>
+        <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Square</span><span class="p">()</span>
+
+<span class="k">def</span> <span class="nf">shapeNameGen</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+    <span class="n">types</span> <span class="o">=</span> <span class="n">Shape</span><span class="o">.</span><span class="n">__subclasses__</span><span class="p">()</span>
+    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
+        <span class="k">yield</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span>
+
+<span class="n">shapes</span> <span class="o">=</span> <span class="p">[</span> <span class="n">ShapeFactory</span><span class="o">.</span><span class="n">createShape</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
+           <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">shapeNameGen</span><span class="p">(</span><span class="mf">7</span><span class="p">)]</span>
+
+<span class="k">for</span> <span class="n">shape</span> <span class="ow">in</span> <span class="n">shapes</span><span class="p">:</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
+    <span class="n">shape</span><span class="o">.</span><span class="n">erase</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>Now the factory method appears in its own class, <strong>ShapeFactory</strong>, as the
+<strong>create( )</strong> method. The different types of shapes must each create their own
+factory with a <strong>create( )</strong> method to create an object of their own type. The
+actual creation of shapes is performed by calling <strong>ShapeFactory.createShape(
+)</strong>, which is a static method that uses the dictionary in <strong>ShapeFactory</strong> to
+find the appropriate factory object based on an identifier that you pass it. The
+factory is immediately used to create the shape object, but you could imagine a
+more complex problem where the appropriate factory object is returned and then
+used by the caller to create an object in a more sophisticated way. However, it
+seems that much of the time you don&#8217;t need the intricacies of the polymorphic
+factory method, and a single static method in the base class (as shown in
+<strong>ShapeFactory1.py</strong>) will work fine.</p>
+<p>Notice that the <strong>ShapeFactory</strong> must be initialized by loading its dictionary
+with factory objects, which takes place in the static initialization clause of
+each of the shape implementations.</p>
+</div>
+<div class="section" id="abstract-factories">
+<h2>Abstract Factories<a class="headerlink" href="#abstract-factories" title="Permalink to this headline">¶</a></h2>
+<p>The <em>Abstract Factory</em> pattern looks like the factory objects we&#8217;ve seen
+previously, with not one but several factory methods. Each of the factory
+methods creates a different kind of object. The idea is that at the point of
+creation of the factory object, you decide how all the objects created by that
+factory will be used. The example given in <em>Design Patterns</em> implements
+portability across various graphical user interfaces (GUIs): you create a
+factory object appropriate to the GUI that you&#8217;re working with, and from then on
+when you ask it for a menu, button, slider, etc. it will automatically create
+the appropriate version of that item for the GUI. Thus you&#8217;re able to isolate,
+in one place, the effect of changing from one GUI to another.</p>
+<p>As another example suppose you are creating a general-purpose gaming environment
+and you want to be able to support different types of games. Here&#8217;s how it might
+look using an abstract factory:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Factory/Games.py</span>
+<span class="c"># An example of the Abstract Factory pattern.</span>
+
+<span class="k">class</span> <span class="nc">Obstacle</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">action</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">Character</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obstacle</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">Kitty</span><span class="p">(</span><span class="n">Character</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obstacle</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Kitty has encountered a&quot;</span><span class="p">,</span>
+        <span class="n">obstacle</span><span class="o">.</span><span class="n">action</span><span class="p">())</span>
+
+<span class="k">class</span> <span class="nc">KungFuGuy</span><span class="p">(</span><span class="n">Character</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obstacle</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;KungFuGuy now battles a&quot;</span><span class="p">,</span>
+        <span class="n">obstacle</span><span class="o">.</span><span class="n">action</span><span class="p">())</span>
+
+<span class="k">class</span> <span class="nc">Puzzle</span><span class="p">(</span><span class="n">Obstacle</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">action</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Puzzle&quot;</span><span class="p">)</span>
+
+<span class="k">class</span> <span class="nc">NastyWeapon</span><span class="p">(</span><span class="n">Obstacle</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">action</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;NastyWeapon&quot;</span><span class="p">)</span>
+
+<span class="c"># The Abstract Factory:</span>
+<span class="k">class</span> <span class="nc">GameElementFactory</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">makeCharacter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+    <span class="k">def</span> <span class="nf">makeObstacle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="c"># Concrete factories:</span>
+<span class="k">class</span> <span class="nc">KittiesAndPuzzles</span><span class="p">(</span><span class="n">GameElementFactory</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">makeCharacter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Kitty</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">makeObstacle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Puzzle</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">KillAndDismember</span><span class="p">(</span><span class="n">GameElementFactory</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">makeCharacter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">KungFuGuy</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">makeObstacle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">NastyWeapon</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">GameEnvironment</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">factory</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">factory</span> <span class="o">=</span> <span class="n">factory</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="n">makeCharacter</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">ob</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="n">makeObstacle</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">play</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="o">.</span><span class="n">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ob</span><span class="p">)</span>
+
+<span class="n">g1</span> <span class="o">=</span> <span class="n">GameEnvironment</span><span class="p">(</span><span class="n">KittiesAndPuzzles</span><span class="p">())</span>
+<span class="n">g2</span> <span class="o">=</span> <span class="n">GameEnvironment</span><span class="p">(</span><span class="n">KillAndDismember</span><span class="p">())</span>
+<span class="n">g1</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>
+<span class="n">g2</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>In this environment, <strong>Character</strong> objects interact with <strong>Obstacle</strong> objects,
+but there are different types of Characters and obstacles depending on what kind
+of game you&#8217;re playing. You determine the kind of game by choosing a particular
+<strong>GameElementFactory</strong>, and then the <strong>GameEnvironment</strong> controls the setup and
+play of the game. In this example, the setup and play is very simple, but those
+activities (the <em>initial conditions</em> and the <em>state change</em>) can determine much
+of the game&#8217;s outcome. Here, <strong>GameEnvironment</strong> is not designed to be
+inherited, although it could very possibly make sense to do that.</p>
+<p>This also contains examples of <em>Double Dispatching</em> and the <em>Factory Method</em>,
+both of which will be explained later.</p>
+<p>Of course, the above scaffolding of <strong>Obstacle</strong>, <strong>Character</strong> and
+<strong>GameElementFactory</strong> (which was translated from the Java version of this
+example) is unnecessary - it&#8217;s only required for languages that have static type
+checking. As long as the concrete Python classes follow the form of the required
+classes, we don&#8217;t need any base classes:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Factory/Games2.py</span>
+<span class="c"># Simplified Abstract Factory.</span>
+
+<span class="k">class</span> <span class="nc">Kitty</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obstacle</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Kitty has encountered a&quot;</span><span class="p">,</span>
+        <span class="n">obstacle</span><span class="o">.</span><span class="n">action</span><span class="p">())</span>
+
+<span class="k">class</span> <span class="nc">KungFuGuy</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obstacle</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;KungFuGuy now battles a&quot;</span><span class="p">,</span>
+        <span class="n">obstacle</span><span class="o">.</span><span class="n">action</span><span class="p">())</span>
+
+<span class="k">class</span> <span class="nc">Puzzle</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">action</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;Puzzle&quot;</span><span class="p">)</span>
+
+<span class="k">class</span> <span class="nc">NastyWeapon</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">action</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s">&quot;NastyWeapon&quot;</span><span class="p">)</span>
+
+<span class="c"># Concrete factories:</span>
+<span class="k">class</span> <span class="nc">KittiesAndPuzzles</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">makeCharacter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Kitty</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">makeObstacle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">Puzzle</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">KillAndDismember</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">makeCharacter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">KungFuGuy</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">makeObstacle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">NastyWeapon</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">GameEnvironment</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">factory</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">factory</span> <span class="o">=</span> <span class="n">factory</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="n">makeCharacter</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">ob</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="n">makeObstacle</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">play</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="o">.</span><span class="n">interactWith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ob</span><span class="p">)</span>
+
+<span class="n">g1</span> <span class="o">=</span> <span class="n">GameEnvironment</span><span class="p">(</span><span class="n">KittiesAndPuzzles</span><span class="p">())</span>
+<span class="n">g2</span> <span class="o">=</span> <span class="n">GameEnvironment</span><span class="p">(</span><span class="n">KillAndDismember</span><span class="p">())</span>
+<span class="n">g1</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>
+<span class="n">g2</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>Another way to put this is that all inheritance in Python is implementation
+inheritance; since Python does its type-checking at runtime, there&#8217;s no need to
+use interface inheritance so that you can upcast to the base type.</p>
+<p>You might want to study the two examples for comparison, however. Does the first
+one add enough useful information about the pattern that it&#8217;s worth keeping some
+aspect of it? Perhaps all you need is &#8220;tagging classes&#8221; like this:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Obstacle</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">Character</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">GameElementFactory</span><span class="p">:</span> <span class="k">pass</span>
+</pre></div>
+</div>
+<p>Then the inheritance serves only to indicate the type of the derived classes.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic simple">
+<li>Add a class <strong>Triangle</strong> to <strong>ShapeFactory1.py</strong></li>
+<li>Add a class <strong>Triangle</strong> to <strong>ShapeFactory2.py</strong></li>
+<li>Add a new type of <strong>GameEnvironment</strong> called <strong>GnomesAndFairies</strong> to
+<strong>GameEnvironment.py</strong></li>
+<li>Modify <strong>ShapeFactory2.py</strong> so that it uses an <em>Abstract Factory</em> to create
+different sets of shapes (for example, one particular type of factory object
+creates &#8220;thick shapes,&#8221; another creates &#8220;thin shapes,&#8221; but each factory
+object can create all the shapes: circles, squares, triangles etc.).</li>
+</ol>
+</div>
+</div>
+
+
+          </div>
+        </div>
+      </div>
+      <div class="sphinxsidebar">
+        <div class="sphinxsidebarwrapper">
+            <p class="logo"><a href="index.html">
+              <img class="logo" src="_static/cover.png" alt="Logo"/>
+            </a></p>
+    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
+    
+            <h3><a href="index.html">Table Of Contents</a></h3>
+            <ul>
+<li><a class="reference external" href="">Factory: Encapsulating Object Creation</a><ul>
+<li><a class="reference external" href="#simple-factory-method">Simple Factory Method</a><ul>
+<li><a class="reference external" href="#preventing-direct-creation">Preventing direct creation</a></li>
+</ul>
+</li>
+<li><a class="reference external" href="#polymorphic-factories">Polymorphic Factories</a></li>
+<li><a class="reference external" href="#abstract-factories">Abstract Factories</a></li>
+<li><a class="reference external" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+            <h4>Previous topic</h4>
+            <p class="topless"><a href="Iterators.html" title="previous chapter">Iterators: Decoupling Algorithms from Containers</a></p>
+            <h4>Next topic</h4>
+            <p class="topless"><a href="FunctionObjects.html" title="next chapter">Function Objects</a></p>
+            <h3>This Page</h3>
+            <ul class="this-page-menu">
+              <li><a href="_sources/Factory.txt">Show Source</a></li>
+            </ul>
+    
+          <h3>Quick search</h3>
+            <form class="search" action="search.html" method="get">
+              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
+              <input type="hidden" name="check_keywords" value="yes" />
+              <input type="hidden" name="area" value="default" />
+            </form>
+    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
+    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
+    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>
+
+        </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Iterators.html" title="Iterators: Decoupling Algorithms from Containers"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>
+    <div class="footer">
+      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
+      Last updated on Dec 06, 2008.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
+    </div>
+  </body>
+</html>

html/Fronting.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Fronting for an Implementation &mdash; Python 3 Patterns, Recipes and Idioms</title>
+    <link rel="stylesheet" href="_static/default.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+          URL_ROOT:    '',
+          VERSION:     '1.0',
+          COLLAPSE_MODINDEX: false,
+          FILE_SUFFIX: '.html'
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="index" title="Index" href="genindex.html" />
+    <link rel="search" title="Search" href="search.html" />
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="StateMachine" href="StateMachine.html" />
+    <link rel="prev" title="Building Application Frameworks" href="AppFrameworks.html" /> 
+  </head>
+  <body>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="StateMachine.html" title="StateMachine"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="AppFrameworks.html" title="Building Application Frameworks"
+             accesskey="P">previous</a> |</li>
+        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
+      </ul>
+    </div>  
+    <div class="document">
+      <div class="documentwrapper">
+        <div class="bodywrapper">
+          <div class="body">
+            
+  
+  <div class="section" id="fronting-for-an-implementation">
+<h1>Fronting for an Implementation<a class="headerlink" href="#fronting-for-an-implementation" title="Permalink to this headline">¶</a></h1>
+<p>Both <em>Proxy</em> and <em>State</em> provide a surrogate class that you use in your code;
+the real class that does the work is hidden behind this surrogate class. When
+you call a method in the surrogate, it simply turns around and calls the method
+in the implementing class. These two patterns are so similar that the <em>Proxy</em> is
+simply a special case of <em>State</em>. One is tempted to just lump the two together
+into a pattern called <em>Surrogate</em>, but the term &#8220;proxy&#8221; has a long-standing and
+specialized meaning, which probably explains the reason for the two different
+patterns.</p>
+<p>The basic idea is simple: from a base class, the surrogate is derived along with
+the class or classes that provide the actual implementation:</p>
+<img alt="_images/surrogate.gif" src="_images/surrogate.gif" />
+<p>When a surrogate object is created, it is given an implementation to which to
+send all of the method calls.</p>
+<p>Structurally, the difference between <em>Proxy</em> and <em>State</em> is simple: a <em>Proxy</em>
+has only one implementation, while <em>State</em> has more than one. The application of
+the patterns is considered (in <em>Design Patterns</em>) to be distinct: <em>Proxy</em> is
+used to control access to its implementation, while <em>State</em> allows you to change
+the implementation dynamically. However, if you expand your notion of
+&#8220;controlling access to implementation&#8221; then the two fit neatly together.</p>
+<div class="section" id="proxy">
+<h2>Proxy<a class="headerlink" href="#proxy" title="Permalink to this headline">¶</a></h2>
+<p>If we implement <em>Proxy</em> by following the above diagram, it looks like this:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Fronting/ProxyDemo.py</span>
+<span class="c"># Simple demonstration of the Proxy pattern.</span>
+
+<span class="k">class</span> <span class="nc">Implementation</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Implementation.f()&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Implementation.g()&quot;</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">h</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Implementation.h()&quot;</span><span class="p">)</span>
+