In this video we will see the new interface of Visual Studio 2010 and IObservable IObserver that they are designed to create a model for push notification. A model of push notification is intended as an area where a provider class that implements the first interface that we have appointed IObservable IObservable interface that sends notifications to all classes implement an interface instead reporter IObserver and listen to the provider class. The purpose of notification is represented by a third class that the class that provides information just for notification.
Imagine the example of a road traffic lights. As we see in this class diagram all cars approaching an intersection must continuously monitor the light from the light to understand, for example if they can go or whether they should stop at the stop sign. When the light changes color all the cars that are unexpected are advised of the change of light and then react accordingly: those who pass the green light instead of the red light will stop. Then this class is nothing but the light provider because we have just mentioned notification to all the cars that are the reporters, changing the color of light. The color of light is the object, the class that provides the notification information and it is light traffic. The added value of a structured model so that the only connection between the provider and the subscription mechanism of the reporter and thus the mechanism for removal of the subscription. Then a reporter to subscribe automatically listens for notifications of provider and for the rest of the mechanism for all the logic that lies beneath the transmission of communication between provider and reporter is responsible for just the first, that the provider, ie our traffic lights. When it is necessary to notify the listeners will be new information for your provider to verify that s'occuperà from its list of reporters to listen if the information in the notification is accurate that is, is a validation of information prior notice and then if you actually Subscribers are able to receive such information. Let us implement our model of push notification through an example that just uses all of these classes.
So let's start from the first element is the structure of traffic lights. The traffic light structure as we have said previously is that the information notified by the traffic lights and then by the provider on all cars that all the objects that are listening that we have a simple structure called traffic light with a member of type string that represents the color the traffic lights. In light of this change will be made to notify all cars. Go ahead and see that we present the traffic class is class provider. The provider class implements the interface IObservable of light that is light, translating in Italian, is a class that was observed and the object of observation is represented by the traffic light.
Visual Studio 2010 This class contains an internal list. This is a black list and a list of observers on a list of objects that have subscribed to the event notification generated by the class of traffic lights. So this is the declaration of list of observers of light traffic. The manufacturer does is create this new list that we have just stated here and create it as observers has not been used as car traffic directly because the class is a class that does not know the type of observer that the observer could potentially also be of a different class than the example and through car or truck through, and so on. Going forward we find the true IObservable interface and its implementation, namely that of the Subscribe (), which takes as a parameter an observer of light traffic. This method allows easy to see how the signature events of the semaphore of a class car of a class through a class of truck. generally of a class observer. Return an item and how they can see IDisposable because this is part of the process of cancellation of the signing of this method is sincere in going to check our list Observers say the internal list of all observers of the events or not the traffic light contains' observer to be added. If you already have then continues otherwise the viewer will be added to the list. The color change method accepts an object of type light traffic light when we manually call this color change through a new traffic light runs in a loop between all observers and if you pass a non-zero value of traffic light, then the observer is notified of the change of the color of the traffic light. Otherwise is instead thrown directly into the class reporter in fact triggered in the first case we see that as the method OnNext dall'help, provides new information to the observer while the latter is triggered the OnError method of notification rather than the presence of a 'fault then we have to close the traffic light method InTrasmission class that represents the end of the transmission between the lights and all observers that the observer is called for each method OnCompleted and the list of observers is then cleaned.
The methods that are implemented within this class are nothing more than what we saw in class, there was traffic lights Subscribe, OnError, OnNext OnCompleted and we see it returning to the classroom and see OnCompleted traffic light, we see OnNext, the OnError method, OnCompleted.
So back down we can see that the method does is Subscribe to subscribe to the provider that implements the interface IObservable. This will add to the list of items covered by this observer of the era of automobile traffic. The OnError method is the notification as we have seen above an anomaly. method OnNext not occur when it detects a change in color of the traffic light while OnCompleted is the notification of the end of the transmissions from the provider class to class reporter. So we see now in the Main method, and let's run this code. In the Main method we are doing is creating a new traffic light and create a series of cars waiting at traffic lights. We have a car that is like this, and subscribing to event notifications of traffic lights. We have two cars, which also endorses the amendments to the class of traffic lights. Finally, we have three cars that are of that type and also makes the events of traffic lights.
These three events are our Visual Studio 2010 auto-generated recall that the method changes the color of the class by providing a traffic light traffic light at our option. The first saràgiallo the second will be instead a red light and the third will be a green light. So the purpose of these examples is that, to change the color of the light, all three cars that have to listen to these changes properly receive the notification and then react accordingly. We can do a test because, as we see below when the color changes such as the color of the light turns yellow then you should trigger the event OnNext, so for every car should appear in this video, this message became the traffic light yellow in this case, with the name of the car has received the notification. Now we go see this other element that triggered the exception that is the moment when you pass an invalid color on the traffic light. Turning over we finally find the car that class is the class that implements the interface IObserver the observer of light, or rather the observer of the light color of the traffic light in this class has a property name and a constructor that takes as a parameter in fact the name of the object there.
Let's see if we were good and if we then run this code, this project and stop breakpoints at strategic points of the Main method and then i launch the project. We arrived here at the point where the color of light has changed to become yellow.
What we want is that all cars, all objects that are listening to the change in the color change, just as they receive notification of the change of color of traffic lights. We check and it is so that all three models have recognized that the color of traffic lights and turn yellow. Let us go forward by pressing a button. We arrived at this point here: the traffic light turned red when we see all three cars have recognized this change of light and that is how all three cars have now recognized that color is red. If we further continue the light has turned green, all three models have recognized that the light is green we go again.
That have arrived here at the end of the transmissions of all three models of cars should know that the broadcasts were interrupted by the provider class to all observers actually all three models have recognized that the programs have been closed. For completeness, we could add another case that is the case, ie when the error is passed to change the color null. Let's just leave this code and see how they behave in class listening. Here, you actually listen to all the objects in the traffic lights have recognized that there was a fault in the transmission and fail to recognize the traffic light. Finally, if we want to deepen and IObserver iObservable interfaces, there is nothing left to go to the MSDN version of Visual Studio 2010 and visit this address where we can see the English version of the class documentation IObservable. We find, however, also refer to the bottom class IObserer so we can document and investigate the topic at will.