DesignPatterns

<html>
<body>
<div id="html-refcard">

	
		<meta charset="utf-8">
		<title>Design Patterns</title>
		
		<script type="text/javascript">
            var uagent = navigator.userAgent.toLowerCase();           
            if (uagent.search("android") > -1) {
              document.write('<link rel="stylesheet" href="../css/refcardz_html_android.css" type="text/css" media="screen">');
            }
        </script>
	
	
		<h1><span id="main_topic">Design</span> Patterns</h1>
		<p class="author_name">By Jason McDonald</p>
		<h2>About Design Patterns</h2>
		<p>This Design Patterns refcard provides a quick reference to the original 23 Gang of Four design patterns, as listed in the book Design Patterns: Elements of Reusable Object-Oriented Software. Each pattern includes class diagrams, explanation, usage information, and a real world example.</p>
		<ul>
			<li class="creation_patterns"><strong>Creational Patterns:</strong> Used to construct objects such that they can be decoupled from their implementing system.</li>
			<li class="structural_patterns"><strong>Structural Patterns:</strong> Used to form large object structures between many disparate objects.</li>
			<li class="behavioral_patterns"><strong>Behavioral Patterns:</strong> Used to manage algorithms, relationships, and responsibilities between objects.</li>
			<li>Object Scope: Deals with object relationships that can be changed at runtime.</li>
			<li>Class Scope: Deals with class relationships that can be changed at compile time.</li>
		</ul>
		<table>
			<tbody>
				<tr>
					<td>C</td>
					<td>Abstract Factory</td>
					<td>S</td>
					<td>Decorator</td>
					<td>C</td>
					<td>Prototype</td>
				</tr>
				<tr>
					<td>S</td>
					<td>Adapter</td>
					<td>S</td>
					<td>Facade</td>
					<td>S</td>
					<td>Proxy</td>
				</tr>
				<tr>
					<td>S</td>
					<td>Bridge</td>
					<td>C</td>
					<td>Factory Method</td>
					<td>B</td>
					<td>Observer</td>
				</tr>
				<tr>
					<td>C</td>
					<td>Builder</td>
					<td>S</td>
					<td>Flyweight</td>
					<td>C</td>
					<td>Singleton</td>
				</tr>
				<tr>
					<td>B</td>
					<td>Chain Of Responsibility</td>
					<td>B</td>
					<td>Interpreter</td>
					<td>B</td>
					<td>State</td>
				</tr>
				<tr>
					<td>B</td>
					<td>Command</td>
					<td>B</td>
					<td>Mediator</td>
					<td>B</td>
					<td>Template Method</td>
				</tr>
				<tr>
					<td>S</td>
					<td>Composite</td>
					<td>B</td>
					<td>Memento</td>
					<td>B</td>
					<td>Visitor</td>
				</tr>
			</tbody>
		</table>
		<h2>Chain Of Responsibility <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_chain.jpg" width="514" height="244" alt="Chain of responsibility diagram"></p>
		<h3>Purpose</h3>
		<p>Gives more than one object an opportunity to handle a request by linking receiving objects together.</p>
		<h4>Use When</h4>
		<ul>
			<li>Multiple objects may handle a request and the handler doesn't have to be a specific object.</li>
			<li>A set of objects should be able to handle a request with the handler determined at runtime.</li>
			<li>A request not being handled is an acceptable potential outcome.</li>
		</ul>
		<h4>Example</h4>
		<p>Exception handling in some languages implements this pattern. When an exception is thrown in a method the runtime checks to see if the method has a mechanism to handle the exception or if it should be passed up the call stack. When passed up the call stack the process repeats until code to handle the exception is encountered or until there are no more parent objects to hand the request to.</p>
		<h2>Command <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_command.jpg" width="508" height="187" alt="Command pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Encapsulates a request allowing it to be treated as an object. This allows the request to be handled in traditionally object based relationships such as queuing and callbacks.</p>
		<h4>Use When</h4>
		<ul>
			<li>You need callback functionality.</li>
			<li>Requests need to be handled at variant times or in variant orders.</li>
			<li>A history of requests is needed.</li>
			<li>The invoker should be decoupled from the object handling the invocation.</li>
		</ul>
		<h4>Example</h4>
		<p>Job queues are widely used to facilitate the asynchronous processing of algorithms. By utilizing the command pattern the functionality to be executed can be given to a job queue for processing without any need for the queue to have knowledge of the actual implementation it is invoking. The command object that is enqueued implements its particular algorithm within the confines of the interface the queue is expecting.</p>
		<h2>Interpreter <em>Class Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_interpreter.jpg" width="508" height="231" alt="Interpreter pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Defines a representation for a grammar as well as a mechanism to understand and act upon the grammar.</p>
		<h4>Use When</h4>
		<ul>
			<li>There is grammar to interpret that can be represented as large syntax trees.</li>
			<li>The grammar is simple.</li>
			<li>Efficiency is not important.</li>
			<li>Decoupling grammar from underlying expressions is desired.</li>
		</ul>
		<h4>Example</h4>
		<p>Text based adventures, wildly popular in the 1980's, provide a good example of this. Many had simple commands, such as "step down" that allowed traversal of the game. These commands could be nested such that it altered their meaning. For example, "go in" would result in a different outcome than "go up". By creating a hierarchy of commands based upon the command and the qualifier (non-terminal and terminal expressions) the application could easily map many command variations to a relating tree of actions.</p>
		<h2>Iterator <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_iterator.jpg" width="508" height="215" alt="Iterator pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for access to the elements of an aggregate object without allowing access to its underlying representation.</p>
		<h4>Use When</h4>
		<ul>
			<li>Access to elements is needed without access to the entire representation.</li>
			<li>Multiple or concurrent traversals of the elements are needed.</li>
			<li>A uniform interface for traversal is needed.</li>
			<li>Subtle differences exist between the implementation details of various iterators.</li>
		</ul>
		<h4>Example</h4>
		<p>The Java implementation of the iterator pattern allows users to traverse various types of data sets without worrying about the underlying implementation of the collection. Since clients simply interact with the iterator interface, collections are left to define the appropriate iterator for themselves. Some will allow full access to the underlying data set while others may restrict certain functionalities, such as removing items.</p>
		<h2>Mediator <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_mediator.jpg" width="508" height="223" alt="Mediator pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.</p>
		<h4>Use When</h4>
		<ul>
			<li>Communication between sets of objects is well defined and complex.</li>
			<li>Too many relationships exist and common point of control or communication is needed.</li>
		</ul>
		<h4>Example</h4>
		<p>Mailing list software keeps track of who is signed up to the mailing list and provides a single point of access through which any one person can communicate with the entire list. Without a mediator implementation a person wanting to send a message to the group would have to constantly keep track of who was signed up and who was not. By implementing the mediator pattern the system is able to receive messages from any point then determine which recipients to forward the message on to, without the sender of the message having to be concerned with the actual recipient list.</p>
		<h2>Memento <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_memento.jpg" width="508" height="239" alt="Memento pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for capturing and externalizing an object's internal state so that it can be restored later, all without violating encapsulation.</p>
		<h4>Use When</h4>
		<ul>
			<li>The internal state of an object must be saved and restored at a later time.</li>
			<li>Internal state cannot be exposed by interfaces without exposing implementation.</li>
			<li>Encapsulation boundaries must be preserved.</li>
		</ul>
		<h4>Example</h4>
		<p>Undo functionality can nicely be implemented using the memento pattern. By serializing and deserializing the state of an object before the change occurs we can preserve a snapshot of it that can later be restored should the user choose to undo the operation.</p>
		<h2>Observer <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_observer.jpg" width="508" height="267" alt="Observer pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Lets one or more objects be notified of state changes in other objects within the system.</p>
		<h4>Use When</h4>
		<ul>
			<li>State changes in one or more objects should trigger behavior in other objects</li>
			<li>Broadcasting capabilities are required.</li>
			<li>An understanding exists that objects will be blind to the expense of notification.</li>
		</ul>
		<h4>Example</h4>
		<p>This pattern can be found in almost every GUI environment. When buttons, text, and other fields are placed in applications the application typically registers as a listener for those controls. When a user triggers an event, such as clicking a button, the control iterates through its registered observers and sends a notification to each.</p>
		<h2>State <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_state.jpg" width="508" height="238" alt="State patterns diagram"></p>
		<h3>Purpose</h3>
		<p>Ties object circumstances to its behavior, allowing the object to behave in different ways based upon its internal state.</p>
		<h4>Use When</h4>
		<ul>
			<li>The behavior of an object should be influenced by its state.</li>
			<li>Complex conditions tie object behavior to its state.</li>
			<li>Transitions between states need to be explicit.</li>
		</ul>
		<h4>Example</h4>
		<p>An email object can have various states, all of which will change how the object handles different functions. If the state is "not sent" then the call to send() is going to send the message while a call to recallMessage() will either throw an error or do nothing. However, if the state is "sent" then the call to send() would either throw an error or do nothing while the call to recallMessage() would attempt to send a recall notification to recipients. To avoid conditional statements in most or all methods there would be multiple state objects that handle the implementation with respect to their particular state. The calls within the Email object would then be delegated down to the appropriate state object for handling.</p>
		<h2>Strategy <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_strategy.jpg" width="508" height="235" alt="Strategy pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Defines a set of encapsulated algorithms that can be swapped to carry out a specific behavior.</p>
		<h4>Use When</h4>
		<ul>
			<li>The only difference between many related classes is their behavior.</li>
			<li>Multiple versions or variations of an algorithm are required.</li>
			<li>Algorithms access or utilize data that calling code shouldn't be exposed to.</li>
			<li>The behavior of a class should be defined at runtime.</li>
			<li>Conditional statements are complex and hard to maintain.</li>
		</ul>
		<h4>Example</h4>
		<p>When importing data into a new system different validation algorithms may be run based on the data set. By configuring the import to utilize strategies the conditional logic to determine what validation set to run can be removed and the import can be decoupled from the actual validation code. This will allow us to dynamically call one or more strategies during the import.</p>
		<h2>Template Method <em>Class Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_template.jpg" width="508" height="180" alt="Template strategy pattern"></p>
		<h3>Purpose</h3>
		<p>Identifies the framework of an algorithm, allowing implementing classes to define the actual behavior.</p>
		<h4>Use When</h4>
		<ul>
			<li>A single abstract implementation of an algorithm is needed.</li>
			<li>Common behavior among subclasses should be localized to a common class.</li>
			<li>Parent classes should be able to uniformly invoke behavior in their subclasses.</li>
			<li>Most or all subclasses need to implement the behavior.</li>
		</ul>
		<h4>Example</h4>
		<p>A parent class, InstantMessage, will likely have all the methods required to handle sending a message. However, the actual serialization of the data to send may vary depending on the implementation. A video message and a plain text message will require different algorithms in order to serialize the data correctly. Subclasses of InstantMessage can provide their own implementation of the serialization method, allowing the parent class to work with them without understanding their implementation details.</p>
		<h2>Visitor <em>Object Behavioral</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_visitor.jpg" width="508" height="264" alt="Visitor pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for one or more operations to be applied to a set of objects at runtime, decoupling the operations from the object structure.</p>
		<h4>Use When</h4>
		<ul>
			<li>An object structure must have many unrelated operations performed upon it.</li>
			<li>The object structure can't change but operations performed on it can.</li>
			<li>Operations must be performed on the concrete classes of an object structure.</li>
			<li>Exposing internal state or operations of the object structure is acceptable.</li>
			<li>Operations should be able to operate on multiple object structures that implement the same interface sets.</li>
		</ul>
		<h4>Example</h4>
		<p>Calculating taxes in different regions on sets of invoices would require many different variations of calculation logic. Implementing a visitor allows the logic to be decoupled from the invoices and line items. This allows the hierarchy of items to be visited by calculation code that can then apply the proper rates for the region. Changing regions is as simple as substituting a different visitor.</p>
		<h2>Adapter <em>Class and Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_adapter.jpg" width="508" height="186" alt="Adapter pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Permits classes with disparate interfaces to work together by creating a common object by which they may communicate and interact.</p>
		<h4>Use When</h4>
		<ul>
			<li>A class to be used doesn't meet interface requirements.</li>
			<li>Complex conditions tie object behavior to its state.</li>
			<li>Transitions between states need to be explicit.</li>
		</ul>
		<h4>Example</h4>
		<p>A billing application needs to interface with an HR application in order to exchange employee data, however each has its own interface and implementation for the Employee object. In addition, the SSN is stored in different formats by each system. By creating an adapter we can create a common interface between the two applications that allows them to communicate using their native objects and is able to transform the SSN format in the process.</p>
		<h2>Bridge <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_bridge.jpg" width="480" height="214" alt="Bridge pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Defines an abstract object structure independently of the implementation object structure in order to limit coupling.</p>
		<h4>Use When</h4>
		<ul>
			<li>Abstractions and implementations should not be bound at compile time.</li>
			<li>Abstractions and implementations should be independently extensible.</li>
			<li>Changes in the implementation of an abstraction should have no impact on clients.</li>
			<li>Implementation details should be hidden from the client.</li>
		</ul>
		<h4>Example</h4>
		<p>The Java Virtual Machine (JVM) has its own native set of functions that abstract the use of windowing, system logging, and byte code execution but the actual implementation of these functions is delegated to the operating system the JVM is running on. When an application instructs the JVM to render a window it delegates the rendering call to the concrete implementation of the JVM that knows how to communicate with the operating system in order to render the window.</p>
		<h2>Composite <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_composite.jpg" width="480" height="267" alt="Composite pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Facilitates the creation of object hierarchies where each object can be treated independently or as a set of nested objects through the same interface.</p>
		<h4>Use When</h4>
		<ul>
			<li>Hierarchical representations of objects are needed.</li>
			<li>Objects and compositions of objects should be treated uniformly.</li>
		</ul>
		<h4>Example</h4>
		<p>Sometimes the information displayed in a shopping cart is the product of a single item while other times it is an aggregation of multiple items. By implementing items as composites we can treat the aggregates and the items in the same way, allowing us to simply iterate over the tree and invoke functionality on each item. By calling the getCost() method on any given node we would get the cost of that item plus the cost of all child items, allowing items to be uniformly treated whether they were single items or groups of items.</p>
		<h2>Decorator <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_decorator.jpg" width="480" height="234" alt="Decorator pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors.</p>
		<h4>Use When</h4>
		<ul>
			<li>Object responsibilities and behaviors should be dynamically modifiable.</li>
			<li>Concrete implementations should be decoupled from responsibilities and behaviors.</li>
			<li>Subclassing to achieve modification is impractical or impossible.</li>
			<li>Specific functionality should not reside high in the object hierarchy.</li>
			<li>A lot of little objects surrounding a concrete implementation is acceptable.</li>
		</ul>
		<h4>Example</h4>
		<p>Many businesses set up their mail systems to take advantage of decorators. When messages are sent from someone in the company to an external address the mail server decorates the original message with copyright and confidentiality information. As long as the message remains internal the information is not attached. This decoration allows the message itself to remain unchanged until a runtime decision is made to wrap the message with additional information.</p>
		<h2>Facade <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_facade.jpg" width="480" height="190" alt="Facade pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Supplies a single interface to a set of interfaces within a system.</p>
		<h4>Use When</h4>
		<ul>
			<li>A simple interface is needed to provide access to a complex system.</li>
			<li>There are many dependencies between system implementations and clients.</li>
			<li>Systems and subsystems should be layered.</li>
		</ul>
		<h4>Example</h4>
		<p>By exposing a set of functionalities through a web service the client code needs to only worry about the simple interface being exposed to them and not the complex relationships that may or may not exist behind the web service layer. A single web service call to update a system with new data may actually involve communication with a number of databases and systems, however this detail is hidden due to the implementation of the façade pattern.</p>
		<h2>Fly Weight <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_flyweight.jpg" width="480" height="231" alt="Fly Weight pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Facilitates the reuse of many fine grained objects, making the utilization of large numbers of objects more efficient.</p>
		<h4>Use When</h4>
		<ul>
			<li>Many like objects are used and storage cost is high.</li>
			<li>The majority of each object's state can be made extrinsic.</li>
			<li>A few shared objects can replace many unshared ones.</li>
			<li>The identity of each object does not matter.</li>
		</ul>
		<h4>Example</h4>
		<p>Systems that allow users to define their own application flows and layouts often have a need to keep track of large numbers of fields, pages, and other items that are almost identical to each other. By making these items into flyweights all instances of each object can share the intrinsic state while keeping the extrinsic state separate. The intrinsic state would store the shared properties, such as how a textbox looks, how much data it can hold, and what events it exposes. The extrinsic state would store the unshared properties, such as where the item belongs, how to react to a user click, and how to handle events.</p>
		<h2>Proxy <em>Object Structural</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_proxy.jpg" width="494" height="205" alt="Proxy pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for object level access control by acting as a pass through entity or a placeholder object.</p>
		<h4>Use When</h4>
		<ul>
			<li>The object being represented is external to the system.</li>
			<li>Objects need to be created on demand.</li>
			<li>Access control for the original object is required.</li>
			<li>Added functionality is required when an object is accessed.</li>
		</ul>
		<h4>Example</h4>
		<p>Ledger applications often provide a way for users to reconcile their bank statements with their ledger data on demand, automating much of the process. The actual operation of communicating with a third party is a relatively expensive operation that should be limited. By using a proxy to represent the communications object we can limit the number of times or the intervals the communication is invoked. In addition, we can wrap the complex instantiation of the communication object inside the proxy class, decoupling calling code from the implementation details.</p>
		<h2>Abstract Factory <em>Object Creational</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_abstract_factory.jpg" width="494" height="232" alt="Abstract Factory pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Provide an interface that delegates creation calls to one or more concrete classes in order to deliver specific objects.</p>
		<h4>Use When</h4>
		<ul>
			<li>The creation of objects should be independent of the system utilizing them.</li>
			<li>Systems should be capable of using multiple families of objects.</li>
			<li>Families of objects must be used together.</li>
			<li>Libraries must be published without exposing implementation details.</li>
			<li>Concrete classes should be decoupled from clients.</li>
		</ul>
		<h4>Example</h4>
		<p>Email editors will allow for editing in multiple formats including plain text, rich text, and <abbr title="Hypertext Markup Language">HTML</abbr> Depending on the format being used, different objects will need to be created. If the message is plain text then there could be a body object that represented just plain text and an attachment object that simply encrypted the attachment into Base64. If the message is HTML then the body object would represent HTML encoded text and the attachment object would allow for inline representation and a standard attachment. By utilizing an abstract factory for creation we can then ensure that the appropriate object sets are created based upon the style of email that is being sent.</p>
		<h2>Builder <em>Object Creational</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_builder.jpg" width="494" height="190" alt="Builder pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Allows for the dynamic creation of objects based upon easily interchangeable algorithms.</p>
		<h4>Use When</h4>
		<ul>
			<li>Object creation algorithms should be decoupled from the system.</li>
			<li>Multiple representations of creation algorithms are required.</li>
			<li>The addition of new creation functionality without changing the core code is necessary.</li>
			<li>Runtime control over the creation process is required.</li>
		</ul>
		<h4>Example</h4>
		<p>A file transfer application could possibly use many different protocols to send files and the actual transfer object that will be created will be directly dependent on the chosen protocol. Using a builder we can determine the right builder to use to instantiate the right object. If the setting is <abbr title="File Transfer Protocol">FTP</abbr> then the FTP builder would be used when creating the object.</p>
		<h2>Factory Method <em>Object Creational</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_factory.jpg" width="494" height="180" alt="Factory pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Exposes a method for creating objects, allowing subclasses to control the actual creation process.</p>
		<h4>Use When</h4>
		<ul>
			<li>A class will not know what classes it will be required to create.</li>
			<li>Subclasses may specify what objects should be created.</li>
			<li>Parent classes wish to defer creation to their subclasses.</li>
		</ul>
		<h4>Example</h4>
		<p>Many applications have some form of user and group structure for security. When the application needs to create a user it will typically delegate the creation of the user to multiple user implementations. The parent user object will handle most operations for each user but the subclasses will define the factory method that handles the distinctions in the creation of each type of user. A system may have AdminUser and StandardUser objects each of which extend the User object. The AdminUser object may perform some extra tasks to ensure access while the StandardUser may do the same to limit access.</p>
		<h2>Prototype <em>Object Creational</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_prototype.jpg" width="494" height="205" alt="Prototype pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Create objects based upon a template of an existing objects through cloning.</p>
		<h4>Use When</h4>
		<ul>
			<li>Composition, creation, and representation of objects should be decoupled from a system.</li>
			<li>Classes to be created are specified at runtime.</li>
			<li>A limited number of state combinations exist in an object.</li>
			<li>Objects or object structures are required that are identical or closely resemble other existing objects or object structures.</li>
			<li>The initial creation of each object is an expensive operation.</li>
		</ul>
		<h4>Example</h4>
		<p>Rates processing engines often require the lookup of many different configuration values, making the initialization of the engine a relatively expensive process. When multiple instances of the engine is needed, say for importing data in a multi-threaded manner, the expense of initializing many engines is high. By utilizing the prototype pattern we can ensure that only a single copy of the engine has to be initialized then simply clone the engine to create a duplicate of the already initialized object. The added benefit of this is that the clones can be streamlined to only include relevant data for their situation.</p>
		<h2>Singleton <em>Object Creational</em></h2>
		<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/ptn_singleton.jpg" width="502" height="157" alt="Singleton pattern diagram"></p>
		<h3>Purpose</h3>
		<p>Ensures that only one instance of a class is allowed within a system.</p>
		<h4>Use When</h4>
		<ul>
			<li>Exactly one instance of a class is required.</li>
			<li>Controlled access to a single object is necessary.</li>
		</ul>
		<h4>Example</h4>
		<p>Most languages provide some sort of system or environment object that allows the language to interact with the native operating system. Since the application is physically running on only one operating system there is only ever a need for a single instance of this system object. The singleton pattern would be implemented by the language runtime to ensure that only a single copy of the system object is created and to ensure only appropriate processes are allowed access to it.</p>
		<div id="author_bio_book">
			<div id="author_bio">		
				<h3>About The Author</h3>
				<p><img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/author.jpg" alt="Photo of author Jason McDonald" width="78" height="94" id="author_img"></p>
				<h4>Jason McDonald</h4>
				<p>The product of two computer programmers, Jason McDonald wrote his first application in BASIC while still in elementary school and has been heavily involved in software ever since. He began his software career when he found himself automating large portions of one of his first jobs. Finding his true calling, he quit the position and began working as a software engineer for various small companies where he was responsible for all aspects of applications, from initial design to support. He has roughly 11 years of experience in the software industry and many additional years of personal software experience during which he has done everything from coding to architecture to leading and managing teams of engineers. Through his various positions he has been exposed to design patterns and other architectural concepts for years. Jason is the founder of the Charleston SC Java Users Group and is currently working to help found a Charleston chapter of the International Association of Software Architects.</p>
				<h5>Blogs</h5>
				<ul>
					<li>Personal Blog<a href="http://www.mcdonaldland.info/">http://www.mcdonaldland.info/</a></li>
					<li>Projects<a href="http://www.charlestonscjug.org/">Charleston SC Java Users Group</a></li>
				</ul>
			</div>
			<div id="suggested_book">
				<h3>Recommended Book</h3>
				<img src="http://refcardz.dzone.com/sites/all/modules/dzone/assets/refcardz/008/images/design_patterns.jpg" alt="Book cover of Design Patterns" width="104" height="131" id="recommended_book_cover">
				<p>Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.</p>
				<hr>
				<div id="purchase">
					<p id="buy_book">BUY NOW</p>
					<p><a href="http://books.dzone.com/books/designpatterns" title="Buy the book now.">books.dzone.com/books/designpatterns</a></p>
				</div>
			</div>
		</div>
	
</div>
</body>
</html>