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!
In short:
, , , , ( ), , . , , , .
OO - .
, High Cohesion ( - ) - , .
- , ( , , , ) , .. , .
- , , , ...
. .. , . , ...
- , .
. , , , , , - . , , , , , . , , , . , , :
, . , ( -) - ( - ) 21.
( ), . , .
, , , , . , , , .
, , , . , , .
, , . , - , , , . , - , -, , .
- - , . :
. , (, ). . ( OO). , , - , . , Facade, .
Facade
. , . , , , , . , Visitor, .
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() { /*...*/ } };
- .
, . , ; , < > . , .
Cohesion - , . , , , ; , , . , .
. , , , , . , , , , .
"" - . , , . Utility . , . -, , - .
, , , .
.
, . , SOLID , , Loosely Coupled. , Gof (, Abstract Factory) DI (, MS Unity NInject .NET) . Loosely Coupled , (, , ), .
Cohesion -, . , , , , , High Cohesion. ? . , , :
, , , , .
, Low Coupling = High Cohesion.