Can Observer be considered encapsulation break?

Asked

Viewed 146 times

7

Observer use of abstraction and interfaces. Let’s assume that a Observer "assists" the state changes of certain classe and informs them to another external agent, we may consider this a encapsulation break, since an external agent has access to these state changes?

1 answer

9


Some consider that yes, in a sense. This can be observed in a excellent response in the OS. But not in the sense of the question here.

At least what is described in the OS response linked above is the opposite of asked here. It breaks the consumer’s encapsulation, the class that signs a notification is that exposes what it should not for whom it wishes to observe. It even causes memory leakage problems if you don’t know what you’re doing, but that’s another problem.

This problem could be solved without abandoning the standard. The problem is not the standard, it is the adopted solution. This can be seen in another question in the OR.

Other problems

Surely you have bigger problems. The same answer shows some. Another answer back in the O.R. describes another problem. There are those who see more problem and propose a solution with another pattern.

Note that this has more to do with coupling than encapsulation. And maybe the coupling problem has more to do with the question here.

The breaking of the encapsulation occurs only when an implementation detail is exposed that should be internal to the object. In a certain way it is also a abstraction leak. And that’s why this concept is useful.

It’s not enough to be a problem

The question is whether this is so important. The fact is that this pattern has been used without any major problems for quite some time. Academics will find problems, they will cling to every detail. Engineers will accept that not everything is perfect and if it works properly without causing critical problems can be used normally.

Some confusion

The default does not necessarily need to use interfaces.

He does not need to observe change of state, that is a possibility. He observes some action, the change of state is only the most common.

Within what is in question I would question: a method getAlgumaCoisa() that takes a state is encapsulation break? An external agent the class has access to the state. Can’t? Or the problem is just to observe the change of state? If that is, what would be the solution? I do not know. Even if you use another pattern, this access would still occur. The premise of the question does not make much sense.

  • I am going to formulate the question better, I believe I did not express it as I should, but the answer was very enlightening.

  • Careful not to invalidate the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.