Tuesday, 1 October 2013

Factory Pattern



Almighty Lord Krishna has a factory of creating species (84 lac species) what ever comes in his mind he create those species.

Roughly he is running following programme in Background

class Cat {}

class Dog {}

class Man {}

class Women {}

     

public class FactoryPatternExample {

      public Object createSpecies(String sankalp) {

            if (sankalp.equals("dog")) {
                  return new Dog();
            } else if (sankalp.equals("cat")) {
                  return new Cat();
            } else if (sankalp.equals("man")) {
                  return new Man();
            } else if (sankalp.equals("women")) {
                  return new Women();
            } else
                  return null;
      }
}

Since createSpecies() method dispenses OBJECT ... Krishnji can give more specific term to Object
As

abstract class Jeev {
      public abstract String getDescription();

}

class Cat extends Jeev {

      @Override
      public String getDescription() {
            // TODO Auto-generated method stub
            return "CAT";
      }

}

class Dog extends Jeev {

      @Override
      public String getDescription() {
            // TODO Auto-generated method stub
            return "DOG";
      }
}

class Man extends Jeev {

      @Override
      public String getDescription() {
            // TODO Auto-generated method stub
            return "MAN";
      }
}

class Women extends Jeev {

      @Override
      public String getDescription() {
            // TODO Auto-generated method stub
            return "WOMEN";
      }
}


Now method createObjects() can return Jeev instead of general Object

Now  84 lac species out of one factory is a complicated so Krishna decided to divide it into parts lets  say AnimalFactory , InsectFactoy  , PlantsFactory etc.


abstract class KrishnaFactory {

      protected abstract Jeev createSpecies(String sankalp);
}

class AnimalFactory extends KrishnaFactory {

      @Override
      protected Jeev createSpecies(String sankalp) {

            if (sankalp.equals("dog")) {
                  return new Dog();
            } else if (sankalp.equals("cat")) {
                  return new Cat();
            } else
                  return null;
      }
}

class HumanFactory extends KrishnaFactory {

      @Override
      protected Jeev createSpecies(String sankalp) {

            if (sankalp.equals("man")) {
                  return new Man();
            } else if (sankalp.equals("women")) {
                  return new Women();
            } else
                  return null;
      }
}  
Bramaji is now happy as the process of creating species is now simple and arranged.

 public class FactoryPatternExample {

      public static void main(String[] args) {

            AnimalFactory animalFactory = new AnimalFactory();
            Jeev jeev = animalFactory.createSpecies("cat");
            System.out.println(jeev.getDescription());
       }
 }






No comments:

Post a Comment