10
We often use things we don’t even think about because it’s like this.
I don’t like to put mechanism bumpers on object that is of specific domain.
If I have a screen control or a client who has some action that triggers an event my normal is to use some form of event where the object has a list of subscribers that obviously grows as it gets interested objects (observers) in some state change or behavior that this observed object is willing to warn has occurred, is occurring or will occur. The control mechanism stays in this object and has the cost in it.
I wondered if it is not more interesting to have a separate object of observation, then this object that was observed informs the object of observation that it has available events and the observer objects sign these desired events. This whole object is responsible for the observation mechanism and the objects that communicate do not have their own mechanisms reducing their responsibility, apparently improving cohesion and coupling.
I worry mainly about the object carrying state that is not part of his domain. I know it’s precious in many cases and I shouldn’t worry too much about it. But the question remains whether from the strict point of view of engineering it makes sense to do something like this.
I don’t want to know which is the best, but whether I’m seeing the big picture properly or missed something important.
Only illustrative example well simplified and naively in pseudocode:
class Score
value = 0
Increase() {
value++
Changed(this)
}
Score() {
RegisterEvent(this, Increase)
}
~Score() {
UnregisterEvent(this, Increase)
}
class Observation {
observables[] = new[]
RegisterEvent(obj, method) {
observables[obj << ptrLength + method] = new[]
}
UnregisterEvent(obj, method) {
observables[obj << ptrLength + method] = null
}
SubscribeEvent(obj, method, action) {
observables[obj << ptrLength + method] += action
UnsubscribeEvent(obj, method, action) {
observables[obj << ptrLength + method] -= action
}
}
class App {
static player1 = new Score()
static player2 = new Score()
}
class Screen {
Screen() {
SubscribeEvent(player1, Score.Increase, PaintScore1) {
SubscribeEvent(player2, Score.Increase, PaintScore2) {
}
~Screen() {
UnsubscribeEvent(player1, Score.Increase, PaintScore1) {
UnsubscribeEvent(player2, Score.Increase, PaintScore2) {
}
PaintScore1(obj) { ... }
PaintScore2(obj) { ... }
}
class Fire {
...
Reached(player) {
(player == 1 ? player1 : player2).Increase()
}
}
Would this be the same thing as the Mediator standard? If it is, would the Mediator be a substitute for the Observer? Or they serve different purposes. With the advent of Mediator would the Observer be obsolete? Or would they complement each other? Or would it be wrong for them to complement each other?
Is this the Event Aggregator? And is this just one of the countless things I thought I invented? Dammit Fowler! P
I do not know if I understand very well, the proposal you described has a certain resemblance to the Mediator Pattern or the combination of Mediator + Observer. In case I didn’t understand it right, can you explain to me the difference between what you are proposing and what Mediator Pattern does not solve?
– Tom Melo
@Tommelo edited, see if help answer. I’ve seen the Mediator, but I still have no experience with it. In fact the idea came seeing an implementation of it, but I saw no events. There the doubt may be just what you want to know :)
– Maniero
Ah, now it’s clearer! Another thing you remember is the Event Aggregator.
– Tom Melo
@Tommelo if he thinks there’s an answer putting these things...
– Maniero