<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vcorrei</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vcorrei"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vcorrei"/>
	<updated>2026-08-08T20:36:16Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70052</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70052"/>
		<updated>2012-11-18T03:41:40Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implements the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which object they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The 'new' operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype.&amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;http://java.dzone.com/articles/intro-design-patterns-prototype&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the [http://www.dotnetlead.com/design-patterns/prototype prototype design pattern] comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] for the prototype design pattern on the right:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;http://www.codeproject.com/Articles/185348/Prototype-Design-Pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; ]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:These example have been taken from &amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes &amp;lt;ref name=&amp;quot;differences&amp;quot;&amp;gt;http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=prototypePattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the [http://en.wikipedia.org/wiki/Composite_pattern Composite pattern], to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Abstract_factory_pattern The Abstract Factory], Prototype, or [http://en.wikipedia.org/wiki/Builder_pattern Builder patterns], which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory &amp;lt;ref name=&amp;quot;msdn&amp;quot;&amp;gt;http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-05.aspx&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70051</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70051"/>
		<updated>2012-11-18T03:37:11Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implements the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which object they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The 'new' operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype.&amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;http://java.dzone.com/articles/intro-design-patterns-prototype&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the [http://www.dotnetlead.com/design-patterns/prototype prototype design pattern] comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] for the prototype design pattern on the right:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;http://www.codeproject.com/Articles/185348/Prototype-Design-Pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; ]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:These example have been taken from &amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes &amp;lt;ref name=&amp;quot;differences&amp;quot;&amp;gt;http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=prototypePattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the [http://en.wikipedia.org/wiki/Composite_pattern Composite pattern], to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Abstract_factory_pattern The Abstract Factory], Prototype, or [http://en.wikipedia.org/wiki/Builder_pattern Builder patterns], which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory &amp;lt;ref name=&amp;quot;msdn&amp;quot;&amp;gt;http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-05.aspx&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70050</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70050"/>
		<updated>2012-11-18T03:36:44Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implements the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which object they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype.&amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;http://java.dzone.com/articles/intro-design-patterns-prototype&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the [http://www.dotnetlead.com/design-patterns/prototype prototype design pattern] comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] for the prototype design pattern on the right:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;http://www.codeproject.com/Articles/185348/Prototype-Design-Pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; ]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:These example have been taken from &amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes &amp;lt;ref name=&amp;quot;differences&amp;quot;&amp;gt;http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=prototypePattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the [http://en.wikipedia.org/wiki/Composite_pattern Composite pattern], to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Abstract_factory_pattern The Abstract Factory], Prototype, or [http://en.wikipedia.org/wiki/Builder_pattern Builder patterns], which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory &amp;lt;ref name=&amp;quot;msdn&amp;quot;&amp;gt;http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-05.aspx&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70049</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=70049"/>
		<updated>2012-11-18T03:32:23Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implements the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype.&amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;http://java.dzone.com/articles/intro-design-patterns-prototype&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the [http://www.dotnetlead.com/design-patterns/prototype prototype design pattern] comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] for the prototype design pattern on the right:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;http://www.codeproject.com/Articles/185348/Prototype-Design-Pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; ]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead &amp;lt;ref name=&amp;quot;codeprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:These example have been taken from &amp;lt;ref name=&amp;quot;javaprototype&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes &amp;lt;ref name=&amp;quot;differences&amp;quot;&amp;gt;http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=prototypePattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the [http://en.wikipedia.org/wiki/Composite_pattern Composite pattern], to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Abstract_factory_pattern The Abstract Factory], Prototype, or [http://en.wikipedia.org/wiki/Builder_pattern Builder patterns], which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory &amp;lt;ref name=&amp;quot;msdn&amp;quot;&amp;gt;http://msdn.microsoft.com/en-us/library/orm-9780596527730-01-05.aspx&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69702</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69702"/>
		<updated>2012-11-16T23:59:25Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69701</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69701"/>
		<updated>2012-11-16T23:57:55Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69700</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69700"/>
		<updated>2012-11-16T23:57:32Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69699</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69699"/>
		<updated>2012-11-16T23:56:24Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69698</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69698"/>
		<updated>2012-11-16T23:55:54Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69697</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69697"/>
		<updated>2012-11-16T23:54:49Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69696</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69696"/>
		<updated>2012-11-16T23:54:23Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69695</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69695"/>
		<updated>2012-11-16T23:53:58Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
* prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
* AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
* GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
*copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
*Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
* Keep the number of classes in the system to a minimum.&lt;br /&gt;
* Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
* With the Composite pattern, to provide archiving.&lt;br /&gt;
* Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
* Flexibility is important.&lt;br /&gt;
* Objects can be extended in subclasses&lt;br /&gt;
* There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
* A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
* The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69692</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69692"/>
		<updated>2012-11-16T23:48:48Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: /* Difference between factory method pattern and prototype pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
* Hide concrete classes from the client.&lt;br /&gt;
* Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
&lt;br /&gt;
•	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Keep the number of classes in the system to a minimum.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
•	With the Composite pattern, to provide archiving.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
•	Flexibility is important.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Objects can be extended in subclasses&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
•	The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69691</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69691"/>
		<updated>2012-11-16T23:46:46Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of [http://en.wikipedia.org/wiki/Creational_pattern creational pattern].  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML diagram] on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''AnimalClient.java'''&lt;br /&gt;
&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
 Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
 animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
 animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
 animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
 for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
 System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
 } // for [13]&lt;br /&gt;
&lt;br /&gt;
===Difference between factory method pattern and prototype pattern===&lt;br /&gt;
&lt;br /&gt;
The prototype pattern and the factory pattern are very similar in both intent and functionality. Both are creational patterns that will create objects of some interface without needing to specifically know the underlying class types. &lt;br /&gt;
&lt;br /&gt;
The main difference between the two patterns however involves how objects are constructed. The factory pattern will, generally, construct an object using the same construction parameters each time. Each object will be initialized with the same state information and be roughly equivalent to each other. The prototype pattern, on the other hand, can use any clone able object that is given to it — even if those objects are of the same class type but with different state information assigned to them. Each object then becomes the prototype or template for any objects cloned from them. An example can demonstrate this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public void factoryDoSomething(Factory factory) {&lt;br /&gt;
    Point pt = factory.createPoint();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public void prototypeDoSomething(Point prototype) {&lt;br /&gt;
    Point pt = (Point) prototype.clone();&lt;br /&gt;
    ... do something with the point ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You will notice that in the factoryDoSomething method, the point that is created is initialized in the same way and cannot be customized1. The prototypeDoSomething method can create a point from any other point with any type of state assigned to it. We could have called it with a &amp;quot;new Point(23,85)&amp;quot; or a &amp;quot;new Point(2929,59483)&amp;quot; and the cloned object would have similar state to those prototypes. The, ahem, point of the matter is that we can customize the state of the objects that will be created by the prototype pattern. &lt;br /&gt;
&lt;br /&gt;
Another major difference between the two patterns is the type of classes that can be created. A factory pattern will know (either through a registry or through sub classing) the various class types that can be created. The prototype pattern is not restricted to this as long as the object can be cloned. This allows the cloning of objects that are loaded dynamically from a class loader or ddl library or some other source where the class of the object is unknown either to the application or the component itself. &lt;br /&gt;
&lt;br /&gt;
Lastly, a factory pattern can handle a limited set of class types efficiently but gets bogged down as the number of types increase. The more types a factory pattern can create, the more cumbersome the factory pattern becomes from the overhead of managing those classes (both in the factory itself and in creating the necessary supporting classes). If the classes that are managed by the factory pattern are a hierarchy of classes for any given implementation, that hierarchy is generally repeated for each type regardless of how similar each class is to any other implementation. If the factory pattern uses a registry of some sort, the registry becomes harder to manage and certainly consumes more resources. On the other hand, the prototype pattern does not suffer from these issues since the pattern is focused on an existing object. The pattern is very scalable as the types increase because it has no management or overhead associated to it. If a class hierarchy is involved for each implementation, the pattern becomes very efficient because it can reduce the number of redundant classes involved since any class can be simply cloned (in other words, you can mix and match classes from different types where applicable). &lt;br /&gt;
&lt;br /&gt;
To sum up, a prototype pattern provides benefits over a factory pattern when the state of the objects should can be customized by the calling application, the class types are dynamically loaded or otherwise unknown or when there is a large number of class type implementations that potentially deal with a lot of similar classes [14].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Prototype pattern when'''&lt;br /&gt;
&lt;br /&gt;
You want to:&lt;br /&gt;
&lt;br /&gt;
•	Hide concrete classes from the client.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Add and remove new classes (via prototypes) at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Keep the number of classes in the system to a minimum.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Adapt to changing structures of data at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern:'''&lt;br /&gt;
&lt;br /&gt;
•	With the Composite pattern, to provide archiving.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Instead of the Factory Method pattern, when subclasses start proliferating.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Use the Factory Method pattern when'''&lt;br /&gt;
&lt;br /&gt;
•	Flexibility is important.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Objects can be extended in subclasses&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	A client delegates responsibilities to subclasses in parallel hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Consider using this pattern instead of:'''&lt;br /&gt;
&lt;br /&gt;
•	The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).The Prototype pattern to store a set of objects to clone from the abstract factory [15].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69683</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69683"/>
		<updated>2012-11-16T23:38:32Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming] concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
We have a look at the UML diagram for the prototype design pattern:&lt;br /&gt;
&lt;br /&gt;
[[File:protypeuml.PNG|650 px|thumb|right|UML Diagram for prototype pattern]]&lt;br /&gt;
&lt;br /&gt;
From the above diagram we can decipher the following things that are: PrototypeManager class is just a manager class that is used to add and retrieve prototypes by an index number; it has the following variable and methods:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• prototypeList variable: It is the collection that stores all the prototypes&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• AddPrototype method: Allows you to add a prototype to the collection and assigning it an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• GetPrototype method: Allows you to retrieve a prototype from the collection using an index number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The IPrototype interface specifies the methods that all prototype classes must implement. It has the Clone method that returns an IPrototype interface.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The ConcretePrototype class is the actual prototype class; it implements the IPrototype interface and has the following property and method:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	copyProperty variable holds the information that is prepopulated. If the variable value is changed then the new instances created will have the new value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Clone method will make a copy of itself and return it. If the copyProperty is a value type (such as int or string) then we can use shallow copy. If the copyProperty is a reference type (such as an object that contains other objects) then we go for deep copy of the variable.&lt;br /&gt;
&lt;br /&gt;
The key to this pattern is that you will create your first object with the expensive initializations, and then store the values as a prototype in the repository. When you need create the same object again you can just get the copy of the prototype from the repository with all the values already populated. This reduces the performance overhead [12].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java Sample code to show prototype design pattern===&lt;br /&gt;
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses namely Sheep and Chicken. TheAnimalCreator class contains references to the two concrete prototypes. During the initialization of theAnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.&lt;br /&gt;
&lt;br /&gt;
'''Animal.java'''&lt;br /&gt;
&lt;br /&gt;
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.&lt;br /&gt;
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
 public Animal clone() {  &lt;br /&gt;
 Animal clonedAnimal = null;&lt;br /&gt;
 try {  &lt;br /&gt;
 clonedAnimal = (Animal) super.clone(); &lt;br /&gt;
 clonedAnimal.setDescription(description);  &lt;br /&gt;
 clonedAnimal.setNumberOfLegs(numberOfLegs);&lt;br /&gt;
 clonedAnimal.setName(name);&lt;br /&gt;
 } catch (CloneNotSupportedException e) {&lt;br /&gt;
 e.printStackTrace();   &lt;br /&gt;
 } // catch &lt;br /&gt;
 return clonedAnimal;   &lt;br /&gt;
 } // method clone&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Sheep.java'''&lt;br /&gt;
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Sheep extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''Chicken.java'''&lt;br /&gt;
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public class Chicken extends Animal {&lt;br /&gt;
&lt;br /&gt;
'''AnimalCreator.java'''&lt;br /&gt;
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the &amp;quot;Prototype&amp;quot; pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
 public Animal retrieveAnimal(String kindOfAnimal) {&lt;br /&gt;
 if (&amp;quot;Chicken&amp;quot;.equals(kindOfAnimal)) {   &lt;br /&gt;
 return (Animal) chicken.clone();   &lt;br /&gt;
 } else if (&amp;quot;Sheep&amp;quot;.equals(kindOfAnimal)) { &lt;br /&gt;
 return (Animal) sheep.clone(); &lt;br /&gt;
 } // if &lt;br /&gt;
 return null;&lt;br /&gt;
 } // method retrieveAnimal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
AnimalClient.java&lt;br /&gt;
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of typeAnimal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.&lt;br /&gt;
Code:&lt;br /&gt;
01.AnimalCreator animalCreator = new AnimalCreator();  &lt;br /&gt;
02.Animal[] animalFarm = new Animal[8];  &lt;br /&gt;
03.animalFarm[0] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);&lt;br /&gt;
04.animalFarm[1] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
05.animalFarm[2] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
06.animalFarm[3] = animalCreator.retrieveAnimal(&amp;quot;Chicken&amp;quot;);   &lt;br /&gt;
07.animalFarm[4] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
08.animalFarm[5] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
09.animalFarm[6] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
10.animalFarm[7] = animalCreator.retrieveAnimal(&amp;quot;Sheep&amp;quot;); &lt;br /&gt;
11.for (int i= 0; i&amp;lt;=7; i++) { &lt;br /&gt;
12.System.out.println(animalFarm[i].helloAnimal());   &lt;br /&gt;
13.} // for&lt;br /&gt;
&lt;br /&gt;
[13]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69677</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69677"/>
		<updated>2012-11-16T23:26:11Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;userpagestemp&amp;quot; /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69676</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69676"/>
		<updated>2012-11-16T23:23:46Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;ootemplate&amp;quot; /&amp;gt;  ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69675</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69675"/>
		<updated>2012-11-16T23:21:38Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
 ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69674</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69674"/>
		<updated>2012-11-16T23:19:44Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create. &amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from &amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same.&amp;lt;ref name=&amp;quot;ootemplate&amp;quot;&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Template method pattern is a behavioural design pattern.&amp;lt;ref name=&amp;quot;wikitemplate&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt; Template method pattern helps to avoid code duplication and aids in code reuse.&amp;lt;ref name=&amp;quot;userpagestemp&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69673</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69673"/>
		<updated>2012-11-16T23:11:57Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69672</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69672"/>
		<updated>2012-11-16T23:09:47Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.&amp;lt;ref name=&amp;quot;sourcemakingfact&amp;quot;&amp;gt;http://sourcemaking.com/design_patterns/factory_method&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69670</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69670"/>
		<updated>2012-11-16T23:06:13Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;userpagesfactory&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69669</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69669"/>
		<updated>2012-11-16T23:05:44Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69668</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69668"/>
		<updated>2012-11-16T23:02:07Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69667</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69667"/>
		<updated>2012-11-16T23:00:47Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.&amp;lt;ref name=&amp;quot;web&amp;quot;&amp;gt;http://www.oodesign.com/factory-pattern.html&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;br /&gt;
&lt;br /&gt;
===Prototype Design Pattern===&lt;br /&gt;
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object.&lt;br /&gt;
The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11].&lt;br /&gt;
This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69665</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69665"/>
		<updated>2012-11-16T22:55:45Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69664</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69664"/>
		<updated>2012-11-16T22:54:57Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HtmlTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public HtmlTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an HTML text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new HtmlTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header HTML text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header HTML text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69663</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69663"/>
		<updated>2012-11-16T22:53:31Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class PlainTextDocumentBuilder extends TextDocument {&lt;br /&gt;
     public PlainTextDocumentBuilder(InputStream in){&lt;br /&gt;
     //check that it is an Plain text document or throw error&lt;br /&gt;
     }&lt;br /&gt;
     public Document createDocument()&lt;br /&gt;
     {&lt;br /&gt;
      return new PlainTextDocument();&lt;br /&gt;
      }	&lt;br /&gt;
     public void printTextHeader () {&lt;br /&gt;
       // Code for header plain text header here.&lt;br /&gt;
     }&lt;br /&gt;
     public void printTextFooter () {&lt;br /&gt;
       // Code for header plain text footer here.&lt;br /&gt;
     }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69662</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69662"/>
		<updated>2012-11-16T22:51:56Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69661</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69661"/>
		<updated>2012-11-16T22:51:22Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;br /&gt;
&lt;br /&gt;
 public abstract class TextDocument {&lt;br /&gt;
&lt;br /&gt;
         &lt;br /&gt;
     public final void printPage () {&lt;br /&gt;
       Document document = createDocument();&lt;br /&gt;
       document.printTextHeader();&lt;br /&gt;
       System.out.println(document.body());&lt;br /&gt;
       document.printTextFooter();&lt;br /&gt;
     }&lt;br /&gt;
     public abstract Document createDocument();&lt;br /&gt;
     public abstract void printTextHeader();&lt;br /&gt;
     public abstract void printTextFooter();&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69660</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69660"/>
		<updated>2012-11-16T22:48:50Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [10]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69659</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69659"/>
		<updated>2012-11-16T22:39:53Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations .&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69658</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69658"/>
		<updated>2012-11-16T22:38:44Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Factory Method Pattern ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram for Template Method Pattern ===&lt;br /&gt;
[[File:Template_method_implementation_-_uml_class_diagram.gif|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Template_method_implementation_-_uml_class_diagram.gif&amp;diff=69657</id>
		<title>File:Template method implementation - uml class diagram.gif</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Template_method_implementation_-_uml_class_diagram.gif&amp;diff=69657"/>
		<updated>2012-11-16T22:37:43Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: UML diagram for Template Method Pattern&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;UML diagram for Template Method Pattern&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69656</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69656"/>
		<updated>2012-11-16T22:34:54Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;br /&gt;
&lt;br /&gt;
=== Template Pattern ===&lt;br /&gt;
Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour.  In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69655</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69655"/>
		<updated>2012-11-16T22:33:21Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;br /&gt;
In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69654</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69654"/>
		<updated>2012-11-16T22:32:16Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;br /&gt;
This example has been taken from [4]&lt;br /&gt;
 public interface ImageReader {&lt;br /&gt;
    public DecodedImage getDecodedImage();&lt;br /&gt;
 }&lt;br /&gt;
 public class GifReader implements ImageReader {&lt;br /&gt;
    public GifReader( InputStream in ) {&lt;br /&gt;
        // check that it's a gif, throw exception if it's not, then if it is decode it.&lt;br /&gt;
    }&lt;br /&gt;
 public DecodedImage getDecodedImage() {&lt;br /&gt;
       return decodedImage;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 public class JpegReader implements ImageReader {&lt;br /&gt;
    //...&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69649</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69649"/>
		<updated>2012-11-16T22:26:17Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code Example ===&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69648</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69648"/>
		<updated>2012-11-16T22:25:14Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;br /&gt;
In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69647</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69647"/>
		<updated>2012-11-16T22:24:26Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69646</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69646"/>
		<updated>2012-11-16T22:23:17Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
&lt;br /&gt;
=== UML Diagram ===&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69645</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69645"/>
		<updated>2012-11-16T22:21:30Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
[[File:Factoryuml.JPG|650 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69643</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69643"/>
		<updated>2012-11-16T22:21:02Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
[[File:Factoryuml.JPG|500 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69642</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69642"/>
		<updated>2012-11-16T22:20:45Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;br /&gt;
[[File:Factoryuml.JPG|200 px|thumb|right|UML Diagram for factory method pattern ]]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Factoryuml.JPG&amp;diff=69641</id>
		<title>File:Factoryuml.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Factoryuml.JPG&amp;diff=69641"/>
		<updated>2012-11-16T22:18:12Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: This is a UML diagram of Factory pattern&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is a UML diagram of Factory pattern&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69638</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69638"/>
		<updated>2012-11-16T22:14:39Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Factory Method'''&lt;br /&gt;
&lt;br /&gt;
Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.[1]Factory method is thus a  type of creational pattern.  An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client. [7]&lt;br /&gt;
&lt;br /&gt;
The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]&lt;br /&gt;
&lt;br /&gt;
Use of factory method pattern makes the code  more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69627</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69627"/>
		<updated>2012-11-16T22:01:56Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Factory Method'''&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69626</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69626"/>
		<updated>2012-11-16T21:59:56Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Factory Method pattern and the related patterns (Template, Prototype)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69625</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w36 av</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w36_av&amp;diff=69625"/>
		<updated>2012-11-16T21:56:30Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: Created page with &amp;quot;hjhfukhfhu&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;hjhfukhfhu&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_aa&amp;diff=69623</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_aa&amp;diff=69623"/>
		<updated>2012-11-16T21:49:55Z</updated>

		<summary type="html">&lt;p&gt;Vcorrei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Factory'''&lt;br /&gt;
----&lt;br /&gt;
''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
==Interface Segregation Principle (ISP)==&lt;br /&gt;
&lt;br /&gt;
The Interface Segregation Principle states that ''&amp;quot;Clients should not be forced to depend upon interfaces that they do not use&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
==ISP in OO Design==&lt;br /&gt;
&lt;br /&gt;
When clients are forced to depend upon interfaces that they don’t use, then those clients are subject to changes to those interfaces. This results in an inadvertent coupling between all the clients. Said another way, when a client depends upon a class that contains interfaces that the client does not use, but that other clients do use, then that client will be affected by the changes that those other clients force upon the class. This relationship is illustrated below, where Fig.1 indicates OO Design without ISP and Fig.2 indicates OO Design with ISP. We would like to avoid such couplings where possible, and so we want to separate the interfaces where possible. ISP is about high cohesion in the interface level. It is often referred to as poor man's Single Responsibility Principle (SRP) because SRP deals with cohesion at the class level.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:isp.jpg|thumb|left|300x350px|Fig.1) Design without ISP]] [[Image:isp2.jpg|thumbnail|none|425x420px|Fig.2) Design with ISP]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Without ISP, the OO Design may contain fat interfaces. &amp;quot;Fat&amp;quot; interfaces arise from classes whose member methods can be broken into two or more groups. Each group of methods in turn serves a different set of clients. The Interface Segregation Principle suggests that client should not know about the method groups as a single class. Instead, clients should know about abstract base classes, one for each method group, which have cohesive interfaces. Fat interfaces lead to inadvertent couplings between clients that ought otherwise to be isolated. By making use of the ADAPTER pattern, either through delegation (object form) or multiple inheritance (class form), fat interfaces can be segregated into abstract base classes that break the unwanted coupling between clients.&lt;br /&gt;
&lt;br /&gt;
The ISP acknowledges that there are objects that require non-cohesive interfaces; however it suggests that clients should not know about them as a single class. Instead, clients should know about abstract base classes that have cohesive interfaces. Some languages&lt;br /&gt;
refer to these abstract base classes as “interfaces”, “protocols” or “signatures”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Examples of ISP==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
The following example is a very easy to understand example of how ISP can be employed in a real world system design.&lt;br /&gt;
&lt;br /&gt;
Consider a cell phone had interfaces of a phone and a MP3 player. ISP advises to keep these two interfaces completely independent of each other. This way the phone can be treated purely as a phone or as a MP3 player. Programmatically,use only the interface of a MP3 player in an implemented method called playSong. In future even if the interface for the phone is modified, the method playSong would remain the same[3].&lt;br /&gt;
&lt;br /&gt;
This example can be used to give a general overview of ISP, without going into much detail of how ISP is used in making complex OO design decision. &lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The following example is best example that shows how ISP is used to deal with fat interfaces. In the illustrations below, UML (Unified Modeling Language) has been used to bring out the design decisions that have been made.&lt;br /&gt;
&lt;br /&gt;
Consider a security system. In this system there are Door objects that can be locked and unlocked, and which know whether they are open or closed. The Door class is abstract so that clients can use objects that conform to the Door interface, without having to depend upon particular implementations of Door. Now consider that one such implementation. TimedDoor needs to sound an alarm when the door has been left open for too long. In order to do this the TimedDoor object communicates with another object called a Timer.&lt;br /&gt;
&lt;br /&gt;
How can we get the TimerClient class to communicate with the TimedDoor class so that the code in the TimedDoor can be notified of the timeout? We force Door, and therefore TimedDoor, to inherit from TimerClient. This ensures that TimerClient can register itself with the Timer and receive the TimeOut message. &lt;br /&gt;
&lt;br /&gt;
Although this solution is common, it is not without problems. Chief among these is that the Door class now depends upon TimerClient. Not all varieties of Door need timing. Indeed, the original Door abstraction had nothing whatever to do with timing. If timingfree derivatives of Door are created, those derivatives will have to provide nil implementations for the TimeOut method. Moreover, the applications that use those derivatives will have to include the definition of the TimerClient class, even though it is not used. &lt;br /&gt;
&lt;br /&gt;
Figure 3 shows a common syndrome of object oriented design in statically typed languages like C++. This is the syndrome of interface pollution. The interface of Door has been polluted with an interface that it does not require. It has been forced to incorporate this interface solely for the benefit of one of its subclasses. If this practice is pursued, then every time a derivative needs a new interface, that interface will be added to the base class. This will further pollute the interface of the base class, making it “fat”&lt;br /&gt;
&lt;br /&gt;
Moreover, each time a new interface is added to the base class, that interface must be implemented (or allowed to default) in derived classes. Indeed, an associated practice is to add these interfaces to the base class as&lt;br /&gt;
nil virtual functions rather than pure virtual functions; specifically so that derived classes are not burdened with the need to implement them. As we learned in class, such a practice violates the Liskov Substitution principle (LSP), leading to maintenance and reusability problems.&lt;br /&gt;
&lt;br /&gt;
The solution to these problems are intriguing. The answer to this lies in the fact that clients of an object do not need to access it through the interface of the object. Rather, they can access it through delegation, or through a base class of the object.&lt;br /&gt;
&lt;br /&gt;
Figure 4 show how Multiple Inheritance can be used, in the class form of the ADAPTER pattern, to achieve the ISP. In this model, TimedDoor inherits from both Door and TimerClient. Although clients of both base classes can make use of TimedDoor, neither actually depend upon the TimedDoor class. Thus, they use the same object through separate interfaces [2].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:isp3.jpg|thumb|left|300x350px|Fig.3) Timed Door Design without ISP]]&lt;br /&gt;
[[Image:isp4.jpg|thumb|none|460x420px|Fig.4) Timed Door Design with ISP]]&lt;br /&gt;
&lt;br /&gt;
'''Out of all the four examples that are presented in this article we consider this the most suitable to be taught in class.&lt;br /&gt;
'''&lt;br /&gt;
===Example 3===&lt;br /&gt;
&lt;br /&gt;
The following example deals with the design of UI for a client-sever architecture. Consider a system in which the user has a choice of three types of UIs, namely GUI (Graphical User Interface), Touchpad and a Console. There is a central server that has to satisfy the request from clients, collects the display interfaces and displays customized result back to the user (Server class). In a system without ISP, addition of a new type of UI, will require a changes in the server interface. Furthermore, it would also require all the other UIs to be recompiled. If proper ISP is applied in the OO Design, new UIs can be added without requiring changes to the server's interfaces. Also the other UIs are not affected. The Fig.5 illustrates this example. &lt;br /&gt;
&lt;br /&gt;
The ISP are implemented by using an intermediary interfaces that interact with the server. These intermediary interfaces implement common functionality of each of the UIs, thus abstracting the actual implementation of the UIs from the server class. Hence new UIs can be added without requiring change to the server and other UIs[4].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:isp5.jpg|thumb|center|425x420px|Fig.5) UI Design ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Example 4===&lt;br /&gt;
&lt;br /&gt;
Consider a slightly more significant example. The traditional Automated Teller Machine (ATM) problem. The user interface of an ATM machine needs to be very flexible. The output may need to be translated into many different language. It may need to be presented on a screen, or on a braille tablet, or spoken out a speech synthesizer. Clearly this can be achieved by creating an abstract base class that has pure virtual functions for all the different messages that need to be presented by the interface. Fig.6 Represents a general UI design for the ATM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:ATM_UI.jpg|thumb|center|400x400px|Fig.6) ATM UI Design ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also consider that different transactions that the ATM can perform is encapsulated as a derivative of the class Transaction. Thus we might have classes such as DepositTransaction, WithdrawlTransaction, TransferTransaction, etc. Each of these objects issues message&lt;br /&gt;
to the UI. For example, the DepositTransaction object calls the RequestDepositAmount member function of the UI class. Whereas the transferTransaction object calls the RequestTransferAmount member function of UI. This corresponds to the diagram in Figure 7.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:ATM_TRN.jpg|thumb|center|400x400px|Fig.7) ATM TRANSACTION Design ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that this is precisely the situation that the ISP tells us to avoid. Each of the transactions is using a portion of the UI that no other object uses. This creates the possibility that changes to one of the derivatives of Transaction will force corresponding change to the UI, thereby affecting all the other derivatives of Transaction, and every other class that depends upon the UI interface.&lt;br /&gt;
&lt;br /&gt;
This unfortunate coupling can be avoided by segregating the UI interface into individual abstract base classes such as DepositUI, withdrawUI and TransferUI. These abstract base classes can then be multiply inherited into the final UI abstract class. Fig.8 show this model[5].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:segregation.jpg|thumb|center|400x400px|Fig.8) ATM SEGREGATION Design ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Catalog / Reference==&lt;br /&gt;
1. [http://www.objectmentor.com/resources/articles/isp.pdf objectMentor]&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html doodleproject]&amp;lt;br&amp;gt;&lt;br /&gt;
3. [http://ifacethoughts.net/2006/03/28/interface-segregation-principle/ ifacethoughts] &amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://www.everything2.com/index.pl?node_id=1259576 everything_oo]&amp;lt;br&amp;gt;&lt;br /&gt;
5. [http://www.cmcrossroads.com/articles/agile-cm-environments/principles-of-agile-version-control:-from-ood-to-tbd.html cmc_oodesign]&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://jayflowers.com/WordPress/?p=91 jayflowers]&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.cet.sunderland.ac.uk/~cs0her/COM379%20Lectures/Lecture3.ppt sunderland_technologies]&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.parlezuml.com/metrics/OO%20Design%20Principles%20&amp;amp;%20Metrics.pdf uml]&amp;lt;br&amp;gt;&lt;br /&gt;
9. [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html javaboutique]&amp;lt;br&amp;gt;&lt;br /&gt;
10.[http://www.ddj.com/blog/architectblog/archives/2006/06/interface_segre.html  arch_of_oo]&amp;lt;br&amp;gt;&lt;br /&gt;
11.[http://hocit.com/forum/showthread.php?p=13932 OOforum]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vcorrei</name></author>
	</entry>
</feed>