Friday, October 31, 2014

Facade design pattern example

Façade è Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Example : Suppose a customer want to know the availability of a specific maruti car (say AltoK10 or Alto800 or Swift) in a maruti showroom. So he/she simply ask the showroom representative for the availability. Now showroom representative checked the availability and tell the user about available quantity.
Here showroom representative acts a FAÇADE.

----------------------------------------------------
package com.exmpl.facade;

public interface Maruti {
       void getAvailability();
}
----------------------------------------------------
package com.exmpl.facade;

public class AltoK10 implements Maruti {

       @Override
       public void getAvailability() {
              System.out
                           .println("There are FIVE piece of Alto K10 are available in our show room.");
       }
}
----------------------------------------------------
package com.exmpl.facade;

public class Alto800 implements Maruti {

       @Override
       public void getAvailability() {
              System.out
                           .println("There are TWENTY piece of Alto 800 are available in our show room.");
       }

}
----------------------------------------------------
package com.exmpl.facade;

public class Swift implements Maruti {

       @Override
       public void getAvailability() {
              System.out.println("SWIFT is out of stock in our show room.");
       }
}
----------------------------------------------------
// ########### Facade class ###########
package com.exmpl.facade;

public class ShowRoomRepresentative {
       private Maruti altoK10;
       private Maruti alto800;
       private Maruti swift;

       public ShowRoomRepresentative() {
              altoK10 = new AltoK10();
              alto800 = new Alto800();
              swift = new Swift();
       }

       public void checkAltoK10Availability() {
              altoK10.getAvailability();
       }

       public void checkAlto800Availability() {
              alto800.getAvailability();
       }

       public void checkSwiftAvailability() {
              swift.getAvailability();
       }
}
----------------------------------------------------
package com.exmpl.facade;

public class Customer {

       public static void main(String[] args) {
              ShowRoomRepresentative representative = new ShowRoomRepresentative();
              System.out.println("Customer : How many Alto K10 is available?");
              representative.checkAltoK10Availability();
              System.out.println("Customer : How many Alto 800 is available?");
              representative.checkAlto800Availability();
              System.out.println("Customer : How many Swift is available?");
              representative.checkSwiftAvailability();
       }

}
----------------------------------------------------
Output è
Customer : How many Alto K10 is available?
There are FIVE piece of Alto K10 are available in our show room.
Customer : How many Alto 800 is available?
There are TWENTY piece of Alto 800 are available in our show room.
Customer : How many Swift is available?

SWIFT is out of stock in our show room.

Thursday, October 30, 2014

Builder Pattern

