Friday, November 25, 2011

Best way to declare static map


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class StaticMapDemo {
      public static final Map<Integer, String> NUMBERS = Collections.unmodifiableMap(getNumbers());
     
      private static Map<Integer, String> getNumbers(){
            Map<Integer,String> map = new HashMap<Integer, String>();
            map.put(1, "one");
            map.put(2, "two");
            map.put(3, "three");
            return map;
      }
     
      public static void main(String[] args) {
            System.out.println(StaticMapDemo.NUMBERS);
      }
}

Output:
{1=one, 2=two, 3=three}

Thursday, November 24, 2011

How to get list from array?


Use the Arrays asList method to get the list.

import java.util.Arrays;
import java.util.List;

public class GetListFromArray {
      public static void main(String[] args){
            String[] array = {"bat", "bet", "bit", "bot", "but"};
            List list = Arrays.asList(array);
            System.out.println(list);
      }
}

Dependency Inversion Design Principle


If high level classes are not working directly with low level classes and they are using interfaces as an abstract layer then we will implement this principle with creational design pattern.

Bad Scenario :
Robot
public class Robot {
      public void work (){
            System.out.println("Robot's work.");
      }
}
Worker
public class Worker {
      public void work(){
            System.out.println("Worker's Work.");
      }
}
WorkManager
public class WorkManager {
      public void manage(Worker worker){
            worker.work();
      }
     
      public void manager(Robot robot){
            robot.work();
      }
}

Good Scenario :
IWork
public interface IWork {
      public void work();
}
Robot
public class Robot implements IWork {
      public void work(){
            System.out.println("Robot's work.");
      }
}
Worker
public class Worker implements IWork {
      public void work(){
            System.out.println("Worker's Work.");
      }
}
WorkManager
public class WorkManager {
      public void manage(IWork work){
            work.work();
      }
}

Tuesday, November 22, 2011

Open-Close Design Principle


Software entities like classes, modules and functions should be open for extension but closed for modifications.
BAD Example:
AreaCalculator need to be modified for every new shape.
Shape:
public interface Shape {

}
Square:
public class Square implements Shape{
      private double side = 0;
     
      public Square(double side){
            this.side = side;
      }
     
      public double getSide(){
            return side;
      }
}
Circle:
public class Circle implements Shape{
      private double radius = 0;
     
      public Circle(double radius){
            this.radius = radius;
      }
      public double getRadius(){
            return radius;
      }
}
AreaCalculator:
public class AreaCalculator {
      public double area(Shape[] shapes){
            double area = 0;
            double value = 0;
            for(Shape shape : shapes){
                  if(shape instanceof Square){
                        value = ((Square)shape).getSide();
                        area += value * value;
                  }else if(shape instanceof Circle){
                        value = ((Circle) shape).getRadius();
                        area += Math.PI * value * value;
                  }
            }
            return area;
      }
}

Good Example:
     In following code, we can add new shapes (Shape – Open for extensions) and those are not going to impact AreaCalculator (Closed for Modifications).

Shape:
public interface Shape {
      public double area();
}
Square:
public class Square implements Shape {
      private double side = 0;
     
      public Square(double side){
            this.side = side;
      }
     
      public double area(){
            return (side * side);
      }
}
Circle:
public class Circle implements Shape{
      private double radius = 0;
     
      public Circle(double radius){
            this.radius = radius;
      }
     
      public double area() {
            return (Math.PI * radius * radius);
      }
}
AreaCalculator:
public class AreaCalculator {
      public double area(Shape[] shapes){
            double area = 0;
            for(Shape shape : shapes){
                  area += shape.area();
            }
            return area;
      }
}

Monday, November 21, 2011

Factory Method Creational Design Pattern


Factory Method is most commonly used creational design pattern. It provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.


Implementation:

Shape:
public interface Shape {
      public void draw();    
}
Circle:
public class Circle implements Shape {
      private double radius = 0;
     
      public Circle(double radius){
            this.radius = radius;
      }
     
      public void draw(){
            System.out.println("Hi! This is circle");
            System.out.println("Circle radius : " + radius );
      }    
}
Square:
public class Square implements Shape{
      private double side = 0;
     
      public Square(double side){
            this.side = side;
      }
     
      public void draw(){
            System.out.println("Hi! This is square");
            System.out.println("Square side : " + side);
      }
}
Rectangle:
public class Rectangle implements Shape {
      private double length = 0;
      private double breadth = 0;
     
      public Rectangle(double length, double breadth){
            this.length = length;
            this.breadth = breadth;
      }
     
      public void draw(){
            System.out.println("Hi! This is rectangle");
            System.out.println("Rectangle length : " + length);
            System.out.println("Rectangle breadth : " + breadth);
      }    
}
ShapeFactory:
public class ShapeFactory {
      public Shape createCircle(double radius){
            return new Circle(radius);
      }
     
      public Shape createSquare(double side){
            return new Square(side);
      }
     
      public Shape createRectangle(double length, double breadth){
            return new Rectangle(length, breadth);
      }
}
User:
public class User {
      public static void main(String[] args) {
            ShapeFactory factory = new ShapeFactory();
            Shape shape = null;
            shape = factory.createCircle(1.23);
            shape.draw();
            shape = factory.createSquare(23);
            shape.draw();
            shape = factory.createRectangle(23, 25);
            shape.draw();    
      }
}