spring - Regarding Dependency Injection -


interface car {     public void tyres(); }  class bmw implements car {     public void tyres() {         system.out.println("bmw");     } }  class hmw implements car {     public void tyres() {         system.out.println("hmw");     } }  public class dependencyinjection { private car car;      public void draw() {          this.car.tyres();     }      public void setcar(car car) {         this.car = car;     }      public static void tyres1(car c) {         c.tyres();     }      public static void main(string[] args) {         car car = new bmw();         tyres1(car);//point 1         dependencyinjection dep=new dependencyinjection();         dep.setcar(car);//point2         dep.draw();     } } 

i want clarification advantage have creating dependency injection @ point 1 , point2.please explain me in detail new spring???

this not spring specific design principle, , it's hard grasp in snippet you've provided not have notable separated pieces, becomes more helpful systems grow.

by removing reference concrete class eliminate "type coupling" means internal class can remain "closed": won't have change if implementation changes of involved class , client code can use different ways adapt needed without inner class needing change or aware of implementation. helps ensure method body within class focusing on role , specific job rather being getting tangled in other implementation (which helps in defining should considered part of api).

you should read concept of solid classes: http://en.wikipedia.org/wiki/solid_(object-oriented_design)

the immediate benefit in experience allows far more complete, isolated unit tests important if part of development process (which recommended systems grow).


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -