• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

how can be achieve loose coupling.

 
Ranch Hand
Posts: 100
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am confused in loose coupling in java, as I know and read, I got to know that loose coupling can be achieve in two ways:
1)by encapsulation 2) by Interface ...am I right?


This is by encapsulation:1part



output is car....


2part if give value is bike then output is bike no need to change code; only value change lp.Setcar("bike");
whole code as it is...






output is bike..


but by interface , I can not understand, how can I achieve loose coupling by interface, can anyone tell and explain me with programming example?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both the example is same. There is no difference. And also neither of them showing loose coupling..

Also there are several issues in this.. ex: Class name always start from capital, Setcar should be setCar..

Here is an example below you can see vehicle class is an interface and drive is a method which can be used with any instance
of the class which implements Vehicle..







 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer this Java Programming Style Guide http://www.javaranch.com/style.jsp for naming convention.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the code example needs to be a little larger than what you showed in your "Loose" class in order for you to start to see and understand how to minimize coupling. Others might have better examples, but this was the first (simplest) example that popped in to my head. I'll start with an implementation that has high coupling and then refactor it to encapsulate information and use dependency injection.

Imagine you have a mechanic. One of the things this mechanic does is checks tire pressure on car tires. We could try modeling that like this:



The above example is not loosely coupled: the Mechanic knows about Tire pressure, even though that's an attribute of Tire, which in turn belongs to Car; and there is no way that we could build a Car with different tires (e.g. with a different initial pressure or different low pressure threshold). This is bad primarily because if anything about the Tires changes, the Mechanic class (two levels removed from Tire!) may be impacted. However, if we did something like this, we could reduce the coupling:



In this example, you can see that the dependency that Car has on Tires has been injected; instead of creating its dependencies itself, Car is given its dependencies at construction time. This makes it very easy to create Cars with different types of Tires. It is an especially useful concept for writing testable software, but I won't go into that right now. It works extremely well with interfaces: you expect an interface at construction time and let the context determine what concrete type it should be. For example, say Tire was an interface and we had AllSeasonTire and WinterTire as concrete implementations. When the Car was assembled, perhaps we find that it is late Fall. We could then decide to build the Car with four WinterTires (if the customer paid for them) instead of AllSeasonTires.

Similarly, the coupling between the Mechanic and the Tires is gone. Now, the Mechanic only talks to the Car. If we change the Car and add additional components to it that need to be checked to ensure the Car is in working order, the Mechanic doesn't need to care (the class doesn't need to change). If we change the type of Tires we used to assemble the Car to have different minimum pressures, the Mechanic isn't affected.

Hope that helps.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic