Home > AOP, Java, Spring > Understanding Dependency Injection (DI)

Understanding Dependency Injection (DI)

spring_triangle

Suppose we have an application to do the drawings of different shapes. The following would be the traditional way of implementing it.

di_1

But in this case we have to instantiate Triangle or a Circle when we need to access the draw method. Then there is an another approach to solve this problem using an Interface or an abstract class. Following is the sample code.

package com.technodyne.common;

public abstract class Shape {

public abstract void draw();
}

Now add the following classes extending the Shape abstract class. 

package com.technodyne.common;

public class Circle extends Shape {

@Override
 public void draw() {
 // TODO Auto-generated method stub

 }

}

and


package com.technodyne.common;

public class Triangle extends Shape {

@Override
 public void draw() {
 // TODO Auto-generated method stub

 }

}

And here is the implementation class for the application.


package com.technodyne.common;

public class DiApplication {

public void drawAction(){

Shape shapeTriangle = new Triangle();
 shapeTriangle.draw();

Shape shapeCircle = new Circle();
 shapeCircle.draw();
 }
 }

But still we should do the instantiate of the Circle and Triangle objects. Therefore as a quick alternative, we can add the following method to do our task. This method doesn’t involve in any object creation. It just execute the draw method of the object which is passed to the method.


public void myDrawMethod(Shape shape){
 shape.draw();
 }

Inside this method we have removed the dependency of Circle or Triangle  objects. But still we need to pass the object to the method. Which means that somewhere in this code snippets, we have to do the instantiate. So the proper approach would be removing object creation from the class.

For that, a separate class can be used to execute the draw method.


package com.technodyne.common;

public class Drawing {

private Shape shape;

/**
 * @param shape the shape to set
 */
 public void setShape(Shape shape) {
 this.shape = shape;
 }

 public void drawShape(){
 this.shape.draw();
 }

}

This Drawing class doesn’t do the object creation. When we pass the triangle object to the Setter, it will execute the draw method inside the triangle class. this will allow to insert new objects to the Drawing class as long as that class is a type of Shape. The drawing class doesn’t own the relationship of Shape types.

Advertisements
Categories: AOP, Java, Spring
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: