• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java decoupling

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I read that decoupling in java between class and between layers.
i feel that decoupling used to create the independancy between class or between layers so what are the advantage on real time.
when i change one class it will not affect the other class is the defination i read but what is practical advantage for doing that?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
F.e. you created an application that works with database through JDBC. Then you though a little and wanted to use Hibernate! In a coupled project, where all layers are mixed, you will change the whole application. In decoupled one, you will change only DAO-level(data access level) and the other parts of application won't know about that fact.
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stanislav bashkirtsev wrote:F.e. you created an application that works with database through JDBC. Then you though a little and wanted to use Hibernate! In a coupled project, where all layers are mixed, you will change the whole application. In decoupled one, you will change only DAO-level(data access level) and the other parts of application won't know about that fact.


thanks for your info.
i heared about spring framwork is using for decoupling between model and controller, controller and presentaion so in spring there is xml file we are declare model view controller components so whenever the situvation i need to change so i can change what ever by declaratively. same like struts2 also.

you mean that is declarative management of view controler presentation is the decoupling or how if any small example would be great to understand

same like between classes how the decoupling helps because i feel interface implementation is the decoupling but when i declare interface and implementation and the object creation is like

userobjectInt o = new userObjectImpl()

so here i am create the userimple with userint interface so where i am decoupling helps here, i mean in which scenorio in future will help for the decoupling.
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

userobjectInt o = new userObjectImpl()

so here i am create the userimple with userint interface so where i am decoupling helps here, i mean in which scenorio in future will help for the decoupling.

It is not decoupling. This principle is called:

Programming to interface, not to implementation.

 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stanislav bashkirtsev wrote:

userobjectInt o = new userObjectImpl()

so here i am create the userimple with userint interface so where i am decoupling helps here, i mean in which scenorio in future will help for the decoupling.

It is not decoupling. This principle is called:

Programming to interface, not to implementation.





then can anyone please explain decoupling in coding?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

userobjectInt o = new userObjectImpl()

so here i am create the userimple with userint interface so where i am decoupling helps here, i mean in which scenorio in future will help for the decoupling.



In that case you use an Interface (watch your naming, classes, interfaces etc start with a capital letter) but still instantiate the concrete class. So the implementing class depends just as much on the implementation.

To really decouple you would create a factory method that does the instantiation for you.

Check this code



You see, now the implementation is completely decoupled from the rest. You can change the actual class that is returned by the factory method, as well as the full implementation.

What you want to do though is having a controller class inject the dependency into the model classes instead of obtaining them yourself.
You can do that either with a setter or in the constructor.

Check this code:



Now the model class is completely independent from a) the implementation of the user object and b) the creation or obtaining.

There are frameworks that handle dependency injection for you, such as Spring, or Google Guice.

Happy Coding...
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



What you want to do though is having a controller class inject the dependency into the model classes instead of obtaining them yourself.



thanks for your info
absolutely it is happy coding

about your great point controller model injection not make sense but some one from this forum only said that spring have controller model injection so it is one of the advantage of spring over struts2

now i am able to understand the decoupling between classes but between layers how the practical code is? is it like spring to generalise the view and controller model injection or how?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic