Geeks With Blogs

News kaleidoscope 1817, lit. "observer of beautiful forms," coined by its inventor, Sir David Brewster (1781-1868), from Gk. kalos "beautiful" + eidos "shape" (see -oid) + -scope, on model of telescope, etc. Figurative meaning "constantly changing pattern" is first attested 1819 in Lord Byron, whose publisher had sent him one.
Kaleidoscope Everything under the sun, ending in .Net

Creational design patterns were introduced in order to avoid the inherent tight coupling between the classes which were stemmed up from the excessive creation of objects of one class by the other class. Factory pattern is one of the most widely used creational patterns. This pattern calls for the use of a specialized object solely to create other objects, much like a real-world factory.




In the above figure, the client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product.

Unfortunately nobody can be told what a design pattern is. [Sound familiar?] You have to see it in action. And herein comes our illustrative box office example:

In context with the above explanation consider a Superhero as a product, a SuperheroGenerator as a Factory and the oh-so-obvious CityInTrouble as a client. Now a CityInTrouble may have a variety of calamities befalling it every other day much like Townsville (yes, I still watch powerpuff girls and I do not see a psychiatrist on Saturdays). So CityInTrouble cannot have the same Superhero solve all their problems and hence they call upon the SuperheroGenerator to deliver a Superhero based on the current problems that our noble citizens are facing. Now you must be wondering - ok... I got why we use a factory but if you cant tell me how to implement it there is not much point to this, is there? Nope. Absolutely not. Hence lets see how this stuff really works:

First we have the Superhero (Product) class with SpecialPower as a property and a Save method:

abstract class Superhero{

   public abstract string SpecialPower{ get; }

  public void Save();


Then we have a SuperheroGenerator (Factory) class:

abstract class SuperheroGenerator {

   public abstract Superhero GetSuperhero();


Notice that both the above classes are abstract and we need to extend these. Now the subclasses can override the respective methods of their parent class to provide their own functionality. So if we had Superman out there inheriting the Superhero class he would have his SpecialPower property returning a "InhumanStrength" and XavierSchoolForMutants would in turn inherit SuperheroGenerator and override its GetSuperhero method to return Wolverine. So in short the entire purpose of a SuperheroGenerator (Factory) would be to enable the CityInTrouble (Client) to get the best Superhero (Product) in town appropriate for its needs without having to worry about how to get one (creating a Product). Welcome the Mayor class!! Now he is the one that will handle the complex functionality of deciding which SuperheroGenerator to contact for which Superhero based on the needs of the city. Both the CityInTrouble and Mayor class will look like this:

class Mayor{

public void SaveTheCity(string helptype) {
  case "LexLuthor":
//Here KryptoniteFactory  is a subclass of SuperheroFactory and
//invokes Superman’s Save method via the Factory GetSuperHero() method.
KryptoniteFactory krypton = new KryptonFactory();

case "Magneto":
//Here XavierSchoolForMutants is a subclass of SuperheroFactory and
//invokes Wolverine’s Save method via the GetSuperHero() method.
XavierSchoolForMutants xavier = new XavierSchoolForMutants ();


LawyerFactory lawyers = new LawyerFactory();



class CityInTrouble{

      Mayor mayor = new Mayor();



Thus the CityInTrouble would only describe the type of help needed and the Mayor would take care of the rest with the help of Superhero Factory.





Posted on Thursday, July 8, 2010 10:10 PM | Back to top

Comments on this post: Design Pattern Series – Factory Pattern

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © kaleidoscope | Powered by: