• Post Reply Bookmark Topic Watch Topic
  • New Topic

Composition- HAS-A relationship - loose coupling are same?  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are composition, has-a relationship and loose coupling related to each other? Also is coding to interfaces related to any of these?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A has-a relationship simply means that one object has a field that references another object. This is also called association. Composition is a special kind of association in which the object "owns" the object its field refers to. This means that the outside world can't access the owned object, and all interaction has to take place through the owner object. For instance, an ArrayList is composed of an array: internally it has a reference to an array, but there's no way for the outside world to access the array directly.

Coupling refers to how much two types know of each other. The less one type needs to know about another type, the looser the two types are said to be coupled. A class that needs to access private, protected or package private members of another class is much more tightly coupled to that class than a class that only needs to access public members. A class that depends on the implementation details of another class is much more tightly coupled to that class than a class that only depends on the guarantees made by the class' API.

Programming to an interface helps reduce tight coupling, because you're limiting your class to accessing only the members declared by the exposed API. It also allows your class to be much more flexible and reusable, since you can swap the types of your dependencies without the class breaking.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  This means that the outside world can't access the owned object, and all interaction has to take place through the owner object.


How is that possible? Can you give a code example?
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:. . . How is that possible? Can you give a code example?
Easy. You give the field private access. You don't provide any set and get methods for that field.At no time do you return the backing array. The exception thrown is usually some subtype of IndexOutOfBoundsException.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Imagine a game of Tic Tac Toe. Let's write a Board class:

As you can see, Board is composed of a Mark[][]. This field is private, and none of the methods of Board return a reference to this array. This is important, because if the outside world had a reference to Board's array, they could modify it so that important assumptions no longer hold (such as the length and the width of the board being equal to the size of the board). They could also place marks in locations where there are already marks. The only way to modify the array is through methods of Board, and these methods assure that only valid modifications are made.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!