PageRenderTime 81ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 2ms

/book/index.html

http://github.com/addyosmani/essential-js-design-patterns
HTML | 8994 lines | 7591 code | 1387 blank | 16 comment | 0 complexity | 84a01cd8c47bb17ca2a4f5ba0ec03f19 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <!doctype html>
  2. <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
  3. <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
  4. <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
  5. <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Learning JavaScript Design Patterns</title>
  10. <meta name="description" content="An open-source book on JavaScript Design Patterns">
  11. <meta name="author" content="Addy Osmani">
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <meta http-equiv="cleartype" content="on">
  14. <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
  15. <!-- build:css styles/vendor.css -->
  16. <!-- bower:css -->
  17. <link rel="stylesheet" href="bower_components/codecode/codecode.css" />
  18. <!-- endbower -->
  19. <link rel="stylesheet" href="styles/style.css" >
  20. <link rel="stylesheet" href="styles/shCore.css">
  21. <link rel="stylesheet" href="styles/shThemeRDark.css">
  22. <link rel="stylesheet" href="styles/sausage.css">
  23. <link rel="stylesheet" href="styles/base.css">
  24. <!-- endbuild -->
  25. </head>
  26. <body>
  27. <!--[if lt IE 10]>
  28. <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  29. <![endif]-->
  30. <div class="container inner wrap">
  31. <header>
  32. <p>
  33. <h1 class="booktitle title">Learning JavaScript Design Patterns</h1>
  34. <h3 class="booktitle author">
  35. A book by <a href="http://twitter.com/addyosmani">Addy Osmani</a>
  36. </h3>
  37. <h2 class="booktitle edition">Volume 1.7.0</h2>
  38. <p class="copyright">
  39. Copyright &copy; Addy Osmani 2017.
  40. </p>
  41. <p class="copyright"><em>Learning JavaScript Design Patterns</em> is released under a <a href="http://creativecommons.org/licenses/by-nc-nd/3.0/">Creative Commons Attribution-Noncommercial-No Derivative Works 3.0</a> unported license. It is available for purchase via <a href="http://shop.oreilly.com/product/0636920025832.do">O'Reilly</a> Media but will remain available for both free online and as a physical (or eBook) purchase for readers wishing to support the project.</p>
  42. <p><a href="https://www.amazon.com/_/dp/1449331815"><img src="../cover/cover.jpg" width="300px" alt="Book cover for Learning JavaScript Design Patterns"></a></p>
  43. <p class="copyright">&nbsp;</p>
  44. <a id="reviews" href="http://shop.oreilly.com/product/0636920025832/ReviewSubmit.do?sortby=publicationDate?pageId=0636920025832.IP"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://addyosmani.github.com/backbone-fundamentals/img/helpful.png" alt="Was this helpful? We'd love you to write a review."></a>
  45. </header>
  46. <div class="content">
  47. <h1>Preface</h1>
  48. <p>Design patterns are reusable solutions to commonly occurring problems in software design. They are both exciting and a fascinating topic to explore in any programming language.</p>
  49. <p>One reason for this is that they help us build upon the combined experience of many developers that came before us and ensure we structure our code in an optimized way, meeting the needs of problems we're attempting to solve.</p>
  50. <p>Design patterns also provide us a common vocabulary to describe solutions. This can be significantly simpler than describing syntax and semantics when we're attempting to convey a way of structuring a solution in code form to others.</p>
  51. <p>In this book we will explore applying both classical and modern design patterns to the JavaScript programming language.</p>
  52. <p>&nbsp;</p>
  53. <h2>Target Audience</h2>
  54. <p>This book is targeted at professional developers wishing to improve their knowledge of design patterns and how they can be applied to the JavaScript programming language.</p>
  55. <p>Some of the concepts covered (closures, prototypal inheritance) will assume a level of basic prior knowledge and understanding. If you find yourself needing to read further about these topics, a list of suggested titles is provided for convenience.</p>
  56. <p>If you would like to learn how to write beautiful, structured and organized code, I believe this is the book for you.</p>
  57. <h2>Acknowledgments</h2>
  58. <p>I will always be grateful for the talented technical reviewers who helped review and improve this book, including those from the community at large. The knowledge and enthusiasm they brought to the project was simply amazing. The official technical reviewers tweets and blogs are also a regular source of both ideas and inspiration and I wholeheartedly recommend checking them out.</p>
  59. <p>
  60. <ul>
  61. <li>Nicholas Zakas (<a href="http://nczonline.net/">http://nczonline.net</a>, <a href="http://twitter.com/slicknet">@slicknet</a>)</li>
  62. <li>Andrée Hansson (<a href="http://andreehansson.se/">http://andreehansson.se</a>, <a href="http://twitter.com/peolanha">@peolanha</a>)</li>
  63. <li>Luke Smith (<a href="http://lucassmith.name">http://lucassmith.name</a>, <a href="http://twitter.com/ls_n">@ls_n</a>)</li>
  64. <li>Eric Ferraiuolo (<a href="http://ericf.me/">http://ericf.me/</a>, <a href="https://twitter.com/ericf">@ericf</a>)</li>
  65. <li>Peter Michaux (<a href="http://michaux.ca">http://michaux.ca</a>, <a href="http://twitter.com/petermichaux">@petermichaux</a>)</li>
  66. <li>Alex Sexton (<a href="http://alexsexton.com">http://alexsexton.com</a>, <a href="http://twitter.com/slexaxton">@slexaxton</a>)</li>
  67. </ul>
  68. </p>
  69. <p>I would also like to thank Rebecca Murphey (<a href="http://rmurphey.com">http://rmurphey.com</a>, <a href="http://twitter.com/rmurphey">@rmurphey</a>) for providing the inspiration to write this book and more importantly, continue to make it both available on GitHub and via O'Reilly.</p>
  70. <p>Finally, I would like to thank my wonderful wife Ellie, for all of her support while I was putting together this publication.</p>
  71. <h2>Credits</h2>
  72. <p>Whilst some of the patterns covered in this book were implemented based on personal experience, many of them have been previously identified by the JavaScript community. This work is as such the production of the combined experience of a number of developers. Similar to Stoyan Stefanov's logical approach to preventing interruption of the narrative with credits (in <em>JavaScript Patterns</em>), I have listed credits and suggested reading for any content covered in the references section.</p>
  73. <p>If any articles or links have been missed in the list of references, please accept my heartfelt apologies. If you contact me I'll be sure to update them to include you on the list.</p>
  74. <h2>Reading</h2>
  75. <p>
  76. Whilst this book is targeted at both beginners and intermediate developers, a basic understanding of JavaScript fundamentals is assumed. Should you wish to learn more about the language, I am happy to recommend the following titles:</p>
  77. <ul>
  78. <li><em>JavaScript: The Definitive Guide</em> by David Flanagan</li>
  79. <li><em>Eloquent JavaScript</em> by Marijn Haverbeke</li>
  80. <li><em>JavaScript Patterns</em> by Stoyan Stefanov</li>
  81. <li><em>Writing Maintainable JavaScript</em> by Nicholas Zakas</li>
  82. <li><em>JavaScript: The Good Parts</em> by Douglas Crockford</li>
  83. </ul>
  84. <p>&nbsp;</p>
  85. <h1><em>Table Of Contents</em></h1>
  86. <div id="contents-list">
  87. <ul>
  88. <li><a href="#introduction">Introduction</a></li>
  89. <li><a href="#whatisapattern">What is a Pattern?</a></li>
  90. <li><a href="#patternity">"Pattern"-ity Testing, Proto-Patterns &amp; The Rule Of Three</a></li>
  91. <li><a href="#designpatternstructure">The Structure Of A Design Pattern</a></li>
  92. <li><a href="#writingdesignpatterns">Writing Design Patterns</a></li>
  93. <li><a href="#antipatterns">Anti-Patterns</a></li>
  94. <li><a href="#categoriesofdesignpatterns">Categories Of Design Pattern</a></li>
  95. <li><a href="#summarytabledesignpatterns">Summary Table Of Design Pattern Categorization</a></li>
  96. <li><a href="#designpatternsjavascript">JavaScript Design Patterns
  97. </a>
  98. <ul>
  99. <li class="subitem"><a href="#constructorpatternjavascript">Constructor Pattern</a></li>
  100. <li class="subitem"><a href="#modulepatternjavascript">Module Pattern</a></li>
  101. <li class="subitem"><a href="#revealingmodulepatternjavascript">Revealing Module Pattern</a></li>
  102. <li class="subitem"><a href="#singletonpatternjavascript">Singleton Pattern</a></li>
  103. <li class="subitem"><a href="#observerpatternjavascript">Observer Pattern</a></li>
  104. <li class="subitem"><a href="#mediatorpatternjavascript">Mediator Pattern</a></li>
  105. <li class="subitem"><a href="#prototypepatternjavascript">Prototype Pattern</a></li>
  106. <li class="subitem"><a href="#commandpatternjavascript">Command Pattern</a></li>
  107. <li class="subitem"><a href="#facadepatternjavascript">Facade Pattern</a></li>
  108. <li class="subitem"><a href="#factorypatternjavascript">Factory Pattern</a></li>
  109. <li class="subitem"><a href="#mixinpatternjavascript">Mixin Pattern</a>
  110. <li class="subitem"><a href="#decoratorpatternjavascript">Decorator Pattern</a></li>
  111. <li class="subitem"><a href="#detailflyweight">Flyweight Pattern</a>
  112. </ul>
  113. </li>
  114. <li><a href="#detailmvcmvp">JavaScript MV* Patterns</a>
  115. <ul>
  116. <li class="subitem"><a href="#detailmvc">MVC Pattern</a>
  117. <li class="subitem"><a href="#detailmvp">MVP Pattern</a>
  118. <li class="subitem"><a href="#detailmvvm">MVVM Pattern</a>
  119. </ul>
  120. </li>
  121. <li><a href="#modularjavascript">Modern Modular JavaScript Design Patterns</a>
  122. <ul>
  123. <li class="subitem"><a href="#detailamd">AMD</a>
  124. <li class="subitem"><a href="#detailcommonjs">CommonJS</a>
  125. <li class="subitem"><a href="#detailharmony">ES Harmony</a>
  126. </ul>
  127. </li>
  128. <li><a href="#designpatternsjquery">Design Patterns In jQuery</a>
  129. <ul>
  130. <li class="subitem"><a href="#compositepatternjquery">Composite Pattern</a></li>
  131. <li class="subitem"><a href="#wrapperpatternjquery">Adapter Pattern</a></li>
  132. <li class="subitem"><a href="#facadepatternjquery">Facade Pattern</a></li>
  133. <li class="subitem"><a href="#observerpatternjquery">Observer Pattern</a></li>
  134. <li class="subitem"><a href="#iteratorpatternjquery">Iterator Pattern</a></li>
  135. <li class="subitem"><a href="#lazyinitialisationjquery">Lazy Initialization Pattern</a></li>
  136. <li class="subitem"><a href="#proxypatternjquery">Proxy Pattern</a></li>
  137. <li class="subitem"><a href="#builderpatternjquery">Builder Pattern</a></li>
  138. </ul>
  139. </li>
  140. <li><a href="#jquerypluginpatterns">jQuery Plugin Design Patterns</a></li>
  141. <li><a href="#detailnamespacing">JavaScript Namespacing Patterns</a></li>
  142. <li><a href="#conclusions">Conclusions</a></li>
  143. <li><a href="#references">References</a></li>
  144. </ul>
  145. </div>
  146. </p>
  147. <p>&nbsp;</p>
  148. <h1 id="introduction">
  149. <a href="#introduction" class="subhead-link">#</a>
  150. Introduction
  151. </h1>
  152. <p>One of the most important aspects of writing maintainable code is being able to notice the recurring themes in that code and optimize them. This is an area where knowledge of design patterns can prove invaluable.</p>
  153. <p>In the first part of this book, we will explore the history and importance of design patterns which can really be applied to any programming language. If you're already sold on or are familiar with this history, feel free to skip to the chapter "<a href="#whatisapattern">What is a Pattern?</a>" to continue reading.</p>
  154. <p>Design patterns can be traced back to the early work of an architect named <a href="http://en.wikipedia.org/wiki/Christopher_Alexander">Christopher Alexander</a>. He would often write publications about his experience in solving design issues and how they related to buildings and towns. One day, it occurred to Alexander that when used time and time again, certain design constructs lead to a desired optimal effect. </p>
  155. <p>In collaboration with Sara Ishikawa and Murray Silverstein, Alexander produced a pattern language that would help empower anyone wishing to design and build at any scale. This was published back in 1977 in a paper titled "A Pattern Language", which was later released as a complete hardcover <a href="http://www.amazon.co.uk/Pattern-Language-Buildings-Construction-Environmental/dp/0195019199/ref=sr_1_1?s=books&ie=UTF8&qid=1329440685&sr=1-1">book</a>. </p>
  156. <p>Some 30 years ago, software engineers began to incorporate the principles Alexander had written about into the first documentation about design patterns, which was to be a guide for novice developers looking to improve their coding skills. It's important to note that the concepts behind design patterns have actually been around in the programming industry since its inception, albeit in a less formalized form. </p>
  157. <p>One of the first and arguably most iconic formal works published on design patterns in software engineering was a book in 1995 called <em>Design Patterns: Elements Of Reusable Object-Oriented Software</em>. This was written by <a href="http://en.wikipedia.org/wiki/Erich_Gamma">Erich Gamma</a>,<a href="http://en.wikipedia.org/w/index.php?title=Richard_Helm&action=edit&redlink=1"> Richard Helm</a>,<a href="http://en.wikipedia.org/wiki/Ralph_Johnson"> Ralph Johnson</a> and<a href="http://en.wikipedia.org/wiki/John_Vlissides"> John Vlissides</a> - a group that became known as the Gang of Four (or GoF for short). </p>
  158. <p>The GoF's publication is considered quite instrumental to pushing the concept of design patterns further in our field as it describes a number of development techniques and pitfalls as well as providing twenty-three core Object-Oriented design patterns frequently used around the world today. We will be covering these patterns in more detail in the section "Categories of Design Patterns".</p>
  159. <p>In this book, we will take a look at a number of popular JavaScript design patterns and explore why certain patterns may be more suitable for your projects than others. Remember that patterns can be applied not just to vanilla JavaScript (i.e standard JavaScript code), but also to abstracted libraries such as <a href="http://jquery.com">jQuery</a> or <a href="http://dojotoolkit.org">dojo</a> as well. Before we begin, let&rsquo;s look at the exact definition of a "pattern" in software design.</p>
  160. <p>&nbsp;</p>
  161. <p>&nbsp;</p>
  162. <h1 id="whatisapattern">What is a Pattern?</h1>
  163. <p>A pattern is a reusable solution that can be applied to commonly occurring problems in software design - in our case - in writing JavaScript web applications. Another way of looking at patterns are as templates for how we solve problems - ones which can be used in quite a few different situations.</p>
  164. <p>So, why is it important to understand patterns and be familiar with them? Design patterns have three main benefits: </p>
  165. <ol start="1" type="1">
  166. <li><strong>Patterns are proven solutions:</strong> They provide solid approaches to solving issues in software development using proven techniques that reflect the experience and insights the developers that helped define them bring to the pattern.</li>
  167. <li><strong>Patterns can be easily reused: </strong>A pattern usually reflects an out of the box solution that can be adapted to suit our own needs. This feature makes them quite robust.</li>
  168. <li><strong>Patterns can be expressive: </strong>When we look at a pattern there&rsquo;s generally a set structure and <em>vocabulary</em> to the solution presented that can help express rather large solutions quite elegantly.</li>
  169. </ol>
  170. <p>Patterns are <strong>not</strong> an exact solution. It&rsquo;s important that we remember the role of a pattern is merely to provide us with a solution scheme. Patterns don&rsquo;t solve all design problems nor do they replace good software designers, however, they <strong>do</strong> support them. Next we&rsquo;ll take a look at some of the other advantages patterns have to offer. </p>
  171. <ul>
  172. <li><strong>Reusing patterns assists in preventing minor issues that can cause major problems in the application development process. </strong>What this means is when code is built on proven patterns, we can afford to spend less time worrying about the structure of our code and more time focusing on the quality of our overall solution. This is because patterns can encourage us to code in a more structured and organized fashion avoiding the need to refactor it for cleanliness purposes in the future.</li>
  173. <li><strong>Patterns can provide generalized solutions which are documented in a fashion that doesn't require them to be tied to a specific problem.</strong> This generalized approach means that regardless of the application (and in many cases the programming language) we are working with, design patterns can be applied to improve the structure of our code.</li>
  174. <li><strong>Certain patterns can actually decrease the overall file-size footprint of our code by avoiding repetition. </strong>By encouraging developers to look more closely at their solutions for areas where instant reductions in <span id="internal-source-marker_0.982673292361492">repetition</span> can be made, e.g. reducing the number of functions performing similar processes in favor of a single generalized function, the overall size of our codebase can be decreased. This is also known as making code more <em>DRY</em>.</li>
  175. <li><strong>Patterns add to a developer's vocabulary, which makes communication faster.</strong></li>
  176. <li><strong>Patterns that are frequently used can be improved over time by harnessing the collective experiences other developers using those patterns contribute back to the design pattern community.</strong> In some cases this leads to the creation of entirely new design patterns whilst in others it can lead to the provision of improved guidelines on how specific patterns can be best used. This can ensure that pattern-based solutions continue to become more robust than ad-hoc solutions may be.</li>
  177. </ul>
  178. <p>&nbsp;</p>
  179. <h3>We already use patterns everyday</h3>
  180. <p>
  181. To understand how useful patterns can be, let's review a very simple element selection problem that the jQuery library solves for us. </p>
  182. <p>
  183. Imagine that we have a script where for each DOM element found on a page with class "foo" we wish to increment a counter. What's the most efficient way to query for this collection of elements? Well, there are a few different ways this problem could be tackled:</p>
  184. <p>
  185. <ol>
  186. <li>Select all of the elements in the page and then store references to them. Next, filter this collection and use regular expressions (or another means) to only store those with the class "foo".</li>
  187. <li>
  188. Use a modern native browser feature such as <code>querySelectorAll()</code> to select all of the elements with the class "foo".</li>
  189. <li>Use a native feature such as <code>getElementsByClassName()</code> to similarly get back the desired collection. </li>
  190. </ol>
  191. </p>
  192. <p>
  193. So, which of these options is the fastest? It's actually option 3. by a factor of 8-10 times the <a href="http://jsperf.com/getelementsbyclassname-vs-queryselectorall/5">alternatives</a>. In a real-world application however, 3. will not work in versions of Internet Explorer below 9 and thus it's necessary to use 1. where both 2. and 3. aren't supported.</p>
  194. <p>
  195. Developers using jQuery don't have to worry about this problem however, as it's luckily abstracted away for us using the <em>Facade</em> pattern. As we'll review in more detail later, this pattern provides a simple set of abstracted interfaces (e.g <code>$el.css()</code>, <code>$el.animate()</code>) to several more complex underlying bodies of code. As we've seen, this means less time having to be concerned about implementation level details.</p>
  196. <p>Behind the scenes, the library simply opts for the most optimal approach to selecting elements depending on what our current browser supports and we just consume the abstraction layer.</p>
  197. <p>We're probably all also familiar with jQuery's <code>$("selector")</code>. This is significantly more easy to use for selecting HTML elements on a page versus having to manually opt for <code>getElementById()</code>, <code>getElementsByClassName()</code>, <code>getElementsByTagName()</code> and so on.</p>
  198. <p>Although we know that <code>querySelectorAll()</code> attempts to solve this problem, compare the effort involved in using jQuery's Facade interfaces vs. selecting the most optimal selection paths ourselves. There's no contest! Abstractions using patterns can offer real-world value.</p>
  199. <p>
  200. We'll be looking at this and more design patterns later on in the book.</p>
  201. <p>&nbsp;</p>
  202. <h1 id="patternity">"Pattern"-ity Testing, Proto-Patterns &amp; The Rule Of Three</h1>
  203. <p>
  204. Remember that not every algorithm, best practice or solution represents what might be considered a complete pattern. There may be a few key ingredients here that are missing and the pattern community is generally wary of something claiming to be one unless it has been heavily vetted. Even if something is presented to us which <strong>appears</strong> to meet the criteria for a pattern, it should not be considered one until it has undergone suitable periods of scrutiny and testing by others.</p>
  205. <p>
  206. Looking back upon the work by Alexander once more, he claims that a pattern should both be a process and a "thing". This definition is obtuse on purpose as he follows by saying that it is the process which should create the "thing". This is a reason why patterns generally focus on addressing a visually identifiable structure i.e we should be able to visually depict (or draw) a picture representing the structure that placing the pattern into practice results in. </p>
  207. <p>
  208. In studying design patterns, it's not irregular to come across the term "proto-pattern". What is this? Well, a pattern that has not yet been known to pass the "pattern"-ity tests is usually referred to as a proto-pattern. Proto-patterns may result from the work of someone that has established a particular solution that is worthy of sharing with the community, but may not have yet had the opportunity to have been vetted heavily due to its very young age.</p>
  209. <p>
  210. Alternatively, the individual(s) sharing the pattern may not have the time or interest of going through the "pattern"-ity process and might release a short description of their proto-pattern instead. Brief descriptions or snippets of this type of pattern are known as patlets.</p>
  211. <p>
  212. The work involved in fully documenting a qualified pattern can be quite daunting. Looking back at some of the earliest work in the field of design patterns, a pattern may be considered "good" if it does the following: </p>
  213. <ul type="disc">
  214. <li><strong>Solves a particular problem</strong>: Patterns are not supposed to just capture principles or strategies. They need to capture solutions. This is one of the most essential ingredients for a good pattern.</li>
  215. <li><strong>The solution to this problem cannot be obvious</strong>: We can find that problem-solving techniques often attempt to derive from well-known first principles. The best design patterns usually provide solutions to problems indirectly - this is considered a necessary approach for the most challenging problems related to design.</li>
  216. <li><strong>The concept described must have been proven</strong>: Design patterns require proof that they function as described and without this proof the design cannot be seriously considered. If a pattern is highly speculative in nature, only the brave may attempt to use it.</li>
  217. <li><strong>It must describe a relationship</strong>: In some cases it may appear that a pattern describes a type of module. Although an implementation may appear this way, the official description of the pattern must describe much deeper system structures and mechanisms that explain its relationship to code.</li>
  218. </ul>
  219. <p>
  220. We would be forgiven for thinking that a proto-pattern which fails to meet guidelines isn't worth learning from, however, this is far from the truth. Many proto-patterns are actually quite good. I&rsquo;m not saying that all proto-patterns are worth looking at, but there are quite a few useful ones in the wild that could assist us with future projects. Use best judgment with the above list in mind and you&rsquo;ll be fine in your selection process.</p>
  221. <p>
  222. One of the additional requirements for a pattern to be valid is that they display some recurring phenomenon. This is often something that can be qualified in at least three key areas, referred to as the <em>rule of three</em>. To show recurrence using this rule, one must demonstrate: </p>
  223. <ol start="1" type="1">
  224. <li><strong>Fitness of purpose</strong> - how is the pattern considered successful?</li>
  225. <li><strong>Usefulness</strong> - why is the pattern considered successful?</li>
  226. <li><strong>Applicability</strong> - is the design worthy of being a pattern because it has wider applicability? If so, this needs to be explained. When reviewing or defining a pattern, it is important to keep the above in mind.</li>
  227. </ol>
  228. <p>&nbsp;</p>
  229. <h1 id="designpatternstructure">
  230. <a href="#designpatternstructure" class="subhead-link">#</a>
  231. The Structure Of A Design Pattern
  232. </h1>
  233. <p>You may be curious about how a pattern author might approach outlining structure, implementation and purpose of a new pattern.&nbsp; A pattern is initially presented in the form of a <strong>rule</strong> that establishes a relationship between:</p>
  234. <p>
  235. <ul>
  236. <li>A <strong>context</strong></li>
  237. <li>A system of <strong>forces</strong> that arises in that context and</li>
  238. <li>A <strong>configuration</strong> that allows these forces to resolve themselves in context</li>
  239. </ul>
  240. <p>With this in mind, let&rsquo;s now take a look at a summary of the component elements for a design pattern. A design pattern should have a:</p>
  241. <ul>
  242. <li><strong>Pattern name</strong> and a <strong>description </strong></li>
  243. <li><strong>Context outline</strong> the contexts in which the pattern is effective in responding to the users needs. </li>
  244. <li><strong>Problem statement</strong> a statement of the problem being addressed so we can understand the intent of the pattern. </li>
  245. <li><strong>Solution</strong> a description of how the user&rsquo;s problem is being solved in an understandable list of steps and perceptions. </li>
  246. <li><strong>Design</strong> a description of the pattern&rsquo;s design and in particular, the user&rsquo;s behavior in interacting with it </li>
  247. <li><strong>Implementation </strong> a guide to how the pattern would be implemented </li>
  248. <li><strong>Illustrations</strong> a visual representation of classes in the pattern (e.g. a diagram) </li>
  249. <li><strong>Examples</strong> an implementation of the pattern in a minimal form </li>
  250. <li><strong>Co-requisites</strong> what other patterns may be needed to support use of the pattern being described? </li>
  251. <li><strong>Relations</strong> what patterns does this pattern resemble? does it closely mimic any others? </li>
  252. <li><strong>Known usage</strong> is the pattern being used in the <em>wild</em>? If so, where and how? </li>
  253. <li><strong>Discussions</strong> the team or author&rsquo;s thoughts on the exciting benefits of the pattern </li>
  254. </ul>
  255. <p>Design patterns are quite a powerful approach to getting all of the developers in an organization or team on the same page when creating or maintaining solutions. If considering working on a pattern of your own, remember that although they may have a heavy initial cost in the planning and write-up phases, the value returned from that investment can be quite worth it. Always research thoroughly before working on new patterns however, as you may find it more beneficial to use or build on top of existing proven patterns than starting afresh.</p>
  256. <p>&nbsp;</p>
  257. <h1 id="writingdesignpatterns">
  258. <a href="#writingdesignpatterns" class="subhead-link">#</a>
  259. Writing Design Patterns
  260. </h1>
  261. <p>Although this book is aimed at those new to design patterns, a fundamental understanding of how a design pattern is written can offer a number of useful benefits. For starters, we can gain a deeper appreciation for the reasoning behind why a pattern is needed. We can also learn how to tell if a pattern (or proto-pattern) is up to scratch when reviewing it for our own needs.</p>
  262. <p>
  263. Writing good patterns is a challenging task. Patterns not only need to (ideally) provide a substantial quantity of reference material for end-users, but they also need to be able to defend why they are necessary.
  264. </p>
  265. <p>Having read the previous section on <em>what</em> a pattern is, we may think that this in itself is enough to help us identify patterns we see in the wild. This is actually not completely true. It's not always clear if a piece of code we're looking at is following a set pattern or just accidentally happens to appear like it does.</p>
  266. <p>
  267. When we're looking at a body of code we think may be using a pattern, we should consider writing down some of the aspects of the code that we believe falls under a particular existing pattern or set of patterns.</p>
  268. <p>In many cases of pattern-analysis we can find that we're just looking at code that follows good principles and design practices that could happen to overlap with the rules for a pattern by accident. Remember - solutions in which neither interactions nor defined rules appear are <em>not</em> patterns. </p>
  269. <p>
  270. If interested in venturing down the path of writing your own design patterns I recommend learning from others who have already been through the process and done it well. Spend time absorbing the information from a number of different design pattern descriptions and take in what&rsquo;s meaningful to you.</p>
  271. <p>Explore structure and semantics - this can be done by examining the interactions and context of the patterns you are interested in so you can identify the principles that assist in organizing those patterns together in useful configurations.</p>
  272. <p>
  273. Once we've exposed ourselves to a wealth of information on pattern literature, we may wish to begin writing our pattern using an <em>existing</em> format and see if we can brainstorm new ideas for improving it or integrating our ideas in there.</p>
  274. <p>An example of a developer that did this is in recent years is Christian Heilmann, who took the existing <em>Module</em> pattern and made some fundamentally useful changes to it to create the <em>Revealing Module</em> pattern (this is one of the patterns covered later in this book). </p>
  275. <p>
  276. The following are tips I would suggest if interested in creating a new design pattern:</p>
  277. <ul type="disc">
  278. <li><strong>How practical is the pattern?</strong>: Ensure the pattern describes proven solutions to recurring problems rather than just speculative solutions which haven&rsquo;t been qualified.</li>
  279. <li><strong>Keep best practices in mind:</strong> The design decisions we make should be based on principles we derive from an understanding of best practices. </li>
  280. <li><strong>Our design patterns should be transparent to the user</strong>: Design patterns should be entirely transparent to any type of user-experience. They are primarily there to serve the developers using them and should not force changes to behavior in the user-experience that would not be incurred without the use of a pattern.</li>
  281. <li><strong>Remember that originality is <em>not</em> key in pattern design</strong>: When writing a pattern, we do not need to be the original discoverer of the solutions being documented nor do you have to worry about our design overlapping with minor pieces of other patterns. If the approach is strong enough to have broad useful applicability, it has a chance of being recognized as a valid pattern.</li>
  282. <li><strong>Patterns need a strong set of examples:</strong> A good pattern description needs to be followed by an equally strong set of examples demonstrating the successful application of our pattern. To show broad usage, examples that exhibit good design principles are ideal.</li>
  283. </ul>
  284. <p>
  285. Pattern writing is a careful balance between creating a design that is general, specific and above all, useful. Try to ensure that if writing a pattern you cover the widest possible areas of application and you should be fine. &nbsp;I hope that this brief introduction to writing patterns has given you some insights that will assist your learning process for the next sections of this book. </p>
  286. <p>&nbsp;</p>
  287. <h1 id="antipatterns">
  288. <a href="#antipatterns" class="subhead-link">#</a>
  289. Anti-Patterns
  290. </h1>
  291. <p>If we consider that a pattern represents a best practice, an anti-pattern represents a lesson that has been learned. The term anti-patterns was coined in 1995 by Andrew Koenig in the November C++ Report that year, inspired by the GoF's book <em>Design Patterns</em>. In Koenig&rsquo;s report, there are two notions of anti-patterns that are presented. Anti-Patterns: </p>
  292. <ul type="disc">
  293. <li>Describe a <em>bad</em> solution to a particular problem which resulted in a bad situation occurring </li>
  294. <li>Describe <em>how</em> to get out of said situation and how to go from there to a good solution </li>
  295. </ul>
  296. <p>
  297. <p>On this topic, Alexander writes about the difficulties in achieving a good balance between good design structure and good context:</p>
  298. <p><em>&ldquo;These notes are about the process of design; the process of inventing physical things which display a new physical order, organization, form, in response to function.every design problem begins with an effort to achieve fitness between two entities: the form in question and its context. The form is the solution to the problem; the context defines the problem&rdquo;.</em></p>
  299. <p>
  300. While it&rsquo;s quite important to be aware of design patterns, it can be equally important to understand anti-patterns. Let us qualify the reason behind this. When creating an application, a project&rsquo;s life-cycle begins with construction however once you&rsquo;ve got the initial release done, it needs to be maintained. The quality of a final solution will either be <em>good</em> or <em>bad</em>, depending on the level of skill and time the team have invested in it. Here <em>good</em> and <em>bad</em> are considered in context - a &lsquo;perfect&rsquo; design may qualify as an anti-pattern if applied in the wrong context. </p>
  301. <p>
  302. The bigger challenges happen after an application has hit production and is ready to go into maintenance mode. A developer working on such a system who hasn&rsquo;t worked on the application before may introduce a <em>bad</em> design into the project by accident. If said <em>bad</em> practices are created as anti-patterns, they allow developers a means to recognize these in advance so that they can avoid common mistakes that can occur - this is parallel to the way in which design patterns provide us with a way to recognize common techniques that are <em>useful.</em></p>
  303. <p>
  304. To summarize, an anti-pattern is a bad design that is worthy of documenting. Examples of anti-patterns in JavaScript are the following: </p>
  305. <ul type="disc">
  306. <li>Polluting the global namespace by defining a large number of variables in the global context</li>
  307. <li>Passing strings rather than functions to either setTimeout or setInterval as this triggers the use of <code>eval()</code> internally.</li>
  308. <li>Modifying the <code>Object</code> class prototype (this is a particularly bad anti-pattern)</li>
  309. <li>Using JavaScript in an inline form as this is inflexible </li>
  310. <li>The use of document.write where native DOM alternatives such as document.createElement are more appropriate. document.write has been grossly misused over the years and has quite a few disadvantages including that if it's executed after the page has been loaded it can actually overwrite the page we're on, whilst document.createElement does not. We can see <a href="http://jsfiddle.net/addyosmani/6T9vX/">here</a> for a live example of this in action. It also doesn't work with XHTML which is another reason opting for more DOM-friendly methods such as document.createElement is favorable.</li>
  311. </ul>
  312. <p>
  313. Knowledge of anti-patterns is critical for success. Once we are able to recognize such anti-patterns, we're able to refactor our code to negate them so that the overall quality of our solutions improves instantly. </p>
  314. <p>&nbsp;</p>
  315. <h1 id="categoriesofdesignpatterns">
  316. <a href="#categoriesofdesignpatterns" class="subhead-link">#</a>
  317. Categories Of Design Pattern
  318. </h1>
  319. <p>&nbsp;</p>
  320. <p>A glossary from the well-known design book, <em>Domain-Driven Terms</em>, rightly states that:</p>
  321. <p><i>&ldquo;A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. The design pattern identifies the participating classes and their instances, their roles and collaborations, and the distribution of responsibilities. </i></p>
  322. <p><i>Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether or not it can be applied in view of other design constraints, and the consequences and trade-offs of its use. Since we must eventually implement our designs, a design pattern also provides sample ... code to illustrate an implementation. </i></p>
  323. <p><i>Although design patterns describe object-oriented designs, they are based on practical solutions that have been implemented in mainstream object-oriented programming languages ....&rdquo;</i></p>
  324. <p>Design patterns can be broken down into a number of different categories. In this section we&rsquo;ll review three of these categories and briefly mention a few examples of the patterns that fall into these categories before exploring specific ones in more detail.
  325. </p>
  326. <p>&nbsp;</p>
  327. <h2>Creational Design Patterns</h2>
  328. <p>
  329. <p>Creational design patterns focus on handling object creation mechanisms where objects are created in a manner suitable for the situation we're working in. The basic approach to object creation might otherwise lead to added complexity in a project whilst these patterns aim to solve this problem by <em>controlling</em> the creation process.</p>
  330. <p>Some of the patterns that fall under this category are: Constructor, Factory, Abstract, Prototype, Singleton and Builder.</p>
  331. <h2>Structural Design Patterns</h2>
  332. <p>Structural patterns are concerned with object composition and typically identify simple ways to realize relationships between different objects. They help ensure that when one part of a system changes, the entire structure of the system doesn't need to do the same. They also assist in recasting parts of the system which don't fit a particular purpose into those that do. </p>
  333. <p>Patterns that fall under this category include: Decorator, Facade, Flyweight, Adapter and Proxy.</p>
  334. <h2>Behavioral Design Patterns</h2>
  335. <p>Behavioral patterns focus on improving or streamlining the communication between disparate objects in a system.</p>
  336. <p>Some behavioral patterns include: Iterator, Mediator, Observer and Visitor.</p>
  337. <p>&nbsp;</p>
  338. <h1 id="summarytabledesignpatterns">
  339. <a href="#summarytabledesignpatterns" class="subhead-link">#</a>
  340. Design Pattern Categorization
  341. </h1>
  342. <p>In my early experiences of learning about design patterns, I personally found the following table a very useful reminder of what a number of patterns has to offer - it covers the 23 Design Patterns mentioned by the GoF. The original table was summarized by Elyse Nielsen back in 2004
  343. and I've modified it where necessary to suit our discussion in this section of the book.
  344. <p>I recommend using this table as reference, but do remember that there are a number of additional patterns that are not mentioned here but will be discussed later in the book.</p>
  345. <h3>A brief note on classes</h3>
  346. <p><strong>Note:</strong> ES2015 introduced native support for classes to JavaScript, however, they are primarily syntactical sugar over JavaScript's existing prototype-based inheritance model. We will not be covering ES2015 classes in this book, but <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">MDN</a> has an excellent introduction to them. </p>
  347. <p>Keep in mind that there will be patterns in this table that reference the concept of "classes". In ES5, JavaScript is a class-less language, however classes can be simulated using functions.</p>
  348. <p>The most common approach to achieving this is by defining a JavaScript function where we then create an object using the <code>new</code> keyword. <code>this</code> can be used to help define new properties and methods for the object as follows:</p>
  349. <pre class="brush: js">
  350. // A car "class"
  351. function Car( model ) {
  352. this.model = model;
  353. this.color = "silver";
  354. this.year = "2012";
  355. this.getInfo = function () {
  356. return this.model + " " + this.year;
  357. };
  358. }
  359. </pre>
  360. <p>We can then instantiate the object using the Car constructor we defined above like this:</p>
  361. <pre class="brush: js" >
  362. var myCar = new Car("ford");
  363. myCar.year = "2010";
  364. console.log( myCar.getInfo() );
  365. </pre>
  366. <p>For more ways to define "classes" using JavaScript, see Stoyan Stefanov's useful <a href="http://www.phpied.com/3-ways-to-define-a-javascript-class/">post</a> on them.</p>
  367. <p>Let us now proceed to review the table.
  368. <p>
  369. <p>
  370. <table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="white">
  371. <tbody><tr bgcolor="#fff">
  372. <td colspan="4"><strong>&nbsp;&nbsp;<b>Creational</b></strong></td>
  373. <td colspan="4">&nbsp;&nbsp;Based on the concept of creating an object.</td>
  374. </tr>
  375. <tr bgcolor="#DBDBDB">
  376. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Class </strong></em></td>
  377. </tr>
  378. <tr bgcolor="#F9F9F9">
  379. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Factory Method</em></td>
  380. <td colspan="3">This makes an instance of several derived classes based on interfaced data or events.</td>
  381. </tr>
  382. <tr bgcolor="#DBDBDB">
  383. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Object</strong></em></td>
  384. </tr>
  385. <tr bgcolor="#F9F9F9">
  386. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Abstract Factory</em></td>
  387. <td colspan="3">Creates an instance of several families of classes without detailing concrete classes.</td>
  388. </tr>
  389. <tr bgcolor="#F9F9F9">
  390. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Builder</em></td>
  391. <td colspan="3">Separates object construction from its representation, always creates the same type of object.</td>
  392. </tr>
  393. <tr bgcolor="#F9F9F9">
  394. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Prototype</em></td>
  395. <td colspan="3">A fully initialized instance used for copying or cloning.</td>
  396. </tr>
  397. <tr bgcolor="#F9F9F9">
  398. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Singleton</em></td>
  399. <td colspan="3">A class with only a single instance with global access points.</td>
  400. </tr>
  401. <tr>
  402. <td height="20" width="6">&nbsp;</td>
  403. <td width="6">&nbsp;</td>
  404. <td width="6">&nbsp;</td>
  405. <td width="139">&nbsp;</td>
  406. <td width="1">&nbsp;</td>
  407. <td width="18">&nbsp;</td>
  408. <td width="18">&nbsp;</td>
  409. <td width="681">&nbsp;</td>
  410. </tr>
  411. <tr bgcolor="#fff">
  412. <td colspan="4"><strong>&nbsp;&nbsp;<b>Structural</b></strong></td>
  413. <td colspan="4">&nbsp;&nbsp;Based on the idea of building blocks of objects.</td>
  414. </tr>
  415. <tr bgcolor="#DBDBDB">
  416. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Class </strong></em></td>
  417. </tr>
  418. <tr bgcolor="#F9F9F9">
  419. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Adapter</em></td>
  420. <td colspan="3">Match interfaces of different classes therefore classes can work together despite incompatible interfaces.</td>
  421. </tr>
  422. <tr bgcolor="#DBDBDB">
  423. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Object</strong></em></td>
  424. </tr>
  425. <tr bgcolor="#F9F9F9">
  426. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Adapter</em></td>
  427. <td colspan="3">Match interfaces of different classes therefore classes can work together despite incompatible interfaces.</td>
  428. </tr>
  429. <tr bgcolor="#F9F9F9">
  430. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bridge</em></td>
  431. <td colspan="3">Separates an object's interface from its implementation so the two can vary independently.</td>
  432. </tr>
  433. <tr bgcolor="#F9F9F9">
  434. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Composite</em></td>
  435. <td colspan="3">A structure of simple and composite objects which makes the total object more than just the sum of its parts.</td>
  436. </tr>
  437. <tr bgcolor="#F9F9F9">
  438. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Decorator</em></td>
  439. <td colspan="3">Dynamically add alternate processing to objects.</td>
  440. </tr>
  441. <tr bgcolor="#F9F9F9">
  442. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Facade</em></td>
  443. <td colspan="3">A single class that hides the complexity of an entire subsystem.</td>
  444. </tr>
  445. <tr bgcolor="#F9F9F9">
  446. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flyweight</em></td>
  447. <td colspan="3">A fine-grained instance used for efficient sharing of information that is contained elsewhere.</td>
  448. </tr>
  449. <tr bgcolor="#F9F9F9">
  450. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Proxy</em></td>
  451. <td colspan="3">A place holder object representing the true object.</td>
  452. </tr>
  453. <tr>
  454. <td colspan="8">&nbsp;</td>
  455. </tr>
  456. <tr bgcolor="#fff">
  457. <td colspan="4"><strong>&nbsp;&nbsp;<b>Behavioral</b></strong></td>
  458. <td colspan="4">&nbsp;&nbsp;Based on the way objects play and work together.</td>
  459. </tr>
  460. <tr bgcolor="#DBDBDB">
  461. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Class </strong></em></td>
  462. </tr>
  463. <tr bgcolor="#F9F9F9">
  464. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Interpreter</em></td>
  465. <td colspan="3">A way to include language elements in an application to match the grammar of the intended language.</td>
  466. </tr>
  467. <tr bgcolor="#F9F9F9">
  468. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Template <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method</em></td>
  469. <td colspan="3">Creates the shell of an algorithm in a method, then defer the exact steps to a subclass.</td>
  470. </tr>
  471. <tr bgcolor="#DBDBDB">
  472. <td colspan="8"><em><strong>&nbsp;&nbsp;&nbsp;&nbsp;Object</strong></em></td>
  473. </tr>
  474. <tr bgcolor="#F9F9F9">
  475. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chain of <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Responsibility</em></td>
  476. <td colspan="3">A way of passing a request between a chain of objects to find the object that can handle the request.</td>
  477. </tr>
  478. <tr bgcolor="#F9F9F9">
  479. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Command</em></td>
  480. <td colspan="3">Encapsulate a command request as an object to enable, logging and/or queuing of requests, and provides error-handling for unhandled requests.</td>
  481. </tr>
  482. <tr bgcolor="#F9F9F9">
  483. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator</em></td>
  484. <td colspan="3">Sequentially access the elements of a collection without knowing the inner workings of the collection.</td>
  485. </tr>
  486. <tr bgcolor="#F9F9F9">
  487. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mediator</em></td>
  488. <td colspan="3">Defines simplified communication between classes to prevent a group of classes from referring explicitly to each other.</td>
  489. </tr>
  490. <tr bgcolor="#F9F9F9">
  491. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memento</em></td>
  492. <td colspan="3">Capture an object's internal state to be able to restore it later.</td>
  493. </tr>
  494. <tr bgcolor="#F9F9F9">
  495. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Observer</em></td>
  496. <td colspan="3">A way of notifying change to a number of classes to ensure consistency between the classes.</td>
  497. </tr>
  498. <tr bgcolor="#F9F9F9">
  499. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State</em></td>
  500. <td colspan="3">Alter an object's behavior when its state changes.</td>
  501. </tr>
  502. <tr bgcolor="#F9F9F9">
  503. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Strategy</em></td>
  504. <td colspan="3">Encapsulates an algorithm inside a class separating the selection from the implementation.</td>
  505. </tr>
  506. <tr bgcolor="#F9F9F9">
  507. <td colspan="5"><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor</em></td>
  508. <td colspan="3">Adds a new operation to a class without changing the class.</td>
  509. </tr>
  510. </tbody></table></p>
  511. <p>&nbsp;</p>
  512. <p>&nbsp;</p>
  513. <h1 id="designpatternsjavascript">
  514. <a href="#designpatternsjavascript" class="subhead-link">#</a>
  515. JavaScript Design Patterns
  516. </h1>
  517. <p>&nbsp;</p>
  518. <p>In this section, we will explore JavaScript implementations of a number of both classic and modern design patterns.</p>
  519. <p>Developers commonly wonder whether there is an <em>ideal</em> pattern or set of patterns they should be using in their workflow. There isn't a true single answer to this question; each script and web application we work on is likely to have its own individual needs and we need to think about where we feel a pattern can offer real value to an implementation.</p>
  520. <p>For example, some projects may benefit from the decoupling benefits offered by the Observer pattern (which reduces how dependent parts of an application are on one another) whilst others may simply be too small for decoupling to be a concern at all.</p>
  521. <p

Large files files are truncated, but you can click here to view the full file