Thursday, October 30, 2014

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.

Factory Pattern


package com.exmpl.factoryPattern;

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

public class Dog implements Animal {

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

public class Cat implements Animal {

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

public class Tiger implements Animal {

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

/*This is the factory class, which create specific animal based on animal type supplied.*/
public class AnimalFactory {
       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;
       }
}
package com.exmpl.factoryPattern;

public class ClientClass {

       public static void main(String[] args) {
              AnimalFactory animalFactory = new AnimalFactory();

              // 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();
       }
}
Output è
Tiger voice --- Growl Growl Growl Growl Growl
Dog voice --- Bark Bark Bark Bark Bark

Cat voice --- Meow Meow Meow Meow Meow

Singleton Pattern

package com.exmpl.singleton;

public class PatternClass {
       // 1. create an object of PatternClass.
       private static PatternClass instance = new PatternClass();

       // 2. Create a private constructor, so that this class can not be
       // instantiated.
       private PatternClass() {
              // No argument constructor.
       }

       // 3. Create a getter method of "instance" property, which return instance
       // of "PatternClass".
       public static PatternClass getInstance() {
              return instance;
       }

       // Normal method.
       public void displayMessage() {
              System.out.println("I am displayMessage() under PatternClass.");
       }
}
package com.exmpl.singleton;

public class ClientClass {

       public static void main(String[] args) {
              /*
               * We can not create an instance of "PatternClass" as below, because of
               * private constructor. The following line will shown an compilation
               * error.
               */
              // PatternClass patternClass = new PatternClass();

              /*
               * Get the instance of "PatternClass", by calling "getInstance()"
               * method.
               */
              PatternClass patternClass = PatternClass.getInstance();
              // Now calling "displayMessage()" method.
              patternClass.displayMessage();
       }
}

Output è I am displayMessage() under PatternClass.

Wednesday, October 29, 2014

Design Patterns

  • 1     Creational Patterns è This type of pattern is responsible to create objects without instantiate objects directly using “new” operator.

                                 i.            Singleton Pattern
                               ii.            Factory Pattern
                              iii.            Abstract Factory Pattern
                             iv.            Builder Pattern
                               v.            Prototype Pattern
  • 2       Structural Pattern è This type of pattern is responsible to describe how objects and classes will be combined to create a large structure.  Here, Inheritance (class and subclass concept) is use to compose Interface.

                                 i.            Adapter Pattern
                               ii.            Bridge Pattern
                              iii.            Composite Pattern
                             iv.            Decorator Pattern
                               v.            Façade Pattern
                             vi.            Flyweight Pattern
                            vii.            Proxy Pattern
  • 3      Behavioral Patterns è This pattern describes interaction between objects. This pattern is used to describe the interaction between objects in such a way, so that objects will interact with each other in a loosely coupled manner.

                                 i.            Chain of Responsibility Pattern
                               ii.            Command Pattern 
                              iii.            Interpreter Pattern
                             iv.            Iterator Pattern
                               v.            Mediator Pattern
                             vi.            Momento Pattern
                            vii.            Observer Pattern
                          viii.            State Pattern
                             ix.            Strategy Pattern
                               x.            Template Pattern

                             xi.            Visitor Pattern

   4. Architectural Pattern ==>
            i. MVC

Saturday, August 3, 2013

Log4j example in java


  1. Download “log4j-x.x.x.jar” file.
  2. Create a java project in eclipse.
  3. Open project’s properties and click on “Java Build Path” and then go to “Libraries” tab and add this jar by click on “Add External Jars..” tab.

Friday, July 12, 2013

Comparator interface to sort java object based on it's parameter.

Sort list of Employee object based on it's name parameter.

1.Bean class:
package com.collection.javacodepractices;

public class Employee {
      private int rollNo;
      private String name;

      public int getRollNo() {
            return rollNo;
      }

      public void setRollNo(int rollNo) {
            this.rollNo = rollNo;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      @Override
      public String toString() {
            return "Employee [rollNo=" + rollNo + ", name=" + name + "]";
      }


}

2.Comparable class:
package com.collection.javacodepractices;

import java.util.Comparator;

public class EmployeeComparable implements Comparator<Employee>{

      @Override
      public int compare(Employee empOne, Employee empTwo) {
            return (empOne.getName().compareToIgnoreCase(empTwo.getName())<0?-1:                                                   (empOne.getName().equalsIgnoreCase(empTwo.getName())?0:1));
      }

}

3.Client class:
package com.collection.javacodepractices;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Client {
      public static void main(String args[]) {
            List<Employee> empList = new ArrayList<Employee>();

            // 1. employee setting.
            Employee emp1 = new Employee();
            emp1.setRollNo(1);
            emp1.setName("Bijoy");
            // 2. employee setting.
            Employee emp2 = new Employee();
            emp2.setRollNo(2);
            emp2.setName("Bappa");
            // 3. employee setting.
            Employee emp3 = new Employee();
            emp3.setRollNo(3);
            emp3.setName("Chintu");
            // 4. employee setting.
            Employee emp4 = new Employee();
            emp4.setRollNo(4);
            emp4.setName("Rahul");
            // 5. employee setting.
            Employee emp5 = new Employee();
            emp5.setRollNo(5);
            emp5.setName("Ajoy");
           
            //Add employee object to list.
            empList.add(emp1);
            empList.add(emp2);
            empList.add(emp3);
            empList.add(emp4);
            empList.add(emp5);
           
            //sorting employee based on name using comparator interface.
            Collections.sort(empList, new EmployeeComparable());
           
            //Print Employee object.
            for(Employee emp:empList){
                  System.out.println(emp.toString());
            }
      }
}

OUTPUT:
Employee [rollNo=5, name=Ajoy]
Employee [rollNo=2, name=Bappa]
Employee [rollNo=1, name=Bijoy]
Employee [rollNo=3, name=Chintu]
Employee [rollNo=4, name=Rahul]