What is the meaning of the terms high / low coupling in object-oriented systems development or oriented programming to Objects (OOP) ?
What is the meaning of the terms high / low coupling in object-oriented systems development or oriented programming to Objects (OOP) ?
Concept
Design Principle
* ¹ But this is just good practice (design principle), not to say that you can not use high-grade coupling in your project, because it is inevitable in some cases. One should only take care to keep your design as low as possible.
Examples
Considerations
The coupling is necessary, between different modules, what the standards preach is that it is the smallest possible, always evaluating if it is necessary to create a certain dependency or if it can be replaced by a smaller dependency.
Note: The concept of coupling is independent of the programming language or technology used in your project. It's about good practices .
Coupling is how much a class knows of another class.
Ideally, there should be low coupling between classes because classes should be experts for the service they were assigned to.
Low coupling is also desired as it becomes easier to identify class-related problems, which is one of the principles of the object-oriented paradigm.
EDIT
"Knowing from another class" means that the class in question uses methods and / or attributes of some other class. This causes the class to depend on the other class, that is, the behavior of the class will be directly linked to the behavior of the other.
The high coupling between classes makes it difficult to maintain them, because imagine, every time you edit a class you are also changing the behavior of the classes that are coupled with it, if you are not aware of the coupled classes you will be running a big risk of being bugged and unexpected behavior in your program.They talked so much about the definition, so I'll talk a bit about weak coupling:
Introduction
Poor coupling is one of the key requirements for building quality object-oriented (OO) software. The weak coupling measures how much a class depends on, or is related to, another class or subsystem. The ability of a class to inherit the behavior of another (s) is one of the main characteristics of the OO paradigm. The main advantage is being able to create new classes almost for free, taking advantage of another's code. This article discusses these two concepts and shows why inheritance generally helps to compromise weak coupling.
Poor coupling
A class with strong coupling depends a lot (often unnecessarily) on others. This can lead to the following problems [Larman]:
· classes that are difficult to take advantage of, since every time it is used all the others it depends on must be present;
· changes in related classes can force local changes and
· are difficult to understand in isolation.
Common forms of coupling occur through: instance variables, local variables to methods or their arguments, calling services in another class, one class derives directly or indirectly from another or a class implements a given interface. In short, whenever a class references another type in any of the above conditions coupling is occurring. Consider the code:
public class X
{
private ClasseConcretaY var1;
void M1(ClasseConcretaW var2 ) { … }
}
There are two main coupling points, in the instance variable var1, which is of type ClassConcretaY, and in the argument var2, which is of type ClassConcretaW. In these two parts of the code the class X refers to two other concrete classes. This means that whenever this class is used, the other two should be available in the program namespace. In case of Java, the package (s) where they are should be in the classpath.
But referencing other classes always causes coupling problems? The answer is, it depends! Referencing stable and widespread classes is rarely a problem. For example, using the java.util package in a Java program will hardly cause future coupling problems, since any Java runtime environment contains this library. The problem is in unstable, unrecognized classes, that is, in classes that are designed to address the specific problems of the projects.
How to decrease the coupling?
A general rule to decrease the coupling is to "program for an interface and not for an implementation" [Gamma]. In the example above this means replacing the declarations of the concrete classes with declarations of interfaces. Doing so decouples the code from a specific implementation, making it dependent only on an interface. This is not the definitive solution, a good project with good attributions of responsibilities is crucial, but it helps a lot. It is easier to understand a class in isolation than just interfaces and more [Gamma]:
clients (class users) remain unaware of the specific types of objects they use, as long as the objects have adherence to the interface that clients expect, the clients remain without knowledge of the classes that implement these objects; they are only aware of the abstract classes that define the interface.
Source: link