How does Design Pattern Observer work and how does it implement it?

Asked

Viewed 5,221 times

21

I am in doubt regarding Design Pattern Observer, when its working and how to use(present practical example).

  • View @Mubarak’s reply (http://stackoverflow.com/questions/13744450/interview-when-do-we-use-observer-and-observable) and the others as well!

1 answer

19


Introducing

When you like a magazine a lot you can go on the stand every day to see if the new issue has arrived or can make a subscription and receive at home. The observer pattern is exactly the signature of something you want to receive when something happens. That is, whenever a class needs to know that an action has occurred or a state has been changed in another class (in theory it could be in the same class, but within itself there are simpler mechanisms to use).

You have to keep checking to see if something happened consistently doesn’t work out. It will consume a lot of processing and possibly you will end up missing the exact moment an event occurred. It is impractical, the program needs to know exactly when something important occurred and be notified of it immediately.

There are some ways to implement this although purists will say that if you do it differently than it was officially established by the standard it is not exactly the Observer standard. I will not cling to pure implementation.

Functioning

Basically you have two agents, the observable class (known as Subject) and the observer.

In the observable class you need to have a status: the list that stores subscribers; and three behaviors: who adds a new subscriber to the list, who withdraws a subscriber, and who notifies the list of subscribers that some event relevant to them has occurred.

In the observing class you need an action to be taken when you are notified that something that interests you has occurred.

Diagrama padrão Observer

It is a way of encapsulating behavior, ensuring that other objects know something you determine without needing to know details of implementing this class. If you want a more formal definition, see on Wikipedia, has even an example in Java.

Use

It is used when you need to know that something happened in your application through itself or libraries that eventually keep watching if external services to the application have something to notify.

In the first case it can be a calculation that has occurred, the termination of an operation, a validation error or other type, a change caused on the screen, a state of an object that has been changed, an object that will be destroyed, etc.

In the second case the observable classes on the surface work in the same way although the specific implementation can escape this way since it needs to "talk" with external server Apis and the operating system that probably do not implement this standard exactly this way, only have some other form of signaling. You use libraries that notify your application on demand when you receive signals from devices (keyboard, mouse, network, etc.), operating system information and its components (file or file system modification, exchange or start/end of software or hardware component, data communication, finally, several actions that occur on that machine) and operations that occurred in services (modification of a table in the database, receiving a request on a web server, etc.).

Standards that handle user interfaces often use a lot of Observer Pattern, be it MVC, MVP, MMVP, etc..

An example

Think of a game that the enemy being killed needs to update the score. The scoreboard needs to sign the enemy’s death notification to change his state. The enemy will have methods for interested parties to sign and cancel the signing of the death event and will have a method that notifies all these subscribers. When this object of the enemy has the death decreed by some criterion shall fire this latter method.

The scoreboard object can be one of the subscribers. It can call the subscription method in the subscriber list. This object will certainly have a method that will be invoked when the event occurs.

Note that any part of the application can make the signature of this event, including the enemy’s own object. This may not be as common, but it might make sense. The death of this enemy, for example, can trigger the destruction of the object and ensure that the subscriber list is destroyed along with it. And it may be that the destruction of the object is another event. It may still be that the forced cancellation of the registration generates another event. Anyway, you can create an event whenever you think there is "someone" wanting to know that you did something in this object.

To the annoyance of the purists the enemy class could make the signature of the scoreboard, because it may be that this class knows of the existence of the scoreboard but not the other way around. That is, the enemy knows he needs to change the score when his death occurs but the score does not know everything that can happen that changes it. Evidently the scoreboard needs to have an action method prepared to be invoked by an event.

Purists would be more comfortable if you had a separate class, possibly a mediator to control these relations between observables and observers. And you get to have something that works analogously to an MVC.

Using in Java

You can make your own implementation of the standard using the criteria and algorithm you find best.

You can choose that the notification signature occurs based on the observing class as a whole (calls a standard method of this class set in interface) or through specific methods. The second case was very complicated to do manually in Java until version 7 which did not have Amble. This form is much more flexible and powerful than the first. I may ignore something but I’ve never liked the first way the observer needs to know what to do with generic notifications. And an important note: some people will say that the second way is not Observer.

The default is so common that Java has it already implemented. The most common is to use the abstract class Observable and the interface Observer existing in the framework. The first already has all the methods to fulfill the role of the standard (not only the minimum necessary) and has a standard implementation of the subscriber list for you not to worry.

Some languages go further and put ease in themselves, as is the case with event of the C#.

Have an example for Android in that reply.

Completion

I could go on detailing, giving more examples, but the internet is already full of material. I find it interesting to have some basic information here, but it would be an exaggeration to have a complete approach. Anyway, I think it fits complementary answers of details that I left aside.

  • 4

    If you like od Pattern Observer and realized that programming is not for you and you are thinking of changing profession, has a link to you on the subject: http://patternobserver.com/ :D

Browser other questions tagged

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