Factory method design pattern for beginners.

Background -

First, let me ask a simple question. Do you really think that "factory method design pattern comes because we had an issue with creating objects? Or it is only for creating objects? Or why we need a factory to create an object when we can create it through constructor?

The answer is NO! Each class is capable of creating objects on its own. Through constructor (until and unless you have not put constructor in private). 

Then why we need factory at all? Is there something like, without Factory method, I am not able to create my software? No, we can create objects without it, then why we are using it very extensively?

Remember one thing, programming languages are very powerful. We can build software without following any design principles. But then, it will be poor in some of these areas - 
  • Maintainability.
  • Extend-ability.
  • Performance.
  • Security.
  • Robust.
  • and many more...
Developers have been developing software for quite a long time, along with their great works. They have also committed many design mistakes while developing software at an early stage (during 70ies). Smart people have learned from mistakes, fixed it, and documented it. Developers found similar errors in many different software, and for the same kind of behavior, they found the patterns

Erich Gamma, with his 3 other friends (Richard Helm, Ralph Johnson, and John Vlissides), has documented GoF design pattern, to teach all people about the mistake they are doing while creating software. They found patterns (similar mistakes) across different software products.


All design patterns related to creating object are categorized as a creational design pattern, and all of them follow Factory concept. To understand factory method design pattern, I would suggest you to read factory concepts first.


Factory method design pattern -


Factory method design pattern is a subset of abstract factory design pattern. In abstract factory design pattern, we create a different type of object that belongs to different object family. But in the factory method design pattern, we create an object which belongs to the same family.
Constructor is a type of factory method, which creates objects of the same kind.


Mostly we use factory method for application development and abstract factory for system level development.

Structure -



Example -





/*
 * Factory method pattern.
 * example # - 1.
 */

 #include<iostream>
 using namespace std;

 class cars {
 public:
 virtual void showCars() = 0;
 };

 class audi:public cars {
 public:
 void showCars() {
  cout<<"this is audi"<<endl;
 }
 };

 class mercedesBenz:public cars {
 public:
 void showCars() {
  cout<<"this is mercedes Benz"<<endl;
 }
 };

 class tata:public cars {
 public:
 void showCars() {
  cout<<"this is tata"<<endl;
 }
 };

 enum carType{audiType = 1, mercedesBenzType, tataType};

 class carFactory {
 public:
 cars* getCar(carType type);
 };

 cars* carFactory::getCar(carType type) {
  cars *pcars = NULL;
  
  switch(type) {
   
  case audiType:
   pcars = new audi();
   break;
  case  mercedesBenzType:
   pcars = new mercedesBenz();
   break;
  case tataType:
   pcars = new tata();
   break;
  default:
   cout<<"Selected type dose not exist"<<endl;
  }
  return pcars;
 }

 int main (int argc, char ** argv) {
  
  carFactory *factory = new carFactory();
  
  cars* car1 = factory->getCar(audiType);
  car1->showCars();
  
  cars* car2 = factory->getCar(mercedesBenzType);
  car2->showCars();
  
  cars* car3 = factory->getCar(tataType);
  car3->showCars();
  
  return 0;
 }


Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

  1. Hi Kunjesh,

    Can you please provide one simple example of factory design pattern in Java ?

    ReplyDelete
    Replies
    1. Hi Ankit, I am a core C and C++ resource, I hope it will not be very tough, for any java resource to convert above mentioned c++ program into java code. Still I will try to do it by myself, but it may take some time, as I am not hardcore java developer, Sorry for late response.

      Delete

Post a Comment

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.