Why does OOP help programmers develop loosely coupled application?
As far as I know, there is a reason of encapsulating data inside a class that makes objects loosely coupled?
Am I correct? Could you please give me an example?
Thanks
Jack
Re: Why does OOP help programmers develop loosely coupled application?
Quote:
Originally Posted by
lucky6969b
As far as I know, there is a reason of encapsulating data inside a class that makes objects loosely coupled?
Am I correct? Could you please give me an example?
Encapsulation can make a design less complex and more loosely coupled yes. It's because it can reduce the number of parts that are communicating directly.
Let's say the design has four parts 1, 2, 3, 4. If all parts communicate they can communicate in 6 ways. Now say you hide parts 1 and 2 in a class A and 3 and 4 in B. Then 1 and 2 can communicate, 3 and 4 can comminucate and A and B can communicate. That's 3 ways of communication so the complexity was cut in half (reduced from 6 to 3) and the design became less tightly coupled.
Re: Why does OOP help programmers develop loosely coupled application?
Hello nuzzle,
why part 1,2,3,4 would have 6 ways of communication? Sorry, having taken allergy drug, feeling extremely dizzy
Thanks
Jack
Re: Why does OOP help programmers develop loosely coupled application?
Quote:
Originally Posted by lucky6969b
why part 1,2,3,4 would have 6 ways of communication?
If you have four people in a room, and each person shakes the hands of every other person once, how many handshakes would there be? The first person shakes the hands of the other three, so that's three handshakes. The second person shakes the hands of the other two (since he/she already shook the hands of the first person, we don't count that to avoid double counting), the third person shakes the hands of the fourth... and tada! 6.
Re: Why does OOP help programmers develop loosely coupled application?
Quote:
Originally Posted by
lucky6969b
why part 1,2,3,4 would have 6 ways of communication?
In addition to laserlight's reply I list here the 6 ways 4 parts can interact,
1 <-> 2
1 <-> 3
1 <-> 4
2 <-> 3
2 <-> 4
3 <-> 4
After the introduction of classes A and B you have,
1 <-> 2
3 <-> 4
A <-> B
Note that encapsultion is not the only OO way to decouple designs so they become more loose. Many of the so called OO design patterns do that too. Mediator is the typical example of that.
Re: Why does OOP help programmers develop loosely coupled application?
Alright, many thanks to nuzzle and laserlight.