CreationalPattern is basically used for creating object with different parameters (some of them may be mandatory and some of them may be optional).Suppose we need to know all required materials for a Cake preparation. But this cake may be veg or non-veg. Now these two types of cake again can be divided with sugerfree or withsuger. So, using Builder Pattern, we can builds a complex object using simple objects and using a step by step approach.
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public interface CakePreparation {
       // Mandatory and optional required materials for cake preparation.
       public void milkRequiredOrNot();

       public void eggRequiredOrNot();

       public void fruitRequiredOrNot();

       public void sugerRequiredOrNot();
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public abstract class NonVegCake implements CakePreparation {

       @Override
       public void milkRequiredOrNot() {
              System.out.println("Milk is requird.");
       }

       @Override
       public void eggRequiredOrNot() {
              System.out.println("Egg is requird.");
       }

       @Override
       public void fruitRequiredOrNot() {
              System.out.println("Fruit is not requird.");
       }

       @Override
       public abstract void sugerRequiredOrNot();
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class SugerFreeNonVegCake extends NonVegCake {

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is not requird.");
       }

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class WithSugerNonVegCake extends NonVegCake{

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is requird.");
       }

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public abstract class VegCake implements CakePreparation {

       @Override
       public void milkRequiredOrNot() {
              System.out.println("Milk is requird.");
       }

       @Override
       public void eggRequiredOrNot() {
              System.out.println("Egg is not requird.");
       }

       @Override
       public void fruitRequiredOrNot() {
              System.out.println("Fruit is requird.");
       }

       @Override
       public abstract void sugerRequiredOrNot();

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class SugerFreeVegCake extends VegCake{

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is not requird.");
       }
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class WithSugerVegCake extends VegCake {

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is requird.");
       }
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class ClientClass {
       public static void main(String args[]) {
              ClientClass obj = new ClientClass();

              // Check material requirement for SugerFreeNonVegCake.
              System.out.println("Check material requirement for SugerFreeNonVegCake.");
              SugerFreeNonVegCake sugerFreeNonVegCake = new SugerFreeNonVegCake();
              obj.requiredMaterialsStatus(sugerFreeNonVegCake);

              // Check material requirement for WithSugerVegCake.
              System.out.println("Check material requirement for WithSugerVegCake.");
              WithSugerVegCake withSugerVegCake = new WithSugerVegCake();
              obj.requiredMaterialsStatus(withSugerVegCake);
       }

       public void requiredMaterialsStatus(CakePreparation cakePreparation) {
              cakePreparation.milkRequiredOrNot();
              cakePreparation.eggRequiredOrNot();
              cakePreparation.fruitRequiredOrNot();
              cakePreparation.sugerRequiredOrNot();
       }
}
------------------------------------------------------------
Output è
Check material requirement for SugerFreeNonVegCake.
Milk is requird.
Egg is requird.
Fruit is not requird.
Suger is not requird.
Check material requirement for WithSugerVegCake.
Milk is requird.
Egg is not requird.
Fruit is requird.

Suger is requird.

Abstract factory

package com.exmpl.abstractFactoryPattern;

public interface Animal {
       void getVoice();
}
---------------------------------------------------------------------------------
package com.exmpl.abstractFactoryPattern;

public class Cat implements Animal {

       @Override
       public void getVoice() {
              System.out.println("Cat voice --- Meow Meow Meow Meow Meow");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class Dog implements Animal {

       @Override
       public void getVoice() {
              System.out.println("Dog voice --- Bark Bark Bark Bark Bark");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class Tiger implements Animal {

       @Override
       public void getVoice() {
              System.out.println("Tiger voice --- Growl Growl Growl Growl Growl");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public interface Bird {
       void getColour();
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class Eagle implements Bird {

       @Override
       public void getColour() {
              System.out.println("Eagle is golden colour.");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class Peacock implements Bird {

       @Override
       public void getColour() {
              System.out.println("Peacock is full of different colours.");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class Sparrow implements Bird {

       @Override
       public void getColour() {
              System.out.println("Sparrow is gray colour.");
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public abstract class AbstractFactory {
       abstract Animal getAnimal(String animalType);

       abstract Bird getBird(String birdType);
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

/*This is the factory class, which create specific animal based on animal type supplied.*/
public class AnimalFactory extends AbstractFactory{
       final static String TIGER = "tiger";
       final static String DOG = "dog";
       final static String CAT = "cat";

       public Animal getAnimal(String animalType) {
              Animal returnAnimal = null;
              if (animalType != null) {
                     if (animalType.equalsIgnoreCase(TIGER)) {
                           returnAnimal = new Tiger();
                     } else if (animalType.equalsIgnoreCase(DOG)) {
                           returnAnimal = new Dog();
                     } else if (animalType.equalsIgnoreCase(CAT)) {
                           returnAnimal = new Cat();
                     }
              }
              return returnAnimal;
       }

       @Override
       Bird getBird(String birdType) {
              return null;
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

/*This is the factory class, which create specific bird based on bird type supplied.*/
public class BirdFactory extends AbstractFactory{
       final static String EAGLE = "eagle";
       final static String PEACOCK = "peacock";
       final static String SPARROW = "sparrow";

       public Bird getBird(String birdType) {
              Bird returnBird = null;
              if (birdType != null) {
                     if (birdType.equalsIgnoreCase(EAGLE)) {
                           returnBird = new Eagle();
                     } else if (birdType.equalsIgnoreCase(PEACOCK)) {
                           returnBird = new Peacock();
                     } else if (birdType.equalsIgnoreCase(SPARROW)) {
                           returnBird = new Sparrow();
                     }
              }
              return returnBird;
       }

       @Override
       Animal getAnimal(String animalType) {
              return null;
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

/*
 * This is the factory of factory classes (AnimalFactory and BirdFactory),
 * which create specific factory based on supplied parameter.
*/
public class FactoryProducer {
       final static String ANIMAL = "animal";
       final static String BIRD = "bird";

       public static AbstractFactory getFactory(String choice) {
              if (choice.equalsIgnoreCase(ANIMAL)) {
                     return new AnimalFactory();
              } else if (choice.equalsIgnoreCase(BIRD)) {
                     return new BirdFactory();
              }
              return null;
       }
}
---------------------------------------------------------------------------------package com.exmpl.abstractFactoryPattern;

public class ClientClass {

       public static void main(String[] args) {
              // Get Animal factory
              AbstractFactory animalFactory = FactoryProducer.getFactory("Animal");

              // Get tiger object from AnimalFactory and check it's voice.
              Animal animal1 = animalFactory.getAnimal("tiger");
              animal1.getVoice();

              // Get dog object from AnimalFactory and check it's voice.
              Animal animal2 = animalFactory.getAnimal("dog");
              animal2.getVoice();

              // Get cat object from AnimalFactory and check it's voice.
              Animal animal3 = animalFactory.getAnimal("cat");
              animal3.getVoice();

              // Get Bird factory
              AbstractFactory birdFactory = FactoryProducer.getFactory("Bird");

              // Get eagle object from BirdFactory and check it's color.
              Bird bird1 = birdFactory.getBird("eagle");
              bird1.getColour();
              // Get peacock object from BirdFactory and check it's color.
              Bird bird2 = birdFactory.getBird("peacock");
              bird2.getColour();

              // Get sparrow object from BirdFactory and check it's color.
              Bird bird3 = birdFactory.getBird("sparrow");
              bird3.getColour();
       }
}

---------------------------------------------------------------------------------
Output è
Tiger voice --- Growl Growl Growl Growl Growl
Dog voice --- Bark Bark Bark Bark Bark
Cat voice --- Meow Meow Meow Meow Meow
Eagle is golden colour.
Peacock is full of different colours.
Sparrow is gray colour.