Category Archives: Design Pattern

Facade Design Pattern

Facade pattern hides the complexities of the system and provide the interface to use that system.
Here we are going to create a shape interface that will be provided to client for using the Shape Maker.

facade.jpg

Step-1 Create an interface Shape

interface Shape
{
 public void draw();
}

class Circle implements Shape
{
  public void draw()
  {
     system.out.println("Hi, this is circle");
  }
}

class Rectangle implements Shape
{
  public void draw()
  {
     system.out.println("Hi, this is rectangle");
  }
}

class Square implements Shape
{
   public void draw()
   {
     system.out.println("Hi, this is square");
   }
}

class ShapeMaker 
{
   Shape circle;
   Shape rectangle;
   Shape square;
   ShapeMaker(Shape shape)
    {
	circle = new Circle();
	rectangle = new Rectangle();
        square = new Square();
    }

   public void drawCircle()
   {
	circle.draw();
   }

  public void drawRectangle()
  {
	rectangle.draw();
  }

  public void drawSquare()
  {
  	square.draw();
  }
}

public class FacadeDemo
{
 ShapeMaker shapeMaker = new ShapeMaker();	
 public static void main(String[] arg)
 {
  shapeMaker.drawCircle();
  shapeMaker.drawRectangle();
  shapeMaker.drawSquare();
 }
}


Output:
Hi, this is circle
Hi, this is rectangle
Hi, this is square

Abstract Factory design pattern

It is a super factory which creates other factories. This factory is also called as Factory of factories.
Example:
Abstractfactory

abstract class Animal
{
 abstract String speak();
}

class Cat
{
 string speak()
 {
  return "Meow";
 }
}

class Dog
{
 string speak()
 {
  return "Bark bark";
 }
}

class Lion
{
 string speak()
 {
  return "Roar roar";
 }
}

class Octopus
{
 string speak()
 {
  return "Squak squak";
 }
}

class Shark
{
 string speak()
 {
  return "cant speak";
 }
}

class SeeAnimalFactory 
{
 public Animal getAnimal(string animalType)
 {
   Animal animal = null;
   If("octopus".equals(animalType))
      animal = new Octopus();
   If("shark".equals(animalType))
      animal = new Shark();
 }
}

class LandAnimalFactory 
{
  public Animal getAnimal(string animalType)
  {
    Animal animal = null;
    if("dog".equals(animalType))
        animal = new Dog();
    if("cat".equals(animalType))
        animal = new Cat();
    if("lion".equals(animalType))
        animal = new Lion();
 }
}

class AnimalFactory
{
 public abstract Animal getAnimal(string anumalType)
  {
    If("see".equals(anumalType))
        return new SeeAnimalFactory();
    else
      return new LandAnimalFactory();
  }
}

class SpeakAnimals 
{
 AnimalFactory animalFactory = new AnimalFactory();
 Animal animal = animalFactory.getAnimal("Dog");
 animal.speak();
}

Factory design pattern

  • We create objects without exposing the creation logic to the client and refer to newly created object using a common interface.
  • In simple words if we have super and sub classes , and based on data provided we have to return the object of one of the sub classes , we use factory pattern.
  • The basic principal behind this pattern is that , at run time we get an object of similar type based on the parameter we passed.
  • If object creation code is spread in whole application, and you need to change the creation of object creation then you need to go in each and every place to make necessary changes.

facade

We have different animals like Duck , Dog and Lion which extends Animal.

abstract Class Animal 
{
  abstract String speak();
}
class Duck
{
 string speak()
 {
  return "Quack quack";
 }
}
class Dog
{
 string speak()
 {
  return "Bark bark";
 }
}

class Roar
{
 string speak()
 {
  return "Roar roar";
 }
}

class AnimalFactory 
{
 public Animal getAnimal(String animalType)
 {
   Animal animal = null;
   if("dog".equals(animalType))
       animal = new Dog();
   if("duck".equals(animalType))
       animal = new Duck();
   if("lion".equals(animalType))
       animal = new Lion();
 }
}
class SpeakAnimals 
{
 AnimalFactory animalFactory = new AnimalFactory();
 Animal animal = animalFactory.getAnimal(“Dog”);
 animal.speak();
}


Now you can check above conditions with this example.