What is Class Coupling?

11

I was reading this response regarding the ServiceProvider and ServiceContainer design patterns, however, the AR < Euler01 quoted a pattern that mentions the coupling between classes, and it was at this point that I was asked a question.

This term:

Inter-Class Coupling

Questions

I'd like to know what is coupling between classes?

If possible, I would like examples of PHP or C # illustration, but feel free to give examples in other languages.     

asked by anonymous 28.03.2017 / 23:00

1 answer

11

In software engineering, the coupling is a metric that defines the degree of interdependence between the elements of a module. We could sum it up in one word: Dependency, that is, coupling is the degree of dependence in which one artifact relates to another, consider an artifact as any "thing" that forms part of the final product in the development environment. object, a class, etc.). In the context of classes, coupling refers to how different classes connect to one another.

Low coupling or low coupling : The components of a system are interconnected so that one depends on the other as little as possible.

Strong coupling or high coupling : The components are interconnected in such a way that it is practically impossible to change one without causing side effects throughout or in a large part of the system.

Strongly coupled classes contains a large number of interactions and dependencies, whereas in weakly coupled classes , the opposite occurs, where dependencies between classes are resolved through well defined public interfaces, reducing as much as possible direct dependencies.

One of the developers of the extinct SUN, in a forum, posted the following text (slightly adapted to the context of this answer) in a discussion about coupling:

  

Legos, those toys to fit, would be considered weakly   because you could simply put the pieces together and   build any system you want. However a jigsaw puzzle   has parts that are strongly coupled. You can not get a   piece of a puzzle (system) and fit it into a puzzle   different, because the system (puzzle) is very dependent on the   parts that were built specifically for that design   private. Legs are constructed in a more general way of   so that with the same pieces that you build a house   I can build a car.

It is virtually impossible to develop software without any coupling, but it is desirable that the coupling be as weak as possible.

I'm going to give an example in python because it's the language I'm currently involved in, of course this example has an absurdity only to highlight the coupling problem.

class A:
    def get_hello():
        b = B
        return b

class B:
    def hello():
        print ('Hello World!')            

>>>> A.get_hello().hello()                   
>>>> Hello World!           

See that to execute hello () is called a method of an object returned by another method, the hello () method is called by the get_hello () method, this is bad because it increases the coupling between classes making maintenance difficult, since if one of them is replaced or refactored it will be paid the price of side effects.

In the context of python it is possible to elucidate a little more in this example:

def func(obj):
    obj.method1()  # Ok
    obj.method1().method2() # Bad

In this example, the passed parameter is an object that has a method (method1), which in turn has a second method embedded, depending on that call in that class is what sets up the coupling problem.

  

Good practice for a developer when building a   class is to ask: I am conceiving something more like the layman   with a jigsaw puzzle?

As mentioned in the comments, I suggest that you also study the concept of cohesion. Coupling refers to how artifacts relate to one another and cohesion refers to how the internal components of an artifact relate.

The greater the cohesion, the lower the coupling level and the better the software design.

    
01.04.2017 / 20:25