Tuesday, December 16, 2014

Dependency Injection in Java

Source picture: http://mealsandmovesblog.com


It will be nice if we know what dependency injection is before we implement it in Java .

What is Dependency Injection?

Dependency Injection is a design pattern.

Every component of an application have a dependency with another component. Let me explain with a simple example below :

 public class Jacket {  
    String color = "blue"+"#87CEEB";   
    public String getColor(){  
        return color;  
    }  
 }  

"Jacket" has a dependency with String. In other word, Jacket can not be created if String object does not exist.
Jacket has tightly coupled with String because Jacket knows how to create String object. Problem will occur if the object creation is complex, and not as a simple as String object. For more complex example, see "Connection" object creation below :

 public static void main() {  
           Connection con = null;  
           String url = "";  
           String user = "";  
           String password = "";  
           try {  
                Class.forName("oracle.jdbc.driver.OracleDriver");  
                try {  
                     con = DriverManager.getConnection(url, user, password);  
                } catch (SQLException e) {  
                     // exception statement here  
                }  
           } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
           }  
      }  

You can imagine if that code appear in every class, your code would be a mess.

From two examples above we can resume the problem of dependency :
  1. High Coupling
  2. Low Cohesion
Dependency Injection comes to solve those problem. 

How dependency injection works?
1. Create String object color outside Jacket
2. Create Jacket object
3. Inject  color Object through constructor or setter method

  So practically, here is the example of dependency injection :

 public class Jacket {  
    private String color;  
    public Jacket(){  
    }  
    public Jacket(String color){ //inject dependency through constructor  
       this.color = color;  
    }  
    public void setColor(String color){ //inject dependency through setter method  
       this.color = color;  
    }  
    public String getColor(){  
        return color;  
    }  
 }  
 public class Main(){  
     public static void main(String []args){  
        String color = "blue"+"#87CEEB"; // create color String object  
        Jacket jacket = new Jacket (color); //inject through constructor  
        Jacket jacket2 = new Jacket();  
        jacket2.setColor(color); //inject through setter method  
    }  
 }   


Next post we will look dependency injection in Spring Framework.


Happy coding :)



Saturday, May 10, 2014

Google Map V3, Multiple Markers with its InfoWindows (Balloon)


I hope this post will help you because even on google sites I did not find tutorial about this yet.

Lets Rock !!

1:  var markerArr = [  
2:          ['Bondi Beach', -33.890542, 151.274856],  
3:          ['Coogee Beach', -33.923036, 151.259052],  
4:          ['Cronulla Beach', -34.028249, 151.157507],  
5:          ['Manly Beach', -33.80010128657071, 151.28747820854187],  
6:          ['Maroubra Beach', -33.950198, 151.259302]  
7:         ];  
8:   var mapProp = {  
9:      center:new google.maps.LatLng(-33.890542, 151.274856),  
10:      zoom:5  
11:      };  
12:   var map=new google.maps.Map(document.getElementById('MAP_CANVAS') //fill with your id  
13:      ,mapProp);  
14:  //Loop markers  
15:   for (var i = 0; i < markerArr.length; i++) {  
16:    var dot = markerArr[i];  
17:    var myLatLng = new google.maps.LatLng(dot[1],dot[2]);  
18:    var infowindow = new google.maps.InfoWindow({  
19:     }); //Just create InfoWindow Object  
20:     var marker = new google.maps.Marker({  
21:       position: myLatLng,  
22:       map: map,  
23:       title: dot[3],  
24:      html : dot[0] // info window content,  
25:     });  
26:     google.maps.event.addListener(marker, 'click', function() {  
27:      infowindow.setContent(this.html); //yeah got its content  
28:       infowindow.open(map,this);  
29:      });  
30:   }  



Wednesday, May 7, 2014

Monday, May 5, 2014

Thursday, May 1, 2014

Java Random

Simple playing random integer with Java :

int randomInt ;
Random randomGenerator = new java.util.Random();
randomInt = randomGenerator.nextInt(100);  // integer random range is between 0 until 100


System.out.println(randomInt );

You can also play with other data types :

System.out.println( randomGenerator.nextBoolean());
System.out.println( randomGenerator.nextDouble());
System.out.println( randomGenerator.nextFloat());
System.out.println( randomGenerator.nextLong());
System.out.println( randomGenerator.nextGaussian());