Wednesday, June 1, 2016

High Conhension(亲密度), Loose Coupling (联结)

Effect on how flexible and maintainable your software

High cohesion increases the probability that a component can be reused in more places.

  • stuck together well , clear , closely, easy understand. e.g. DAC all related to Database.
  • e.g. Low Conhension -  EmalMessage has Login

Loose coupling allows components to be used independently from other components.  e.g IPOD

  • Tight Coupling, 2 classes are tight together (Class A instantiate object Class B), e.g. motherboard need to depend RAM, Processor.
  • Tight coupling increases the maintenance cost as it is difficult and changes to one component would affect all other components that are connected to it.
  • Solution, add interface in between,
  • Lego (loose coupling) , Puzzle (high Coupling)

So, next time you are writing a class, think to yourself: Am I writing a Lego or am I writing a puzzle piece?

No comments:

Post a Comment