aspose file tools*
The moose likes Beginning Java and the fly likes what makes this loose coupling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what makes this loose coupling?" Watch "what makes this loose coupling?" New topic
Author

what makes this loose coupling?

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
I found this code snippet in the Sierra and Bates java certification book. From what I can garner from the book, it is loose because the first class, DoTaxes, has knowledge of a member of the second class, SalesTaxRates (salesRate)--I'm sure that's not right. Or is it because the second class has called the doColorado() method of the DoTaxes class? Which brings me to my other question: Since both classes are using instances, variables and methods from each other, what gets called or processed first? It's like going around in circles.



Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
this piece of code is the opposite of loose coupling -> tight coupling...

the two classes are highly dependent from each other (they share instance variables and methods).

In good code it is easy to just change an implementation of something with something else without breaking other code. Now imagine you want to implement a new version of SalesTaxRates. You will have to reimplement DoTaxes as well. And now, think of 5000 classes ;-)


JDBCSupport - An easy to use, light-weight JDBC framework -
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Oh yes, I meant tight coupling and said loose in error. Thank you.

But how about my other question, the one about which process occurs first, "salesRate = new DoTaxes().doColorado();" or "SalesTaxRates str = new SalesTaxRates();" or is this all part of the tight coupling trauma? I mean, if the classes access members from each other, what happens first?

For instance, when class SalesTaxRates creates the salesRate float variable, has the DoTaxes() class been processed yet? Because if it hasn't, then how would the salesRate even be created, since it comes from "new DoTaxes().doColorado()"? And if it has, then how did it create the SalesTaxRates object str without first having the second object? This circuitous thinking is driving me batty!

I think understanding this logic would go a long way towards my Java education.

One last question: Why is there a "new" in front of DoTaxes().doColorado() if salesRates is a float variable?

Thanks for any help at all.

Vonique
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
There is no problem here.

At the time getSalesRate get's called and calls doColorado which then accesses salesRate, this variable will simply be null since it has not been initialized.

Your doColorado method should return a value, by the way.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
Nice to see you again, Vonique. A method called something like "doColorado" looks very suspect to me. It should be something like "foo(colorado)".
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Nice to come back here, Campbell. I always get good answers----so interesting. I finally decided to enrol in a real Java class at the community college, starting in two weeks. Studying on my own is too confusing. It's been great having this site as a resource, though.

I'm sure I will be back, as usual. Thanks for all the help in the past, Campbell. And thanks Sebastian for your explanation. Sometimes I know something but need another explanation to confirm it.

Vonique
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what makes this loose coupling?