{"id":18,"date":"1999-06-04T04:05:00","date_gmt":"1999-06-04T04:05:00","guid":{"rendered":""},"modified":"2020-08-01T20:55:32","modified_gmt":"2020-08-01T20:55:32","slug":"growing-frameworks-patterns","status":"publish","type":"post","link":"https:\/\/billwake.com\/growing-frameworks-patterns\/","title":{"rendered":"Growing Frameworks: Patterns"},"content":{"rendered":"<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Design patterns are micro-architectures that solve a particular design problem. See the class text <i>Design Patterns.<\/i> A pattern is a literary form that describes a problem, the forces surrounding it, and a solution that resolves those forces.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The designs described here will influence your framework&#8217;s structure. They will also be at work in your implementation classes.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">We will examine several design patterns, and how they&#8217;ve been applied in the Java class libraries (mostly AWT and Swing).<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">\u201cMicro-architecture\u201d is meant to convey the idea of a small piece of the total architecture. It may consist of only a couple classes, configured in a way that will solve a particular problem. For example, the Singleton pattern says how to handle classes that should have only one instance:<\/span><\/div>\n<div style=\"margin-left: 0.25in; text-indent: -0.25in;\"> <span style=\"font-family: Symbol; font-size: 10pt;\">\u00b7<span style=\"font: 7pt &quot;Times New Roman&quot;;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span>  <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Make the constructor private (so clients can\u2019t create them)<\/span><\/div>\n<div style=\"margin-left: 0in; text-indent: 0in;\"> <span style=\"font-family: Symbol; font-size: 10pt;\">\u00b7<span style=\"font: 7pt &quot;Times New Roman&quot;;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span>  <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Provide a static method<\/span> <span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">getInstance()<\/span><\/span> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">that returns the single instance.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">This only involves the design structure of one class, but it solves the problem at hand. More complicated patterns may address two, three, or several classes, but they don\u2019t try to provide a comprehensive solution to the \u201cwhole\u201d problem.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Patterns as flex axes [TBD]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">A solution to a problem can be thought of as representing something in a multi-dimensional space. By making the dimensions independent, we attain greater flexibility in assembling the whole solution. While we study patterns, we\u2019ll look at not only what they are, but what makes them flexible.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">This is not a comprehensive analysis of patterns. The best reference is still the original book <i>Design Patterns<\/i> by Gamma et al. Our goal here is to provide enough information to briefly expalin what each pattern does, and relate its flexibility to the needs of frameworks.<\/span><\/div>\n<p><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> <\/span><\/p>\n<div><\/div>\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border: medium none;\">\n<tbody>\n<tr>\n<td style=\"border: 0.75pt solid black; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><b><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Pattern<\/span><\/b><\/div>\n<\/td>\n<td style=\"border-color: black black black -moz-use-text-color; border-style: solid solid solid none; border-width: 0.75pt 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><b><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">What It Makes Flexible<\/span><\/b><\/div>\n<\/td>\n<td style=\"border-color: black black black -moz-use-text-color; border-style: solid solid solid none; border-width: 0.75pt 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><b><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Form vs. Function<\/span><\/b><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Factory Method<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">What is constructed<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Structure<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Adapter<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Support of an interface<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Structure<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Facade<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Complex structure behind a simple interface<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Structure<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Composite<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Containment structure<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Structure<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Decorator<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Cumulative effects<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Algorithm<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Template Method<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Steps of a fixed algorithm<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Algorithm<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Strategy<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Algorithm<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Algorithm<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Observer<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Independence of model from view<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">? [TBD]<\/span><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 0.75pt 0.75pt; padding: 0in 5.4pt; width: 88.05pt;\" valign=\"top\" width=\"117\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Command<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 195.3pt;\" valign=\"top\" width=\"260\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Functions treated as objects<\/span><\/div>\n<\/td>\n<td style=\"border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 0.75pt 0.75pt medium; padding: 0in 5.4pt; width: 90.3pt;\" valign=\"top\" width=\"120\">\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">? [TBD]<\/span><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Factory Method<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">You don&#8217;t always want to expose the details of how an object is constructed. You may not want the user to be aware of the particular construction &#8211; maybe you&#8217;ll change in the future, maybe you build different object types depending on the circumstances, etc.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">One way around this is to use a Factory Method: a class responsible for building instances of another class. The factory method is often a static method in a Factory class; usually the method is named<\/span> <span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">createXxx()<\/span><\/span> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">or<\/span> <span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">makeXxx()<\/span><\/span> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">or some such to indicate its purpose. Sometimes the factory is the same as the class of the desired object.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">When the constructed class is the same as the factory, you&#8217;ll see a class structure like this:<\/span><\/div>\n<div><\/div>\n<div>class Xxx {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> \/\/ Factory methods<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public static Xxx makeSomeXxx() { &#8230;}<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public static Xxx makeAnotherXxx() { &#8230;}<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> \/\/ Protected constructor<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> protected Xxx() { &#8230;} <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> \/\/ Public methods<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void doSomething() { &#8230;}<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public int getValue() { &#8230;}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">(Note that the factory method need not return a &#8220;real&#8221; Xxx; often it will return an instance of a subclass of Xxx.)<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">One example of this is the Border object in Swing. There are many border types &#8211; etched, line, titled, etc. There is a BorderFactory object that knows how to return an instance of each.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Why this separation? For one thing, borders are mostly constant: once created they can&#8217;t be changed. So, the factory can keep track of what it has created. Once it has created the first instance of EtchedBorder, it saves it, and returns the saved value each time one is requested. (The client doesn&#8217;t care that it&#8217;s not a unique border object.) In this way, we avoid creating many unnecessary objects.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Recall that we used a factory method in our graph package. This let us avoid making clients depend on the actual Node and Edge types. Without the factory, we&#8217;d have client code like this:<\/span><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Node n1 = new SimpleNode();<\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">If we ever wanted to use a different node type, we&#8217;d have to find and change every constructor! By using the factory method, our code is like this:<\/span><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Graph graph = new SimpleGraph();<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Node n = graph.MakeNode();<\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">We still have to worry about the Graph constructor, but we&#8217;ll create many more nodes than graphs, and the nodes are now independent of their type.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use a Factory Method when you want to vary (hide!) the construction of objects.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD: returning a specialized subclass]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Adapter<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">You have one interface, but you need another. Use an Adapter to translate between them.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">For example, we might like the user to select an object, and then we&#8217;ll do something with the selection. What&#8217;s the user selecting from? It could be a list, tree, table, radio-button, etc. We don&#8217;t want to commit to a particular choice, so we have this interface:<\/span><\/div>\n<div> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public interface ObjectSelectionListener {<\/div>\n<div> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void objectSelected (ObjectSelectionEvent e);<\/div>\n<div> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The problem is: list, tree, etc. don&#8217;t know this interface &#8211; they were written before it even existed! To make a list able to notify us of object selections, we&#8217;ll introduce an Adapter. It will be a ListSelectionListener so it can work with Lists, and it will manage ObjectSelectionListeners so it can work with our interface.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD &#8211; check whether it&#8217;s list or ListModel]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div>public class ListToObjectAdapter implements ListSelectionListener {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> protected JList list;<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public ListToObjectAdapter (JList list) {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> this.list = list;<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> list.addSelectionListener(this);<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void valueChanged (ListSelectionEvent e) {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Event e2 = new ObjectSelectionEvent(list.getSelectedValue());<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> \/\/ for each listener &#8211; send e2<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Vector v = new Vector();<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void addObjectSelectionListener(ObjectSelectionListener listener)<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> v.addElement(listener);<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/div>\n<div><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void removeObjectSelectionListener(ObjectSelectionListener listener) {<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> v.removeElement(listener);<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/div>\n<div>}<\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">You might use it like this:<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> JList list = new JList(listModel);<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ListToObjectAdapter adapter = new ListToObjectAdapter(list);<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ObjectSelectionListener osl = new MyObjectDisplay();<\/div>\n<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> adapter.add(osl);<\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">When an event comes in to the adapter, it broadcasts to <i>its<\/i> listeners.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use an Adapter when you must match two interfaces that are conceptually related, but physically incompatible.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Facade<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD]<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Composite<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Objects often contain instances of their own class (recursively). When you notice such a structure, you can sometimes make use of the Composite pattern.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Parent<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Containers can contain containers, but eventually get to leaves.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^ |<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> |<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> | V<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> V<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;<\/span> Container<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Child<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The Java AWT hierarchy is like this:<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Component<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^<span>&nbsp;<\/span> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> |<span>&nbsp;<\/span> ihs<span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ihs<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;<\/span> Container<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Button etc.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">With Composite, there&#8217;s a decision about the containment capabilities: is Component aware of them or not? The original AWT says &#8220;Components are not containers.&#8221;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">In the new Swing library, they&#8217;ve reversed that decision<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Component<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^ ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;<\/span> Container<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Button etc still exist<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;<\/span> JComponent<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ^<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">JButton etc<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The Swing &#8220;Jxxx&#8221; components are all containers &#8211; they inherit from JComponent which inherits from Container.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Why the change? It makes some things a little easier. You can &#8220;atomize&#8221; components more. From the outside, you deal with a JButton as a single entity, but inside it is be composed of its inner area and a border, rather than being monolithic.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use Composite when you have a recursive structure.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Decorator<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Sometimes you have a recursive structure (perhaps a Composite), and you want to attach behavior all through the structure. The Decorator pattern tells how to provide this: make sure each object has a common interface and &#8220;wrap&#8221; the additional behavior around the original object. Usually the interface is cumulative in some sense.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">An example in Swing is the border class. (Borders are an extension\/replacement of Insets.) For example<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> &#8212;&#8212;-<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> matte<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Title<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Line<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> contents<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">There is a type of Border called a CompoundBorder, which layers two borders together (an outer and an inner border). The above example is represented like this:<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Compound Border<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> MatteBorder<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> CompoundBorder<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> TitledBorder<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> LineBorder<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Borders can report their sizes and draw themselves. A CompoundBorder is the same: its size is the sum of the two contained border sizes, and it draws itself by letting the borders draw themselves one inside the other.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD: more pure if all borders were compound]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Another example is the ScrollPane. In most ways, it acts like a panel containing a component. Drawing is done by drawing scrollbars (if needed) and then letting the the &#8220;contained&#8221; component draw itself.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use this pattern when you have a recursive structure of objects with compatible interfaces, and behavior that accumulates.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Template Method<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">You have an algorithm that is generally useful, but the details are particular to an implementation.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD capsule description]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">[TBD example &#8211; applet]<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">An example of this is the paint() method used by Component. The Component class has no idea how a particular component will be drawn, but the AWT knows that each will draw itself when paint() is called.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Component<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> =======<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <i>paint(Graphics g)<\/i><\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span>&nbsp;&nbsp;<\/span> &lt;&gt;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span>&nbsp;&nbsp;&nbsp;<\/span> |<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> MyComponent<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ==========<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> paint(Graphics g)<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">In some other class, there is code like this:<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> for each component c<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> if it&#8217;s visible<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span>&nbsp;&nbsp;&nbsp;<\/span> Create a graphics context g<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span>&nbsp;&nbsp;&nbsp;<\/span> c.paint(g)<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> end if<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> end for<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The idea can apply to a set of methods (rather than just one):<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <i>setup()<\/i><\/span><\/div>\n<div><i><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> process(Item e)<\/span><\/i><\/div>\n<div><i><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> cleanup()<\/span><\/i><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use this pattern when the basic algorithm is stable, but the individual steps change.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Strategy<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">You may have objects that are stable, but an algorithm that changes.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">For example, the AWT layout is like this. A container&#8217;s contents are separate from their layout. You can swap in a new layout manager without affecting how the contents are built.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Manager<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ======<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> arrange() { <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> &#8212;-&gt;<span>&nbsp;&nbsp;&nbsp;<\/span> Strategy<\/span><\/div>\n<div> <span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> defer to strategy<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> ABCStrategy<span>&nbsp;&nbsp;<\/span> DEFStrategy<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Another classic example is text layout. Your word processor may have an editing mode (where words are wrapped to fit the screen) and a print preview mode (where word breaks are balanced against page size). This can be implemented as a strategy.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use a strategy when the algorithm changes.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Observer<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">One of the best known patterns is the Observer pattern (also known as Model-View-Controller). This pattern decouples the update of a data from its display.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Suppose you have a data structure, and one or more displays (e.g., a text editor). When the data structure changes, you&#8217;d like to update all displays. If the data structure directly updates the display, there&#8217;s a problem: every time a new display is added, the data structure must be changed.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The Observer pattern breaks this dependency by splitting the model from its views. The model is coupled to the display only weakly: each display registers with the model to request notification when the model changes. The display gets the notice, and queries the model to figure out what changed.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The Observer pattern is embedded in the JDK 1.1 event model. For instance, a Button generates ActionEvent notifications. An ActionListener can register with the button, and when the button is clicked, the listener is notified.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> Button<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> MyActionListener<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> addActionListener (1)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> actionPerformed (2)<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Button b = new Button (&#8220;test&#8221;);<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">b.addActionListener (new ActionListener() {<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> public void actionPerformed (ActionEvent e) {<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> System.out.println(&#8220;Button clicked; event=&#8221;+e);<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> }<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">} );<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Notice that the button has no knowledge of the particulars of a listener: it just knows how to refer to one, and to call its actionPerformed() method.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use this pattern when you need to decouple classes.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<p><b><i><span style=\"font-family: Arial; font-size: 10pt;\"> <\/span><\/i><\/b><\/p>\n<h2><span style=\"font-size: 10pt;\">Command<\/span><\/h2>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Sometimes you need a function to act like an object. The Command pattern addresses this.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">For example, suppose you want to provide a camera simulator, to show the effect of lens filters. You might want the user to drag filters onto a stack, and then show the effect of the combination. You could use the Command pattern: make the filters be command objects, each one a function on the image&#8217;s appearance.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">In the Swing package, there is support for the Command pattern in the Action class and the undo package. The Action class encapsulates functions and objects. For example, an editor can give you a list of Actions, corresponding to functions such as copy or move-to-end-of-line.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">The undo package introduces a class UndoableEdit. Each UndoableEdit can be done or undone. These objects can be saved to a list, so undo\/redo can work by doing or undoing each of the actions in the list.<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\"> &nbsp;<\/span><\/div>\n<div><span style=\"font-family: &quot;Times New Roman&quot;; font-size: 10pt;\">Use this pattern to make a function work as an object.<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design patterns are micro-architectures that solve a particular design problem. See the class text Design Patterns. A pattern is a literary form that describes a problem, the forces surrounding it, and a solution that resolves those forces. &nbsp; The designs described here will influence your framework&#8217;s structure. They will also be at work in your [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-frameworks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Growing Frameworks: Patterns - Bill Wake&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/billwake.com\/growing-frameworks-patterns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Growing Frameworks: Patterns - Bill Wake&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Design patterns are micro-architectures that solve a particular design problem. See the class text Design Patterns. A pattern is a literary form that describes a problem, the forces surrounding it, and a solution that resolves those forces. &nbsp; The designs described here will influence your framework&#8217;s structure. They will also be at work in your [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/billwake.com\/growing-frameworks-patterns\/\" \/>\n<meta property=\"og:site_name\" content=\"Bill Wake&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"1999-06-04T04:05:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-01T20:55:32+00:00\" \/>\n<meta name=\"author\" content=\"Bill Wake\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bill Wake\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/\"},\"author\":{\"name\":\"Bill Wake\",\"@id\":\"https:\\\/\\\/billwake.com\\\/#\\\/schema\\\/person\\\/b551cc572ce5d025b081e031a73eec0c\"},\"headline\":\"Growing Frameworks: Patterns\",\"datePublished\":\"1999-06-04T04:05:00+00:00\",\"dateModified\":\"2020-08-01T20:55:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/\"},\"wordCount\":3774,\"commentCount\":0,\"articleSection\":[\"frameworks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/\",\"url\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/\",\"name\":\"Growing Frameworks: Patterns - Bill Wake&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/billwake.com\\\/#website\"},\"datePublished\":\"1999-06-04T04:05:00+00:00\",\"dateModified\":\"2020-08-01T20:55:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/billwake.com\\\/#\\\/schema\\\/person\\\/b551cc572ce5d025b081e031a73eec0c\"},\"breadcrumb\":{\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/billwake.com\\\/growing-frameworks-patterns\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/billwake.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Growing Frameworks: Patterns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/billwake.com\\\/#website\",\"url\":\"https:\\\/\\\/billwake.com\\\/\",\"name\":\"Bill Wake&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/billwake.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/billwake.com\\\/#\\\/schema\\\/person\\\/b551cc572ce5d025b081e031a73eec0c\",\"name\":\"Bill Wake\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g\",\"caption\":\"Bill Wake\"},\"url\":\"https:\\\/\\\/billwake.com\\\/author\\\/bill-wake\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Growing Frameworks: Patterns - Bill Wake&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/billwake.com\/growing-frameworks-patterns\/","og_locale":"en_US","og_type":"article","og_title":"Growing Frameworks: Patterns - Bill Wake&#039;s Blog","og_description":"Design patterns are micro-architectures that solve a particular design problem. See the class text Design Patterns. A pattern is a literary form that describes a problem, the forces surrounding it, and a solution that resolves those forces. &nbsp; The designs described here will influence your framework&#8217;s structure. They will also be at work in your [&hellip;]","og_url":"http:\/\/billwake.com\/growing-frameworks-patterns\/","og_site_name":"Bill Wake&#039;s Blog","article_published_time":"1999-06-04T04:05:00+00:00","article_modified_time":"2020-08-01T20:55:32+00:00","author":"Bill Wake","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Bill Wake","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/#article","isPartOf":{"@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/"},"author":{"name":"Bill Wake","@id":"https:\/\/billwake.com\/#\/schema\/person\/b551cc572ce5d025b081e031a73eec0c"},"headline":"Growing Frameworks: Patterns","datePublished":"1999-06-04T04:05:00+00:00","dateModified":"2020-08-01T20:55:32+00:00","mainEntityOfPage":{"@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/"},"wordCount":3774,"commentCount":0,"articleSection":["frameworks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/billwake.com\/growing-frameworks-patterns\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/","url":"http:\/\/billwake.com\/growing-frameworks-patterns\/","name":"Growing Frameworks: Patterns - Bill Wake&#039;s Blog","isPartOf":{"@id":"https:\/\/billwake.com\/#website"},"datePublished":"1999-06-04T04:05:00+00:00","dateModified":"2020-08-01T20:55:32+00:00","author":{"@id":"https:\/\/billwake.com\/#\/schema\/person\/b551cc572ce5d025b081e031a73eec0c"},"breadcrumb":{"@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/billwake.com\/growing-frameworks-patterns\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/billwake.com\/growing-frameworks-patterns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/billwake.com\/"},{"@type":"ListItem","position":2,"name":"Growing Frameworks: Patterns"}]},{"@type":"WebSite","@id":"https:\/\/billwake.com\/#website","url":"https:\/\/billwake.com\/","name":"Bill Wake&#039;s Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/billwake.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/billwake.com\/#\/schema\/person\/b551cc572ce5d025b081e031a73eec0c","name":"Bill Wake","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8e2b28fe375cac050dad2f141da886dde7ff7a1edc00116f801ec96cf3849fa1?s=96&d=mm&r=g","caption":"Bill Wake"},"url":"https:\/\/billwake.com\/author\/bill-wake\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":1,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/posts\/18\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/billwake.com\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}