Dependiency Injection Explained
Let us assume that interface A has a method doAction(). this doAction() requires an instance of a class implementing the interface B. Also let's assume that we have class A1 and A2 which implements A and class B1 and B2 which implements interface B.
In the do() mehod in Class C these classes will be used in this manner.
public void do()
{
A a = new A1();
a.doAction();
}
for this to work A1 should asociate with an instance of B.
typical implementation would be
class A1 implements A{
B b = null;
public A()
{
b = new B1();
}
public void doAction()
{
//uses b
}
}
The problem with this definition of A1 and B1 is that B1 ise intimately coupled in the definition of Class A1. A1 has to no which instance of B is used (ie B1)
One step forward is to have a setB() method in the A interface and let the calling class tell Which implementation of B to be used. This looks more configurable, but effectively we are moving the decision to class C.
Now do() method will change to following
public void do()
{
A a = new A1();
a.setB(new B1());
a.doAction();
}
the difference is instead A1 determining which B to be used now the method do() is determining
With dependency injection this decision is left to the container. Dependency injection supported container before giving the Class A1 to be used by Class C it will set the instance of B1 to b. Now all the instances of the classes As, Bs and Cs are decoupled.
But we need a way to inform the container that when giving an instance of A1 it should set the b to B1 before providing to the application.. This is called wiring and is done by the dependency injection container. In spring this can be specified in XML.
Why use DI
if TDD(Test Driven Development) is used, the advantage gained when creating test cases alone justifies the use of Dependency Injection . If for testing and production different implementations of B is requuired then it can be easily done if DI is used. An instance of a mock object MockB can be used easily if DI prinicpal is used. With the earlier two implementation with the first one it's impossible to use MockB and in the sescond instance MockB can be used to test doAction() method but cannot be used to test do() method.
Blog Archive
Search This Blog
Saturday, January 26, 2008
Thursday, January 24, 2008
Importing Existing Projects to Eclipse
Using Eclipse 3.3.1.1
There are various ways to do this task. The one I have adopted and worked is as follows.
Create a Workspace for Eclipse or use existing workspace.
Unzip the Project if it's a zipped folder in to a folder
File > Import > Existing Project into workspace > [copy project into workspace - this is optional]
This will create a Project in the eclipse workspace.
Running Web Project in Tomcat whithin websphere workspace
If creating a new project, create the project as a dynamic web poject and then in the wizard Tomcat can be decided as the target platform. Then right click on TomCat server in the server view and add the project.
If trying to run an existing project, when right clicked on the server it won't give the option to add the project to the server. To make it happen.
1. Create a new Web Project.
2. Copy the .Settings folder from that project to the existing project folder and change the project name, classpaths and context
3. copy the entries in .project and .classpath into the existing projects corresponding files.
4. change the output folder in .classpath file to web/WEB-INF/classes (or appropriate folder)
5. copy .settings folder in the new Web Project folder into your existing projects folder and change the values appropriately.
When the project is added to tomcat in the workspace
workspace1\Servers\Tomcat v5.5 Server at localhost-config in server.xml following line will be added.
There are various ways to do this task. The one I have adopted and worked is as follows.
Create a Workspace for Eclipse or use existing workspace.
Unzip the Project if it's a zipped folder in to a folder
File > Import > Existing Project into workspace > [copy project into workspace - this is optional]
This will create a Project in the eclipse workspace.
Running Web Project in Tomcat whithin websphere workspace
If creating a new project, create the project as a dynamic web poject and then in the wizard Tomcat can be decided as the target platform. Then right click on TomCat server in the server view and add the project.
If trying to run an existing project, when right clicked on the server it won't give the option to add the project to the server. To make it happen.
1. Create a new Web Project.
2. Copy the .Settings folder from that project to the existing project folder and change the project name, classpaths and context
3. copy the entries in .project and .classpath into the existing projects corresponding files.
5. copy .settings folder in the new Web Project folder into your existing projects folder and change the values appropriately.
When the project is added to tomcat in the workspace
workspace1\Servers\Tomcat v5.5 Server at localhost-config in server.xml following line will be added.
Subscribe to:
Comments (Atom)