<?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=Kal+el</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=Kal+el"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Kal_el"/>
	<updated>2026-10-04T12:03:53Z</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_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28826</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28826"/>
		<updated>2009-11-18T23:00:26Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* What is factory method? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Examples of Factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
===Limitations of factory pattern===&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28816</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28816"/>
		<updated>2009-11-18T22:54:10Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory pattern implementation in programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Examples of Factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
===Limitations of factory pattern===&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28815</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28815"/>
		<updated>2009-11-18T22:53:38Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* More Examples of factory pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Factory pattern implementation in programming languages===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
===Limitations of factory pattern===&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28814</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28814"/>
		<updated>2009-11-18T22:52:26Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
===Limitations of factory pattern===&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28813</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28813"/>
		<updated>2009-11-18T22:49:56Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Limitations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations of factory pattern==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28812</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28812"/>
		<updated>2009-11-18T22:48:28Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.It provides customization hooks. When the objects are created directly inside the class it's hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.The factory has to be used for a family of objects. If the classes doesn't extend common base class or interface they can not be used in a factory design template.&lt;br /&gt;
&lt;br /&gt;
The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.&lt;br /&gt;
&lt;br /&gt;
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28811</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28811"/>
		<updated>2009-11-18T22:45:51Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&lt;br /&gt;
 public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
 public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28805</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28805"/>
		<updated>2009-11-18T22:41:02Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
In the Application class the CreateDocument method either has a default implementation or it doesn't have any implementation at all, this operation being redefined in the MyApplication subclass so that it creates a MyDocument object and returns a reference to it.&lt;br /&gt;
&lt;br /&gt;
public Document CreateDocument(String type){&lt;br /&gt;
	if (type.isEqual(&amp;quot;html&amp;quot;))&lt;br /&gt;
		return new HtmlDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;proprietary&amp;quot;))&lt;br /&gt;
		return new MyDocument();&lt;br /&gt;
	if (type.isEqual(&amp;quot;pdf&amp;quot;))&lt;br /&gt;
		return new PdfDocument ();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Assuming that the Application class has a member called docs that represents a list of documents being handled by the application, then the NewDocument method should look like this:&lt;br /&gt;
