3
//Ex1: (passing by object)
class A {
}

class B {
   void foo(A a) {
      <do something with a>
   }
}

//Ex2: (composition)
class C {
   A a
   void foo(){
      <do something with a>
   }
}

My question is: which pattern has lower coupling? And which pattern is more preferred in the real world ?

  • Depends on the situation, each has it's own benefits/weaknesses – dann.dev May 21 '12 at 09:27
  • 1
    If you pass an object, you can pass a different object for every method call. So there is a semantic difference to having an attribute. What shall "lower coupling" mean, btw? If you try to solve the same problem, with an attribute you would always need to call a setter before using a different A first. – user unknown May 21 '12 at 09:34
  • 1
    I'd prefer class B but instead of giving foo an A, I would give an Interface that is implemented by A. That way would be even less coupled. – tgr May 21 '12 at 09:36
  • 1
    `B.foo()` is coupled to `A`. The entire class `C` is coupled to `A`. Which do you think is 'less coupled'? – user207421 May 21 '12 at 09:42

3 Answers3

1

I will try to explain loose-coupling.

  1. Program in interface, that gives use the possibility of passing objects of different type which implements the same interface during runtime, and here classes from different inheritance tree can implement the same interface.

eg:

 Animal is the Interface

    Dog class implements Animal
    Cat class implements Animal
    Lion class implements Animal

   /////////////////
   calling method
  /////////////////

   callAnimal(new Dog);



  /////////////////
   called method
  /////////////////

  public void (Animal a){

   // some code

  }

Encapsulate the behaviour which keeps changing.... into Abstract classes, or Interfaces, so it will be easy when changes comes, and as it is loosely coupled, there are less chances for the code to break.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

I would say neither really has lower coupling, it's more implementation dependent. The best simple explanation of coupling, is that if the properties of class A change, how much will you need to change class B. In this case, the method foo will mostly likely have to make the same changes in both scenarios.

As for real world, it's completely dependent on the situation. Is class A only used in class B? Is it a web app, will you need to inject or outject class A? is class A going to be a singleton? What will be using class B? What does each class even do?!

In summary you can't make an argument either way until you look at the function of each class and how they will be used (that's where cohesion comes into it).

dann.dev
  • 2,406
  • 3
  • 20
  • 32
0

IMHO the second option (composition) is preferred, because:

  • The caller doesn't need to know about A
  • B is free to change its implementation to use a class other than A to do its job (without affecting/recompiling calling code)

The first option (passing by object) creates greater coupling between the caller and A (although B is coupled either way).

Bohemian
  • 412,405
  • 93
  • 575
  • 722