File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes coupling is lowered, and cohesion is increased Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "coupling is lowered, and cohesion is increased" Watch "coupling is lowered, and cohesion is increased" New topic
Author

coupling is lowered, and cohesion is increased

Anthony Stillwell
Greenhorn

Joined: Mar 24, 2012
Posts: 8
Hi every one. I am working on homework again. Well for hours now and the latest question is as follows:

----- Homework Questions ------

1. How can this code be re-written so that coupling is lowered, and cohesion is increased? Re-write the code as best as you can. Make sure it will compile.



************************************************************************************************************************************************************************************
I finally understand the concept of coupling and cohesion but it is much harder applying the concept to actual code. Coupling refers to the degree to which one class knows about or uses members of another class. Loose coupling is the desirable state of having classes that are well encapsulated, minimize references to each other, and limit the breadth of API usage. Tight coupling is the undesirable state of having classes that break the rules of loose coupling.
Cohesion refers to the degree in which a class has a single, well-defined role or responsibility. High cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility. Low cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities.

I looked at other examples that have been previously posted on this site but still having trouble applying the concept to this code. I know I want Class A and Class B to have minimize references to each other and stand alone if possible. Any direction would be appreciated. Thanks

[Edit: Added code tags (UseCodeTags)]
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Let me raise some random thought:
1. B myB = new B();
myB.doBStuff() ;
What will happen? An instance a is created to invoke doMoreAStuff() method, where a B b instance is created to invoke doBStuff(), where another a is created to invoke doMoreAStuff(). Will this doBStuff() invocation never stop?
I think so. Therefore, I think it is better to avoid this infinite method calls.

2. Regarding to cohension, it involves A to do A's stuff in B. It also involves B to do B's stuff in A. The cohension is low. It is better to have A class to do its own stuff and B class to do its own B stuff.

3. Regarding to coupling, there is a coupling between A and B because B depends on a's doMoreAStuff() and so does A.

I remember when I learn Command design pattern, we can create an extra class called class C, for example. A instance sends a command to C instance. C instance use create B b and invoke the doBStuff() and get sthe result and returns the result to A.
B instane sends a command to C instance. C instances create an A object, which invokes the doMoreAStuff(),gets the result and returns it to B.

I think I need to review Command design pattern in order to answer this question. But in general, A just sends a command to C and gets a result. A does not know what C does. B just sends a command to C and gets the result from A. B does not know what C does.
This can reduce the coupling between A and B.

The good thing is if some implementations change in B, A won't know about it. If some implementations change in A, B won't know about it.

For example,

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I just ran the code by adding a main method with


It throws a stack over flow error. I think the first thing is to update the code, so that the infinte method invocation won't happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: coupling is lowered, and cohesion is increased
 
Similar Threads
What is coupling and cohesion ?
Tight Coupling
Has-A relationship
OO Concepts - Encapsulation, Cohesion, Coupling
Coupling and Cohesion