Architectural pattern: publisher-subscriber |
Written by Mottola Michele - Italy - Reggio Emilia |
Tuesday, 15 September 2015 13:54 |
Last Updated on Tuesday, 15 September 2015 14:10 |
IntroductionThis pattern is also know as ’Observer’ (GoF). The mainly difference is that in architectural patterns there is a distinction for what is a components and what is a connectors. Here i want show just a basic working without this distinction. Besides i want show how implement it using the API of java, utility class that simplify the java implementation. What is the problemI have one or more objects (observer or listner or subscriber) that are interested to know when there are some events from another object (subject or publisher) and they want be informed when it happen. When that event happen the subscriber can perform their action that can be different from subscriber to subscriber. How the publisher-subscriber workThe first thing to do for the subscribers (or observers) is registering to the publisher (or subject) so they can be notified when a event happen. To do that they just use attach() method of ConcreteSubject. When there is an event (that maybe also an internal change of state), in the ConcreteSubject, it notify all subscribers registered to it using notify() method. Internally this method action a call to all update() method of all subscrivers. Then, the update() methods perform their action (for example can call getState() method of their publisher, or doing any other action). An example of implementation using java APIAs i told, java has a utilty class that provide an API for the observer. The first is the interface java.util.Observable and the second is the class java.util.Observable. Now i want show an example using these. In this example i want show that when a boiler get a temperature of 100 or 200 degree, it notify this to the two sensors. So the sensors get the temperature of boiler and perform their actions. To start we need to add the two subscriver to the bublisher using addObserver() method. Boiler.javaSensor1.javaSensor2.javaIt is to notice that when i set the state of boiler to 100, before calling the notifyObserver() method, i need to change the state of boiler using setChanged() method. If i don’t use setChanged() method the notifyObserver() doesn’t work. Basically that method mean that in boiler there was an event. so it’s ready to notify listeners. When notifyObserver() has finished, it internally call clearChanged() that mean the object is set as ’not changed’. Test.javaOutput
i am sensor2 and the temperature is 100 and i perform my action |
Comments
thought this put up used to be great. I don't recognize who you might be but certainly you are going to a well-known blogger should you aren't already.
Cheers!
write a litte more on this topic? I'd be very thankful if you could elaborate a little bit more.
Kudos!
find out more about the issue and found most individuals will go along with your views on this website.
how could i subscribe for a blog web site? The account aided me a appropriate deal.
I have been a little bit familiar of this your broadcast offered brilliant transparent concept
the reason that if like to read it after that my contacts will too.
this information.
quick shout out and say I truly enjoy reading through your posts.
Can you recommend any other blogs/websites/forums that
go over the same subjects? Appreciate it!
found something which helped me. Many thanks!
RSS