formations/python/python3/fr/_build/html/DesignPatterns.html

190 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Les design patterns &#8212; Documentation Formation Python 1</title>
<link rel="stylesheet" href="_static/classic.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',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/translations.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Recherche" href="search.html" />
<link rel="next" title="La librairie standard" href="stdlib.html" />
<link rel="prev" title="Le style de programmation par exceptions" href="exceptions.html" />
</head>
<body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<table><tr><td>
<img src="_static/sphinx.png" alt="logo" />
</td><td>
<h1>&nbsp; &nbsp; Programmation python</h1>
</td></tr></table>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="stdlib.html" title="La librairie standard"
accesskey="N">suivant</a> |</li>
<li class="right" >
<a href="exceptions.html" title="Le style de programmation par exceptions"
accesskey="P">précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<p>&nbsp;</p>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="les-design-patterns">
<h1>Les design patterns<a class="headerlink" href="#les-design-patterns" title="Lien permanent vers ce titre"></a></h1>
<p>Les design patterns <strong>ne sont pas</strong> indépendants du langage.
Ils dépendent de limplémentation.</p>
<div class="section" id="le-duck-typing">
<h2>Le duck typing<a class="headerlink" href="#le-duck-typing" title="Lien permanent vers ce titre"></a></h2>
<p>En python, le duck typing est une forme extreme de programmation par interface.
Ne pas hériter dans des directions fausse</p>
<p>exemple : une voiture ne peut hériter dun moteur, parce que un moteur nest pas une voiture.</p>
</div>
<div class="section" id="hold-or-wrap">
<h2>hold or wrap ?<a class="headerlink" href="#hold-or-wrap" title="Lien permanent vers ce titre"></a></h2>
<p><strong>hold</strong>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">O</span><span class="o">.</span><span class="n">S</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
</pre></div>
</div>
<p>Cela induit un couplage fort (cf la loi de Demeter)</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">law of Demeter : never more than one dot.</p>
</div>
<p>wrap : a hold by private name, with a:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">S</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
</pre></div>
</div>
<p>Ou bien une méthode getattr:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="fm">__getattr__</span><span class="p">()</span>
</pre></div>
</div>
<p>Gets coupling right.</p>
</div>
<div class="section" id="wrapper-can-restrict-inheritance-cannot-restrict">
<h2>wrapper can restrict, inheritance cannot restrict<a class="headerlink" href="#wrapper-can-restrict-inheritance-cannot-restrict" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">RestrictingWrapper</span><span class="p">(</span><span class="nb">object</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">w</span><span class="p">,</span> <span class="n">block</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_w</span> <span class="o">=</span> <span class="n">w</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_block</span> <span class="o">=</span> <span class="n">block</span>
<span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_block</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">,</span> <span class="n">n</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_w</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="pattern-de-creation-singleton">
<h2>Pattern de création : singleton<a class="headerlink" href="#pattern-de-creation-singleton" title="Lien permanent vers ce titre"></a></h2>
<p># utiliser un module au lieu dune classe</p>
<p>in <cite>toto.py</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Toto</span><span class="p">()</span>
<span class="n">toto</span> <span class="o">=</span> <span class="n">Toto</span><span class="p">()</span>
</pre></div>
</div>
<p>in another module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">toto</span> <span class="k">import</span> <span class="n">toto</span>
</pre></div>
</div>
</div>
<div class="section" id="la-factory">
<h2>la factory<a class="headerlink" href="#la-factory" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="n">pkg</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
<span class="n">m</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="n">pkg</span><span class="p">,</span> <span class="p">{},</span> <span class="p">{},</span> <span class="p">[</span><span class="n">obj</span><span class="p">])</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
<span class="bp">cls</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="s1">&#39;p1.p2.p3&#39;</span><span class="p">,</span> <span class="s1">&#39;c4&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="template-method-self-delegation">
<h2>template method (self delegation)<a class="headerlink" href="#template-method-self-delegation" title="Lien permanent vers ce titre"></a></h2>
<p># Abstract base class:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">OrganiseMethod</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">org_method</span><span class="p">():</span>
<span class="k">def</span> <span class="nf">do_this</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">do_that</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">Concrete</span><span class="p">(</span><span class="n">OrganiseMethod</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">do_this</span><span class="p">():</span> <span class="o">...</span>
<span class="k">def</span> <span class="nf">do_that</span><span class="p">():</span> <span class="o">...</span>
</pre></div>
</div>
<p>il est préférable de lever une NotImplementedError, ce qui revient à faire
une classe abstraite.</p>
</div>
</div>
</div>
</div>
</div>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="search.html"> Recherche</a> | &nbsp;
<!-- a href="genindex.html"> Genindex</a-->
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="stdlib.html" title="La librairie standard"
>suivant</a> |</li>
<li class="right" >
<a href="exceptions.html" title="Le style de programmation par exceptions"
>précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2015, cadoles (www.cadoles.com).
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>