&lt;br /&gt;
public void NewDocument(String type){&lt;br /&gt;
	Document doc=CreateDocument(type);&lt;br /&gt;
	Docs.add(doc);&lt;br /&gt;
	Doc.Open();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
This method will be inherited by the MyApplication class and, so, through the CreateDocument method, it will actually instantiate MyDocument objects. We will call the CreateDocument method a Factory Method because it is responsible with 'making' an object. Through this method, redefined in Application's subclasses, we can actually shape the situation in which the Application class creates objects without knowing their type. From this point of view the factory method is pattern which provides us a way to achieve the DIP principle.&lt;br /&gt;
&lt;br /&gt;
Specific problems and implementation&lt;br /&gt;
&lt;br /&gt;
When implementing the Factory Method design pattern some issues may appear:&lt;br /&gt;
&lt;br /&gt;
Definition of Creator class&lt;br /&gt;
&lt;br /&gt;
If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:&lt;br /&gt;
&lt;br /&gt;
    * Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can't foresee what ConcreteProduct it will instantiate.&lt;br /&gt;
    * Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.&lt;br /&gt;
&lt;br /&gt;
Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28802</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28802"/>
		<updated>2009-11-18T22:38:50Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28800</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28800"/>
		<updated>2009-11-18T22:37:08Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
 public interface Product { … }&lt;br /&gt;
 public abstract class Creator &lt;br /&gt;
 {&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
 }&lt;br /&gt;
 public class ConcreteProduct implements Product { … }&lt;br /&gt;
 public class ConcreteCreator extends Creator &lt;br /&gt;
 {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
 public class Client &lt;br /&gt;
 {&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28797</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28797"/>
		<updated>2009-11-18T22:34:47Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28796</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28796"/>
		<updated>2009-11-18T22:33:59Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
     	public interface Product&lt;br /&gt;
	{ &lt;br /&gt;
	}&lt;br /&gt;
	public abstract class Creator&lt;br /&gt;
	{&lt;br /&gt;
		public void anOperation() &lt;br /&gt;
		{&lt;br /&gt;
			Product product = factoryMethod();&lt;br /&gt;
		}&lt;br /&gt;
		protected abstract Product factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	public class ConcreteProduct implements Product {&lt;br /&gt;
	}&lt;br /&gt;
	public class ConcreteCreator extends Creator &lt;br /&gt;
	{&lt;br /&gt;
		protected Product factoryMethod() &lt;br /&gt;
		{&lt;br /&gt;
			return new ConcreteProduct();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	public class Client&lt;br /&gt;
	{&lt;br /&gt;
		public static void main( String arg[] ) &lt;br /&gt;
		{&lt;br /&gt;
			Creator creator = new ConcreteCreator();&lt;br /&gt;
			creator.anOperation();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28793</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28793"/>
		<updated>2009-11-18T22:27:25Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Example of factory method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
public interface Product{ &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public abstract class Creator{&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ConcreteProduct implements Product {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ConcreteCreator extends Creator {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Client{&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===More Examples of factory pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28792</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28792"/>
		<updated>2009-11-18T22:26:20Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* What is factory method? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
public interface Product{ &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public abstract class Creator{&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ConcreteProduct implements Product {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ConcreteCreator extends Creator {&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Client{&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Applicability &amp;amp; Examples'''&lt;br /&gt;
&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Example - Documents Application'''&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28789</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28789"/>
		<updated>2009-11-18T22:23:38Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
The participants classes in this pattern are:&lt;br /&gt;
&lt;br /&gt;
    * Product defines the interface for objects the factory method creates.&lt;br /&gt;
    * ConcreteProduct implements the Product interface.&lt;br /&gt;
    * Creator(also refered as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects&lt;br /&gt;
    * ConcreteCreator overrides the generating method for creating ConcreteProduct objects&lt;br /&gt;
&lt;br /&gt;
All concrete products are subclasses of the Product class, so all of them have the same basic implementation, at some extent. The Creator class specifies all standard and generic behavior of the products and when a new product is needed, it sends the creation details that are supplied by the client to the ConcreteCreator. Having this diagram in mind, it is easy for us now to produce the code related to it. Here is how the implementation of the classic Factory method should look:&lt;br /&gt;
&lt;br /&gt;
public interface Product { … }&lt;br /&gt;
&lt;br /&gt;
public abstract class Creator &lt;br /&gt;
{&lt;br /&gt;
	public void anOperation() &lt;br /&gt;
	{&lt;br /&gt;
		Product product = factoryMethod();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected abstract Product factoryMethod();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ConcreteProduct implements Product { … }&lt;br /&gt;
&lt;br /&gt;
public class ConcreteCreator extends Creator &lt;br /&gt;
{&lt;br /&gt;
	protected Product factoryMethod() &lt;br /&gt;
	{&lt;br /&gt;
		return new ConcreteProduct();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Client &lt;br /&gt;
{&lt;br /&gt;
	public static void main( String arg[] ) &lt;br /&gt;
	{&lt;br /&gt;
		Creator creator = new ConcreteCreator();&lt;br /&gt;
		creator.anOperation();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Applicability &amp;amp; Examples&lt;br /&gt;
The need for implementing the Factory Method is very frequent. The cases are the ones below:&lt;br /&gt;
&lt;br /&gt;
    * when a class can't anticipate the type of the objects it is supposed to create&lt;br /&gt;
    * when a class wants its subclasses to be the ones to specific the type of a newly created object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 1 - Documents Application.&lt;br /&gt;
&lt;br /&gt;
Take into consideration a framework for desktop applications. Such applications are meant to work with documents. A framework for desktop applications contains definitions for operations such as opening, creating and saving a document. The basic classes are abstract ones, named Application and Document, their clients having to create subclasses from them in order to define their own applications. For generating a drawing application, for example, they need to define the DrawingApplication and DrawingDocument classes. The Application class has the task of managing the documents, taking action at the request of the client (for example, when the user selects the open or save command form the menu).&lt;br /&gt;
&lt;br /&gt;
Because the Document class that needs to be instantiated is specific to the application, the Application class does not know it in advance, so it doesn't know what to instantiate, but it does know when to instantiate it. The framework needs to instantiate a certain class, but it only knows abstract classes that can't be instantiated.&lt;br /&gt;
&lt;br /&gt;
The Factory Method design pattern solves the problem by putting all the information related to the class that needs to be instantiated into an object and using them outside the framework, as you can see below&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28786</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=28786"/>
		<updated>2009-11-18T22:21:20Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''Motivation'''&lt;br /&gt;
&lt;br /&gt;
Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.&lt;br /&gt;
&lt;br /&gt;
The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you've paid for the room you want and this way he can be looked at as a “room” factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a “phone-call” factory, because he controls the access to calls, too.&lt;br /&gt;
&lt;br /&gt;
*'''Intent'''&lt;br /&gt;
&lt;br /&gt;
    * Defines an interface for creating objects, but let subclasses to decide which class to instantiate&lt;br /&gt;
    * Refers to the newly created object through a common interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*'''Implementation'''&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27309</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27309"/>
		<updated>2009-11-17T14:31:21Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Types of Creational Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== &lt;br /&gt;
In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== &lt;br /&gt;
Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== &lt;br /&gt;
Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== &lt;br /&gt;
It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== &lt;br /&gt;
This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== &lt;br /&gt;
In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== &lt;br /&gt;
This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27303</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27303"/>
		<updated>2009-11-17T13:50:21Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Architectural pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27295</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27295"/>
		<updated>2009-11-17T13:26:23Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Creational patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations. In comparison to design patterns, architectural patterns are larger in scale.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
==== Factory method pattern ==== In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
==== Abstract factory pattern ==== Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27294</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27294"/>
		<updated>2009-11-17T13:25:40Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Types of Creational Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations. In comparison to design patterns, architectural patterns are larger in scale.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
&lt;br /&gt;
====Factory method pattern==== In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
====Abstract factory pattern==== Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27293</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27293"/>
		<updated>2009-11-17T13:24:50Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations. In comparison to design patterns, architectural patterns are larger in scale.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
====Factory method pattern==== In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
====Abstract factory pattern==== Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
====Prototype pattern==== Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
====Builder pattern==== It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
====Singleton pattern==== This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
====Object pool==== In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
====Lazy initialization pattern==== This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27291</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27291"/>
		<updated>2009-11-17T13:22:48Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Classification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
====Architectural pattern====&lt;br /&gt;
Architectural pattern are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations. In comparison to design patterns, architectural patterns are larger in scale.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27287</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27287"/>
		<updated>2009-11-17T13:19:27Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Classification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize &lt;br /&gt;
relationships between entities.&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27286</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27286"/>
		<updated>2009-11-17T13:18:44Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Classification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
====Creational pattern====&lt;br /&gt;
&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Structural pattern====&lt;br /&gt;
&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Behavioral pattern====&lt;br /&gt;
&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27284</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27284"/>
		<updated>2009-11-17T13:17:53Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Classification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
*Creational pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
*Structural pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.&lt;br /&gt;
&lt;br /&gt;
*Behavioral pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27282</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27282"/>
		<updated>2009-11-17T13:17:08Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Introduction to Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
*Creational pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.&lt;br /&gt;
&lt;br /&gt;
*Structural pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.&lt;br /&gt;
&lt;br /&gt;
*Concurrency pattern&lt;br /&gt;
&lt;br /&gt;
In software engineering, concurrency patterns are those types of design patterns that deal with multi-threaded programming paradigm.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27279</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27279"/>
		<updated>2009-11-17T13:12:30Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Classification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27278</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27278"/>
		<updated>2009-11-17T13:11:13Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Introduction to Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
===Classification===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.For a complete list go to&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29#Classification_and_list]&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27276</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27276"/>
		<updated>2009-11-17T13:09:19Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Introduction to Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&amp;quot;Don't reinvent the wheel&amp;quot; all of us have heard this saying and &amp;quot;Design Patterns&amp;quot; is the software engineer's way of saying the same thing. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The credit for introduction and formalization of these reusable solutions goes to the &amp;quot;Gang of Four&amp;quot;(Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides)[http://c2.com/cgi/wiki?GangOfFour]. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
Design patterns reside in the domain of modules and interconnections. At a higher level there are Architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system.&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27271</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27271"/>
		<updated>2009-11-17T12:59:33Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
Design patterns reside in the domain of modules and interconnections. At a higher level there are Architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system.&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
===What is creational pattern===&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
===What is factory method?===&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27270</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27270"/>
		<updated>2009-11-17T12:58:01Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.&lt;br /&gt;
&lt;br /&gt;
Design patterns reside in the domain of modules and interconnections. At a higher level there are Architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system.&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27269</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27269"/>
		<updated>2009-11-17T12:54:28Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Factory Pattern==&lt;br /&gt;
&lt;br /&gt;
===When to use Factory Method?===&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
===Example of factory method===&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Java'''====&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
====Factory method in '''Python'''====&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
====Factory method in Ruby====&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
====Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]====&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27268</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27268"/>
		<updated>2009-11-17T12:53:09Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==Creational patterns==&lt;br /&gt;
&lt;br /&gt;
===Types of Creational Pattern===&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Class diagram of Factory Method==&lt;br /&gt;
&lt;br /&gt;
==When to use Factory Method?==&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
==Example of factory method==&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Java'''===&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Python'''===&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
===Factory method in Ruby===&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
===Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]===&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27267</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=27267"/>
		<updated>2009-11-17T12:51:47Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&lt;br /&gt;
===Classification and list===&lt;br /&gt;
&lt;br /&gt;
Design patterns were originally grouped into the categories Creational patterns, Structural patterns, and Behavioral patterns, and described them using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion. For further background on object-oriented programming, see inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==List of creational patterns==&lt;br /&gt;
&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Class diagram of Factory Method==&lt;br /&gt;
&lt;br /&gt;
==When to use Factory Method?==&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
==Example of factory method==&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Java'''===&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Python'''===&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
===Factory method in Ruby===&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
===Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]===&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=26958</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=26958"/>
		<updated>2009-11-15T21:17:18Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction to Design Patterns==&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==List of creational patterns==&lt;br /&gt;
&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Class diagram of Factory Method==&lt;br /&gt;
&lt;br /&gt;
==When to use Factory Method?==&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
==Example of factory method==&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Java'''===&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Python'''===&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
===Factory method in Ruby===&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
===Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]===&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=26801</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 6 Factory Design Pattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_6_Factory_Design_Pattern&amp;diff=26801"/>
		<updated>2009-11-14T09:26:52Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Factory method is a type of creational pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Be sure to emphasize how it is different from the more general Creator pattern. Also give several examples of how it can be used, and where it provides a more elegant solution than other creational patterns.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
*'''What is factory method?'''&lt;br /&gt;
&lt;br /&gt;
In object oriented language, factory method pattern is a creational design pattern which deals with the mechanism of creating objects of the classes without specifying the exact class of the object. The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6]&lt;br /&gt;
&lt;br /&gt;
*'''What is creational pattern?'''&lt;br /&gt;
&lt;br /&gt;
In software engineering creational patterns are used to create objects suitable to the situation. In object oriented design object creation may lead to design problems if they are not controlled according to situations. Creational patterns deals with this mechanism. There are a list of different creational patterns available which solves problem of creating objects in different situations. The list includes factory design pattern as well. Here we'll see how factory design pattern is different from other creational design patterns and where it is mostly used. &lt;br /&gt;
&lt;br /&gt;
==List of creational patterns==&lt;br /&gt;
&lt;br /&gt;
* '''Factory method pattern:''' In object oriented design a factory is a place used for creating objects. Factory pattern provides centralize creation of an object of a specific type choosing one of several implementations. It encapsulates the creation of objects. At run time the decision is made about which object to instantiate depending on the situation. So it creates objects without specifying the class of the objects.&lt;br /&gt;
&lt;br /&gt;
* '''Abstract factory pattern:''' Abstract factory is the place to take centralize decision of what factory to instantiate. It provides an encapsulation to a group of factories that have common properties. The clients who implement abstract factories doesn't know which concrete object it gets from individual internal factories. The clients only implements the generic interface of these factories. To understand how this pattern is used refer to [http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract factory]. This abstraction hides the details of different implementation of objects from their general usage. Factory design pattern is used inherently in this pattern design.&lt;br /&gt;
&lt;br /&gt;
* '''Prototype pattern:''' Prototype means making clones. This pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. The cost of creating new objects is resource intensive job. This pattern gives solution in this scenario where object cloning saves this cost in terms of resources. This pattern is used to avoid building a class hierarchy of factories that parallels the class hierarchy of products. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Prototype_pattern Prototype pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Builder pattern:''' It is a creational pattern which abstracts the object creation pattern. Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Builder_pattern Builder pattern]. This is much more complex object creation pattern than factory method. There may be cases where design starts with factory method and eventually leads to builder pattern according to the flexibility needed in object creation process.&lt;br /&gt;
&lt;br /&gt;
* '''Singleton pattern:''' This pattern restricts instantiation of the class to exactly one object. There may be situations where exactly one object of the class is needed. For example server configuration, logger etc where this pattern is used. Other design patterns like builder, prototype or abstract patterns can also use singleton pattern in their implementation. For a detailed understanding of this pattern refer to [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern].&lt;br /&gt;
&lt;br /&gt;
* '''Object pool:''' In object oriented design object creation and destruction after completing all operations on it is an expensive task. For example socket connection, database connection etc. are repetitive tasks but are resource intensive in terms of time. This pattern avoids this expensive acquisition and release of resources by by maintaining an object pool. The clients of the object pool places request when it needs an object and releases the hold on the object after performing desired tasks. Then the released objects are returned back to the object pool for reuse. Objects in object pool are specific type of [http://en.wikipedia.org/wiki/Factory_object factory object]. It can be used for creating singleton object also. &lt;br /&gt;
&lt;br /&gt;
* '''Lazy initialization pattern:''' This pattern deals with the mechanism of delaying the creation of an object until the first time the object is needed. The delay in object creation may be required depending on the situations like the calculation of a value or some other expensive process. This pattern can be used together with factory design pattern to design &amp;quot;lazy factory&amp;quot;. To understand how 'lazy factory' works refer to [http://en.wikipedia.org/wiki/Lazy_initialization lazy initialization].&lt;br /&gt;
&lt;br /&gt;
==Class diagram of Factory Method==&lt;br /&gt;
&lt;br /&gt;
==When to use Factory Method?==&lt;br /&gt;
&lt;br /&gt;
The Factory patterns can be used in following situations:&lt;br /&gt;
&lt;br /&gt;
* When flexibility is important.&lt;br /&gt;
&lt;br /&gt;
* When a class does not know which class of objects it is going to create.&lt;br /&gt;
&lt;br /&gt;
* When a class gives the freedom to its subclasses to specify which objects to create.&lt;br /&gt;
&lt;br /&gt;
* Programmers use factory pattern where they have to create an object of any one of sub-classes depending on the data provided. The information about which class object is to create is not known to programmers beforehand and the decision is made at run time depending on the data provided.&lt;br /&gt;
&lt;br /&gt;
* When there is a specific reason why one subclass would be chosen over another. Basically this logic forms part of the Factory Method.&lt;br /&gt;
&lt;br /&gt;
* When the clients delegates responsibilities to subclasses in parallel hierarchies [http://www.rmfusion.com/design_patterns/gof/factory_method_pattern.htm 6].&lt;br /&gt;
&lt;br /&gt;
* In test driven environment for the classes to be put under test[http://en.wikipedia.org/wiki/Factory_method#cite_note-1 3].&lt;br /&gt;
&lt;br /&gt;
==Example of factory method==&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Java'''===&lt;br /&gt;
&lt;br /&gt;
 abstract class Coffee {&lt;br /&gt;
    public abstract double getPrice();&lt;br /&gt;
 }&lt;br /&gt;
 class MochaCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.65;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CafeLate extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 2.10;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class DarkRoastCoffee extends Coffee {&lt;br /&gt;
    public double getPrice() {&lt;br /&gt;
        return 1.96;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class CoffeeFactory {&lt;br /&gt;
    public enum CoffeeType {&lt;br /&gt;
        Mocha,&lt;br /&gt;
        CafeLate,&lt;br /&gt;
        DarkRoast&lt;br /&gt;
    }&lt;br /&gt;
    public static Coffee createCoffee(CoffeeType coffeeType) {&lt;br /&gt;
        switch (coffeeType) {&lt;br /&gt;
            case Mocha:&lt;br /&gt;
                return new MochaCoffee();&lt;br /&gt;
            case CafeLate:&lt;br /&gt;
                return new CafeLate();&lt;br /&gt;
            case DarkRoast:&lt;br /&gt;
                return new DarkRoastCoffee();&lt;br /&gt;
        }&lt;br /&gt;
        throw new IllegalArgumentException(coffeeType + &amp;quot; is not an expected coffee type. This &amp;quot; + coffeeType + &amp;quot; is not served.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class coffeeLover {&lt;br /&gt;
    /*&lt;br /&gt;
     * Create all available coffees in the coffeeshop and print their prices&lt;br /&gt;
     */&lt;br /&gt;
    public static void main (String args[]) {&lt;br /&gt;
        for (CoffeeFactory.CoffeeType coffeeType : CoffeeFactory.CoffeeType.values()) {&lt;br /&gt;
            System.out.println(&amp;quot;Price of &amp;quot; + coffeeType + &amp;quot; is &amp;quot; + CoffeeFactory.createCoffee(coffeeType).getPrice());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Factory method in '''Python'''===&lt;br /&gt;
&lt;br /&gt;
 #: &lt;br /&gt;
 # A simple static factory method.&lt;br /&gt;
 from __future__ import generators&lt;br /&gt;
 import random&lt;br /&gt;
 ##&lt;br /&gt;
 class Coffee(object):&lt;br /&gt;
  # Create based on class name:&lt;br /&gt;
  def factory(type):&lt;br /&gt;
    #return eval(type + &amp;quot;()&amp;quot;)&lt;br /&gt;
    if type == &amp;quot;CafeMocha&amp;quot;: return CafeMocha()&lt;br /&gt;
    if type == &amp;quot;CafeLate&amp;quot;: return CafeLate()&lt;br /&gt;
    assert 1, &amp;quot;We don't serve this coffee: &amp;quot; + type&lt;br /&gt;
  factory = staticmethod(factory)&lt;br /&gt;
 ##&lt;br /&gt;
 class CafeMocha(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeMocha.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeMocha.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 class CafeLate(Coffee):&lt;br /&gt;
  def dark(self): print &amp;quot;CafeLate.dark&amp;quot; &lt;br /&gt;
  def white(self): print &amp;quot;CafeLate.white&amp;quot; &lt;br /&gt;
 ##&lt;br /&gt;
 # Generate coffee name strings:&lt;br /&gt;
 def caffeeNameGen(n):&lt;br /&gt;
  types = Coffee.__subclasses__()&lt;br /&gt;
  for i in range(n):&lt;br /&gt;
    yield random.choice(types).__name__&lt;br /&gt;
 ##&lt;br /&gt;
 coffee_name = \&lt;br /&gt;
  [ Coffee.factory(i) for i in coffeeNameGen(7)]&lt;br /&gt;
 ##&lt;br /&gt;
 for coffee in coffee_name:&lt;br /&gt;
  coffee.dark()&lt;br /&gt;
  coffee.white()&lt;br /&gt;
 #:~&lt;br /&gt;
&lt;br /&gt;
===Factory method in Ruby===&lt;br /&gt;
&lt;br /&gt;
 class CoffeeFactory  &lt;br /&gt;
   def initialize(type)  &lt;br /&gt;
      @dark_coffee = self.class.const_get(&amp;quot;#{type}Dark&amp;quot;)  &lt;br /&gt;
      @white_coffee = self.class.const_get(&amp;quot;#{type}White&amp;quot;)  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_dark  &lt;br /&gt;
      @dark_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
   //&lt;br /&gt;
   def new_white  &lt;br /&gt;
      @white_coffee.new  &lt;br /&gt;
   end  &lt;br /&gt;
 end  &lt;br /&gt;
 //&lt;br /&gt;
 cafe_factory = CoffeeFactory.new('CAFE')  &lt;br /&gt;
 cafe_dark = cafe_factory.new_dark   &lt;br /&gt;
 //&lt;br /&gt;
 late_factory = CoffeeFactory.new('LATE')  &lt;br /&gt;
 late_white = late_factory.new_white&lt;br /&gt;
&lt;br /&gt;
===Factory method in [http://sourcemaking.com/design_patterns/factrory_method/c%2523 C#]===&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
  using System.Collections;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // An array of creators &lt;br /&gt;
      Creator[] creators = new Creator[2];&lt;br /&gt;
      creators[0] = new ConcreteCreatorA();&lt;br /&gt;
      creators[1] = new ConcreteCreatorB();&lt;br /&gt;
      //&lt;br /&gt;
      // Iterate over creators and create products &lt;br /&gt;
      foreach(Creator creator in creators)&lt;br /&gt;
      {&lt;br /&gt;
        Product product = creator.FactoryMethod();&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created {0}&amp;quot;, &lt;br /&gt;
          product.GetType().Name);&lt;br /&gt;
      }&lt;br /&gt;
      //&lt;br /&gt;
      // Wait for user &lt;br /&gt;
      Console.Read();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Product&amp;quot; &lt;br /&gt;
  abstract class Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductA&amp;quot; &lt;br /&gt;
  class ConcreteProductA : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteProductB&amp;quot; &lt;br /&gt;
  class ConcreteProductB : Product&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;Creator&amp;quot; &lt;br /&gt;
  abstract class Creator&lt;br /&gt;
  {&lt;br /&gt;
    public abstract Product FactoryMethod();&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorA : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductA();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  // &amp;quot;ConcreteCreator&amp;quot; &lt;br /&gt;
  class ConcreteCreatorB : Creator&lt;br /&gt;
  {&lt;br /&gt;
    public override Product FactoryMethod()&lt;br /&gt;
    {&lt;br /&gt;
      return new ConcreteProductB();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  //&lt;br /&gt;
  Created ConcreteProductA&lt;br /&gt;
  Created ConcreteProductB&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
&lt;br /&gt;
In spite of advantages of factory method there are few limitations of factory method. &lt;br /&gt;
* The main limitation is this pattern is very difficult to refactor.&lt;br /&gt;
&lt;br /&gt;
* Another difficulty is that the subclasses can't be extended as the pattern initializes the constructor as private. Generally the subclasses inherit the superclass constructor but here in this case it can not be done as the superclass constructor is always private.&lt;br /&gt;
&lt;br /&gt;
* This difficulty can be overcome by changing constructor type from private to protected but the problem is then the subclasses need to re implement all the methods again with same signature.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Index==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] http://wapedia.mobi/en/Creational_pattern - Creational pattern&lt;br /&gt;
&lt;br /&gt;
[2] http://www.artima.com/forums/flat.jsp?forum=17&amp;amp;thread=80613 - Prototype pattern&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Factory_method - Factory method&lt;br /&gt;
&lt;br /&gt;
[4] http://www.allapplabs.com/java_design_patterns/factory_pattern.htm - Factory method&lt;br /&gt;
&lt;br /&gt;
[5] http://sourcemaking.com/design_patterns/factrory_method/c%2523 - Factory method in C#&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=25174</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=25174"/>
		<updated>2009-10-10T00:52:13Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java Code &lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
As seen from our discussion Object Model inculcates the principles of Abstraction to enable the developer to create user friendly, consistent and reusable software.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24727</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24727"/>
		<updated>2009-10-09T23:15:55Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java Code &lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
As seen from our discussion Object Model inculcates the principles of Abstraction to enable the developer to create user friendly consistent and reusable software.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24718</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24718"/>
		<updated>2009-10-09T23:08:22Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java Code &lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24716</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24716"/>
		<updated>2009-10-09T23:07:01Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java Code &lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24715</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24715"/>
		<updated>2009-10-09T23:05:59Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24700</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24700"/>
		<updated>2009-10-09T22:59:24Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming Languages===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism:  &amp;quot;Guard&amp;quot; and &amp;quot;Center&amp;quot; player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Also, something worth noting here is the way we will use any player whether he is &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; is the same as we have provided a common interface for them using Inheritance and Polymorphism.&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24694</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24694"/>
		<updated>2009-10-09T22:53:52Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism :&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24676</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24676"/>
		<updated>2009-10-09T22:45:52Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance: Both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism :&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24671</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24671"/>
		<updated>2009-10-09T22:44:08Z</updated>

		<summary type="html">&lt;p&gt;Kal el: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example:&lt;br /&gt;
&lt;br /&gt;
* Basketball Player is an abstract class&lt;br /&gt;
* Guard and Center are concrete implementations of abstract class BasketballPlayer&lt;br /&gt;
&lt;br /&gt;
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example&lt;br /&gt;
&lt;br /&gt;
*Data Abstraction &lt;br /&gt;
  **Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be &amp;quot;Guard&amp;quot; or &amp;quot;Centre&amp;quot; &lt;br /&gt;
* Encapsulation&lt;br /&gt;
  **By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'&lt;br /&gt;
* Inheritance &lt;br /&gt;
  ** both Guard and Centre inherit features like &amp;quot;dribble&amp;quot; and 'jersey number' from BasketballPlayer&lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24643</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24643"/>
		<updated>2009-10-09T22:34:00Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word &amp;quot;abstract&amp;quot; which we can use to create an abstract class. &amp;quot;An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses.&amp;quot; Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.&lt;br /&gt;
&lt;br /&gt;
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are &amp;quot;center&amp;quot;, &amp;quot;forward&amp;quot; and &amp;quot;guard&amp;quot; . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to &amp;quot;dribble&amp;quot; and &amp;quot;pass&amp;quot; to name a few.&lt;br /&gt;
&lt;br /&gt;
Lets see how a Java Class hierarchy will look like for such an example:&lt;br /&gt;
&lt;br /&gt;
public abstract class BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   private int number; //number on jersey&lt;br /&gt;
   public void dribble(); // abstract method declaration&lt;br /&gt;
   public void pass();&lt;br /&gt;
   public void run()&lt;br /&gt;
    {&lt;br /&gt;
      // code that can make a player run&lt;br /&gt;
     }   &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Guard extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and block Center Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and blocking code &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Centre extends BasketballPlayer&lt;br /&gt;
{&lt;br /&gt;
   public void dribble() // abstract method implementation&lt;br /&gt;
   {&lt;br /&gt;
     // code that can make my player dribble extraordinarily and trick Gaurd Players&lt;br /&gt;
   } &lt;br /&gt;
   public void pass()&lt;br /&gt;
   {&lt;br /&gt;
     // excellent pass throwing and Recieving code &lt;br /&gt;
   }&lt;br /&gt;
   public void shoot() // method specific to a centre player&lt;br /&gt;
   {&lt;br /&gt;
     // shooting code goes here&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time. &lt;br /&gt;
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp. &lt;br /&gt;
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems. &lt;br /&gt;
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes. &lt;br /&gt;
Consider for example a sample Java fragment to represent some common farm &amp;quot;animals&amp;quot; to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class to represent both the state of the animal and its functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public abstract class LivingThing&lt;br /&gt;
{&lt;br /&gt;
     boolean isHungry() ;&lt;br /&gt;
     void eat(Food f);&lt;br /&gt;
     void moveTo(Location l);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public class Animal extends LivingThing&lt;br /&gt;
{&lt;br /&gt;
     private Location loc;&lt;br /&gt;
     private double energyReserves;&lt;br /&gt;
   &lt;br /&gt;
     boolean isHungry() {&lt;br /&gt;
         return energyReserves &amp;lt; 2.5;&lt;br /&gt;
     }&lt;br /&gt;
     void eat(Food f) {&lt;br /&gt;
         // Consume food&lt;br /&gt;
         energyReserves += f.getCalories();&lt;br /&gt;
     }&lt;br /&gt;
     void moveTo(Location l) {&lt;br /&gt;
         // Move to new location&lt;br /&gt;
         loc = l;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With the above definition, one could create objects of type &amp;lt;tt&amp;gt;Animal&amp;lt;/tt&amp;gt; and call their methods like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
thePig = new Animal();&lt;br /&gt;
theCow = new Animal();&lt;br /&gt;
if (thePig.isHungry()) {&lt;br /&gt;
    thePig.eat(tableScraps);&lt;br /&gt;
}&lt;br /&gt;
if (theCow.isHungry()) {&lt;br /&gt;
    theCow.eat(grass);&lt;br /&gt;
}&lt;br /&gt;
theCow.moveTo(theBarn);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In the above example, the class ''&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;'' is an abstraction used in place of an actual animal, ''&amp;lt;code&amp;gt;LivingThing&amp;lt;/code&amp;gt;'' is a further abstraction (in this case a generalisation) of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24513</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24513"/>
		<updated>2009-10-09T21:43:45Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Object Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time. &lt;br /&gt;
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp. &lt;br /&gt;
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems. &lt;br /&gt;
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes. &lt;br /&gt;
Consider for example a sample Java fragment to represent some common farm &amp;quot;animals&amp;quot; to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class to represent both the state of the animal and its functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public abstract class LivingThing&lt;br /&gt;
{&lt;br /&gt;
     boolean isHungry() ;&lt;br /&gt;
     void eat(Food f);&lt;br /&gt;
     void moveTo(Location l);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public class Animal extends LivingThing&lt;br /&gt;
{&lt;br /&gt;
     private Location loc;&lt;br /&gt;
     private double energyReserves;&lt;br /&gt;
   &lt;br /&gt;
     boolean isHungry() {&lt;br /&gt;
         return energyReserves &amp;lt; 2.5;&lt;br /&gt;
     }&lt;br /&gt;
     void eat(Food f) {&lt;br /&gt;
         // Consume food&lt;br /&gt;
         energyReserves += f.getCalories();&lt;br /&gt;
     }&lt;br /&gt;
     void moveTo(Location l) {&lt;br /&gt;
         // Move to new location&lt;br /&gt;
         loc = l;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With the above definition, one could create objects of type &amp;lt;tt&amp;gt;Animal&amp;lt;/tt&amp;gt; and call their methods like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
thePig = new Animal();&lt;br /&gt;
theCow = new Animal();&lt;br /&gt;
if (thePig.isHungry()) {&lt;br /&gt;
    thePig.eat(tableScraps);&lt;br /&gt;
}&lt;br /&gt;
if (theCow.isHungry()) {&lt;br /&gt;
    theCow.eat(grass);&lt;br /&gt;
}&lt;br /&gt;
theCow.moveTo(theBarn);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In the above example, the class ''&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;'' is an abstraction used in place of an actual animal, ''&amp;lt;code&amp;gt;LivingThing&amp;lt;/code&amp;gt;'' is a further abstraction (in this case a generalisation) of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24511</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24511"/>
		<updated>2009-10-09T21:42:22Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Object Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Object Models make use of following Four concepts or ideas to achieve abstraction principles: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
&lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time. &lt;br /&gt;
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp. &lt;br /&gt;
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems. &lt;br /&gt;
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes. &lt;br /&gt;
Consider for example a sample Java fragment to represent some common farm &amp;quot;animals&amp;quot; to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class to represent both the state of the animal and its functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public abstract class LivingThing&lt;br /&gt;
{&lt;br /&gt;
     boolean isHungry() ;&lt;br /&gt;
     void eat(Food f);&lt;br /&gt;
     void moveTo(Location l);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public class Animal extends LivingThing&lt;br /&gt;
{&lt;br /&gt;
     private Location loc;&lt;br /&gt;
     private double energyReserves;&lt;br /&gt;
   &lt;br /&gt;
     boolean isHungry() {&lt;br /&gt;
         return energyReserves &amp;lt; 2.5;&lt;br /&gt;
     }&lt;br /&gt;
     void eat(Food f) {&lt;br /&gt;
         // Consume food&lt;br /&gt;
         energyReserves += f.getCalories();&lt;br /&gt;
     }&lt;br /&gt;
     void moveTo(Location l) {&lt;br /&gt;
         // Move to new location&lt;br /&gt;
         loc = l;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With the above definition, one could create objects of type &amp;lt;tt&amp;gt;Animal&amp;lt;/tt&amp;gt; and call their methods like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
thePig = new Animal();&lt;br /&gt;
theCow = new Animal();&lt;br /&gt;
if (thePig.isHungry()) {&lt;br /&gt;
    thePig.eat(tableScraps);&lt;br /&gt;
}&lt;br /&gt;
if (theCow.isHungry()) {&lt;br /&gt;
    theCow.eat(grass);&lt;br /&gt;
}&lt;br /&gt;
theCow.moveTo(theBarn);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In the above example, the class ''&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;'' is an abstraction used in place of an actual animal, ''&amp;lt;code&amp;gt;LivingThing&amp;lt;/code&amp;gt;'' is a further abstraction (in this case a generalisation) of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24500</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24500"/>
		<updated>2009-10-09T21:38:14Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in Object Oriented Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Four concepts are critical to understanding object models. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
 &lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time. &lt;br /&gt;
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp. &lt;br /&gt;
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems. &lt;br /&gt;
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes. &lt;br /&gt;
Consider for example a sample Java fragment to represent some common farm &amp;quot;animals&amp;quot; to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class to represent both the state of the animal and its functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public abstract class LivingThing&lt;br /&gt;
{&lt;br /&gt;
     boolean isHungry() ;&lt;br /&gt;
     void eat(Food f);&lt;br /&gt;
     void moveTo(Location l);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
public class Animal extends LivingThing&lt;br /&gt;
{&lt;br /&gt;
     private Location loc;&lt;br /&gt;
     private double energyReserves;&lt;br /&gt;
   &lt;br /&gt;
     boolean isHungry() {&lt;br /&gt;
         return energyReserves &amp;lt; 2.5;&lt;br /&gt;
     }&lt;br /&gt;
     void eat(Food f) {&lt;br /&gt;
         // Consume food&lt;br /&gt;
         energyReserves += f.getCalories();&lt;br /&gt;
     }&lt;br /&gt;
     void moveTo(Location l) {&lt;br /&gt;
         // Move to new location&lt;br /&gt;
         loc = l;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With the above definition, one could create objects of type &amp;lt;tt&amp;gt;Animal&amp;lt;/tt&amp;gt; and call their methods like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
thePig = new Animal();&lt;br /&gt;
theCow = new Animal();&lt;br /&gt;
if (thePig.isHungry()) {&lt;br /&gt;
    thePig.eat(tableScraps);&lt;br /&gt;
}&lt;br /&gt;
if (theCow.isHungry()) {&lt;br /&gt;
    theCow.eat(grass);&lt;br /&gt;
}&lt;br /&gt;
theCow.moveTo(theBarn);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In the above example, the class ''&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;'' is an abstraction used in place of an actual animal, ''&amp;lt;code&amp;gt;LivingThing&amp;lt;/code&amp;gt;'' is a further abstraction (in this case a generalisation) of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24492</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 15 A&amp;OM</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_15_A%26OM&amp;diff=24492"/>
		<updated>2009-10-09T21:34:02Z</updated>

		<summary type="html">&lt;p&gt;Kal el: /* Abstraction in object oriented programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Abstraction and the Object Model==&lt;br /&gt;
===Abstraction===&lt;br /&gt;
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html]. &lt;br /&gt;
&lt;br /&gt;
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.&lt;br /&gt;
&lt;br /&gt;
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29]. &lt;br /&gt;
&lt;br /&gt;
* Control abstraction in involves the use of subprograms and related concepts control flows &lt;br /&gt;
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype. &lt;br /&gt;
&lt;br /&gt;
One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.&lt;br /&gt;
&lt;br /&gt;
===Object Model===&lt;br /&gt;
Four concepts are critical to understanding object models. They are: &lt;br /&gt;
* Data Abstraction &lt;br /&gt;
* Encapsulation &lt;br /&gt;
* Inheritance &lt;br /&gt;
* Polymorphism&lt;br /&gt;
&lt;br /&gt;
====Encapsulation====&lt;br /&gt;
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.&lt;br /&gt;
====Data Abstraction ====&lt;br /&gt;
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].&lt;br /&gt;
&lt;br /&gt;
====Inheritance ====&lt;br /&gt;
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.&lt;br /&gt;
====Polymorphism====&lt;br /&gt;
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.&lt;br /&gt;
 &lt;br /&gt;
===Abstraction in Object Oriented Programming===&lt;br /&gt;
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time. &lt;br /&gt;
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp. &lt;br /&gt;
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems. &lt;br /&gt;
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes. &lt;br /&gt;
Consider for example a sample Java fragment to represent some common farm &amp;quot;animals&amp;quot; to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt; class to represent both the state of the animal and its functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang = java&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Animal extends LivingThing&lt;br /&gt;
{&lt;br /&gt;
     private Location loc;&lt;br /&gt;
     private double energyReserves;&lt;br /&gt;
   &lt;br /&gt;
     boolean isHungry() {&lt;br /&gt;
         return energyReserves &amp;lt; 2.5;&lt;br /&gt;
     }&lt;br /&gt;
     void eat(Food f) {&lt;br /&gt;
         // Consume food&lt;br /&gt;
         energyReserves += f.getCalories();&lt;br /&gt;
     }&lt;br /&gt;
     void moveTo(Location l) {&lt;br /&gt;
         // Move to new location&lt;br /&gt;
         loc = l;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
With the above definition, one could create objects of type &amp;lt;tt&amp;gt;Animal&amp;lt;/tt&amp;gt; and call their methods like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=java&amp;gt;&lt;br /&gt;
thePig = new Animal();&lt;br /&gt;
theCow = new Animal();&lt;br /&gt;
if (thePig.isHungry()) {&lt;br /&gt;
    thePig.eat(tableScraps);&lt;br /&gt;
}&lt;br /&gt;
if (theCow.isHungry()) {&lt;br /&gt;
    theCow.eat(grass);&lt;br /&gt;
}&lt;br /&gt;
theCow.moveTo(theBarn);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In the above example, the class ''&amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;'' is an abstraction used in place of an actual animal, ''&amp;lt;code&amp;gt;LivingThing&amp;lt;/code&amp;gt;'' is a further abstraction (in this case a generalisation) of &amp;lt;code&amp;gt;Animal&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
If one requires a more differentiated hierarchy of animals — to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives — that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat-quality. &lt;br /&gt;
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.&lt;br /&gt;
&lt;br /&gt;
===Conclusion===&lt;br /&gt;
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis. &lt;br /&gt;
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis.&lt;br /&gt;
&lt;br /&gt;
===References ===&lt;br /&gt;
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html &lt;br /&gt;
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29&lt;br /&gt;
#http://www.service-architecture.com/database/articles/object_model_concepts.html &lt;br /&gt;
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html&lt;/div&gt;</summary>
		<author><name>Kal el</name></author>
	</entry>
</feed>