Sunday, 29 September 2013

Filter or Criteria Design Pattern - Introduction


Click here to watch in Youtube : https://www.youtube.com/watch?v=q0SmOyn2Awg

Click the below Image to Enlarge
Filter or Criteria Design Pattern - Introduction

Service Locator Design Pattern - KeyPoints

Iterator Design Pattern - KeyPoints

Business Delegate Design Pattern - KeyPoints

Abstract Factory Design Pattern - KeyPoints

Abstract Factory Design Pattern - Implementation


Click here to watch in Youtube : https://www.youtube.com/watch?v=Bbjh8atQJz0

Click the below Image to Enlarge
Abstract Factory Design Pattern - Implementation - Class Diagram





















Animal.java

public interface Animal
{
public String speak();
}


Cat.java

public class Cat implements Animal
{

@Override
public String speak()
{
return "Meow Meow Meow";
}

}


Dog.java

public class Dog implements Animal
{

@Override
public String speak()
{
return "Bark Bark Bark";
}

}


Lion.java

public class Lion implements Animal
{

@Override
public String speak()
{
return "Roar";
}

}


Octopus.java

public class Octopus implements Animal
{
@Override
public String speak()
{
return "SQUAWCK";
}
}


Shark.java

public class Shark implements Animal
{
@Override
public String speak()
{
return "Cannot Speak";
}
}


AnimalFactory.java

public abstract class AnimalFactory
{
public abstract Animal getAnimal( String animalType );
public static AnimalFactory getAnimalFacory(String factoryType)
{
AnimalFactory animalFactory = null ;
if("sea".equals(factoryType))
{
animalFactory = new SeaAnimalFactory();
}
else
{
animalFactory = new LandAnimalFactory();
}
return animalFactory;
}

}


LandAnimalFactory.java

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


SeaAnimalFactory.java

public class SeaAnimalFactory extends AnimalFactory
{
public Animal getAnimal( String animalType )
{
Animal animal = null;
if( "shark".equals(animalType) )
{
animal = new Shark();
}
else if( "octopus".equals(animalType) )
{
animal = new Octopus();
}
return animal;
}
}


Client.java

public class Client
{

public static void main( String[] args )
{
Animal animal = null;
AnimalFactory animalFactory = null;
String speakSound = null;

// Get Factory object by passing the factory type
animalFactory = AnimalFactory.getAnimalFacory("sea");

System.out.println("Animal Factory type : " + animalFactory.getClass().getName());
System.out.println();

// Get Animal object by passing the animal type
animal = animalFactory.getAnimal("shark");
System.out.println("Animal Type : "+animal.getClass().getName());
speakSound = animal.speak();
System.out.println("shark speak : "+speakSound);
System.out.println();

animal = animalFactory.getAnimal("octopus");
System.out.println("Animal Type : "+animal.getClass().getName());
speakSound = animal.speak();
System.out.println("octopus speak : "+speakSound);

System.out.println("---------------------------------------------------------");
// Get Factory by passing the factory type
animalFactory = AnimalFactory.getAnimalFacory("land");

System.out.println("Animal Factory type : " + animalFactory.getClass().getName());
System.out.println();

// Get Animal object by passing the animal type
animal = animalFactory.getAnimal("dog");
System.out.println("Animal Type : "+animal.getClass().getName());
speakSound = animal.speak();
System.out.println("dog speak : "+speakSound);
System.out.println();

animal = animalFactory.getAnimal("cat");
System.out.println("Animal Type : "+animal.getClass().getName());
speakSound = animal.speak();
System.out.println("cat speak : "+speakSound);

System.out.println();
animal = animalFactory.getAnimal("lion");
System.out.println("Animal Type : "+animal.getClass().getName());
speakSound = animal.speak();
System.out.println("lion speak : "+speakSound);

}
}


Output

Animal Factory type : SeaAnimalFactory

Animal Type : Shark
shark speak : Cannot Speak

Animal Type : Octopus
octopus speak : SQUAWCK
---------------------------------------------------------
Animal Factory type : LandAnimalFactory

Animal Type : Dog
dog speak : Bark Bark Bark

Animal Type : Cat
cat speak : Meow Meow Meow

Animal Type : Lion
lion speak : Roar

Abstract Factory Design Pattern - Introduction


Click here to watch in Youtube : https://www.youtube.com/watch?v=cvqyJvVjxj4

Click the below Image to Enlarge
Abstract Factory Design Pattern - Introduction
























See also:

  • Abstract Factory Design Pattern - Implementation
  • Abstract Factory Design Pattern - key Points
  • All Design Patterns Links
  • Friday, 27 September 2013

    Factory Design Pattern - Implementation



    Click here to watch in Youtube : https://www.youtube.com/watch?v=q9p-E19JFlA

    Click the below Image to Enlarge
    Factory Design Pattern - Implementation - Class Diagram




















    Animal.java

    public interface Animal
    {
    public String speak();
    }


    Dog .java

    public class Dog implements Animal
    {

    @Override
    public String speak()
    {
    return "Bark Bark Bark";
    }

    }


    Duck.java

    public class Duck implements Animal
    {

    @Override
    public String speak()
    {
    return "Quack Quack Quack";
    }

    }

    Lion.java

    public class Lion implements Animal
    {

    @Override
    public String speak()
    {
    return "Roar";
    }

    }

    AnimalFactory.java

    public class AnimalFactory
    {
    public Animal getAnimal( String animalType )
    {
    Animal animal = null;
    if( "dog".equals(animalType) )
    {
    animal = new Dog();
    }
    else if( "duck".equals(animalType) )
    {
    animal = new Duck();
    }
    else if( "lion".equals(animalType) )
    {
    animal = new Lion();
    }
    return animal;
    }
    }


    Client.java

    public class Client
    {

    public static void main( String[] args )
    {
    AnimalFactory animalFactory = new AnimalFactory();
    Animal animal = null;
    String speakSound = null;
    animal = animalFactory.getAnimal("dog");
    System.out.println("Animal Type : "+animal.getClass().getName());
    speakSound = animal.speak();

    System.out.println("dog speak : "+speakSound);
    System.out.println();

    animal = animalFactory.getAnimal("duck");
    System.out.println("Animal Type : "+animal.getClass().getName());
    speakSound = animal.speak();

    System.out.println("duck speak : "+speakSound);
    System.out.println();

    animal = animalFactory.getAnimal("lion");
    System.out.println("Animal Type : "+animal.getClass().getName());
    speakSound = animal.speak();

    System.out.println("lion speak : "+speakSound);

    }

    }

    Output

    Animal Type : Dog
    dog speak : Bark Bark Bark

    Animal Type : Duck
    duck speak : Quack Quack Quack

    Animal Type : Lion
    lion speak : Roar

    Google+ Followers

    Google+ Badge