Are low bonds and a high degree of adhesion related to each other?

I have to write two versions of the same code. One with low adhesion and high cohesion, and the other with low adhesion, but this time with low cohesion. I really don’t understand what is the difference? How can I have low grip and low grip? They seem so connected that it is impossible to do.

Can someone explain this? Maybe with an example? Thank!

+4
source share
7 answers

In short:

, , , , ( ), , . , , , .

OO - .

, High Cohesion ( - ) - , .

- , ( , , , ) , .. , .

- , , , ...

. .. , . , ...

+1

- , .

. , , , , , - . , , , , , . , , , . , , :

  • ,
  • ,
  • ,
  • ,
  • ,
  • ,

, . , ( -) - ( - ) 21.

  • ,
  • ,
  • ,
  • ,
  • ,
  • ,

( ), . , .

+1

, , , , . , , , .

, , , . , , .

, , . , - , , , . , - , -, , .

0

- - , . :

  • . , (, ). . ( OO). , , - , . , Facade, .

  • . , . , , , , . , Visitor, .

, 3 , :

#include <Document>
#include <Printer>
#include <SpellCheker>
class HighCoupling{
    // This class need other class to be usable
    Document *doc;
    Printer *activePrinter;
    CretaeDocument() { /*...*/ }
    SaveDocument() { /*...*/ }
    PrintDocument() { /*...*/ }    
    CheckSpell() { /*...*/ }    
};

#include <Document>
class LowCouplingHighCohesion {
    // This class don't need other classes
    // This class is a specialist on some task
    CretaeDocument() { /*...*/ }
    SaveDocument(Format *) { /*...*/ }
    LoadDocument() { /*...*/ }
};

#include <Document>
Clas LowCouplingLowCohesion {
    // This class don't need other classes
    // This class do many unrelated things
    CretaeDocument() { /*...*/ }
    SaveDocument() { /*...*/ }
    OpenDocument() { /*...*/ }
    Undo() { /*...*/ }
    ChangeDocumentBackground()  { /*...*/ }
    SearchInDocument() { /*...*/ }
};
0

- .

, . , ; , < > . , .

Cohesion - , . , , , ; , , . , .

. , , , , . , , , , .


"" - . , , . Utility . , . -, , - .

, , , .

0

.

  • , . , SOLID , , Loosely Coupled. , Gof (, Abstract Factory) DI (, MS Unity NInject .NET) . Loosely Coupled , (, , ), .

  • Cohesion -, . , , , , , High Cohesion. ? . , , :

    • - XML
    • - XML

, , , , .

-1

, Low Coupling = High Cohesion.

-1
source

All Articles