Skip to content

Instantly share code, notes, and snippets.

@dimabory
Last active September 5, 2022 08:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimabory/140223715fe24025eeacc78251f063b0 to your computer and use it in GitHub Desktop.
Save dimabory/140223715fe24025eeacc78251f063b0 to your computer and use it in GitHub Desktop.
coupling, cohesion, encapsulation

http://wiki.c2.com/?CouplingAndCohesion

Low Coupling

Coupling refers to the relationship of a module with another module. A module is said to be highly coupled with another module if changes to it will result to changes to the other module. And a module is said to be loosely coupled if a module is independent of any other modules. This can be achieved by having a stable interface that effectively hides the implementation of another module.

Benefits of low coupling are

  • maintainability – changes are confined in a single module
  • testability – modules involved in unit testing can be limited to a minimum
  • readability – classes that need to be analyzed are kept at a minimum

High Cohesion

Cohesion refers to the measure of how strongly-related the functions of a module are. Low cohesion refers to modules that have different unrelated responsibilities. High cohesion refers to modules that have functions that are similar in many aspects.

The benefits of high cohesion are

  • Readability – (closely) related functions are contained in a single module
  • Maintainability – debugging tends to be contained in a single module
  • Reusability – classes that have concentrated functionalities are not polluted with useless functions

Principle of Encapsulation

Encapsulation is synonymous to information hiding. It is the process of concealing the internal representation and implementation of a class. Encapsulation shields the class from misuse and thus increases the resuability of the class. That is, it hides the implementation such that the internal structure of a class is free to change without affecting the implementation of the classes that use this particular class.

COUPLING

PROBLEM:

How to support low dependency, low change impact, and increase reuse?

WHEN ARE TWO CLASSES COUPLED?

  • Common forms of coupling from TypeX to TypeY:
    • TypeX has an attribute that refers to a TypeY instance.
    • A TypeX object calls on services of a TypeY object.
    • TypeX has a method that references an instance of TypeY (parameter, local variable, return type).
    • TypeX is a direct or indirect subclass of TypeY.
    • TypeY is an interface and TypeX implements that interface.

COHESION

PROBLEM:

How to keep objects focused, understandable, and manageable, and as a side-effect, support Low Coupling?

LOW COHESION

  • A class with low cohesion does many unrelated things or does too much work. Such classes are undesirable; they suffer from the following problems:
    • hard to comprehend;
    • hard to reuse;
    • hard to maintain;
    • delicate; constantly affected by change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment