java - Is this code abusing the Observer Pattern? -


If an observation object informs its observers, then down one or more variables (animals) to a new animal Has it been an improper use of Observer pattern since the Observer to finish all the four animals, when only one sub-group of them has changed? If so, how can it be decided? In addition, how can an interface be made, which will specify that the Observer has to set those specific animals and whether the supervisor inspectors have to inform that if those animals have been changed? Thanks in advance.

  implements the Public Sector Supervisor Observer {Public Zero Update (Overview, Object O1) {SetHipo (OHAHpo ()); SetBassetHound (o.getBassetHound); SetOstrich (o.getOstrich ()); SetAntelope ((o.getAntelope ());}}    

  • update only A subset

    Most of the time, this is not a problem, there are some instructions that will refresh the values. However, if you want to update subset, you can You should use the second logic of the update and set it in a discriminatory manner that will tell you what the update is. You can use a set of these types of objects

    • Seeing specific animals

      Often, you will simply start the code of your supervisor with (if! Should_rebure (x)) return; where should_observe indicates that the object will be seen.

      Another way of doing this is to observe an observer that will become an oberver A quick example is easy to understand:

        public class overviewAnimal Observe Observavar Observer Vrhwar (private class & lt ;? & Gt; animal; // constructor public synchronized d void update (Overview, Object arg revealed) {if (! Animal.isInstance (arg)) return; // Right animal set not changed (); NotifyObservers (Arg); }} Observer observer opinion obsHippo = new observation (hippo class); Obs.addObserver (obsHippo); // If you have changed the hippo, you want to notify you, then use the obpippo   

      Sometimes it is easy to completely rectify the supervisor pattern: it is more The control and your viewable can be an interface so that you do not need a sub-type.

Comments