The Observer pattern (sometimes called the Publish/Subscribe mode) is one of the software design patterns.
In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes.
This is usually done by calling the methods provided by each observer.
When implementing the observer pattern, it is important to note that the interaction between the observer and the observed object does not reflect a direct call between the classes, otherwise it will tightly couple the observer and the observed object, fundamentally violating the principle of object-oriented design.
Neither the Observer "observes" the object of observation, nor the observer notifies the observer of its own change, should not be called directly.
The observer pattern in Ruby is implemented
1 Module Subject2 3 #Initializes an array that holds the viewer4 defInitialize5Puts"subject initialize."6@observers = []7 End8 9 #plug the Observer into the array (in this article, these observers are actually an instantiated class)Ten defadd_observer ob One@observers <<ob A End - - #remove the observer from the array the defdelete_observer ob - @observers. Delete ob - End - + #notify the viewer - #1. Iterate through all the observers in the array + #2. Execute the Observer's Update method, passing self as a parameter, where self is the observer (The Observer class is instantiated) A defnotify_observers at@observers. Each do |ob| - ob.update Self - End - End - End - in #The person being observed - classEmployee to include Subject + Attr_reader:name,: Title - attr_reader:salary the * defInitialize name, title, salary $ Super ()Panax NotoginsengPuts"Employee Initialize." -@name =name the@title =title +@salary =Salary A End the + #notifies the Observer when calling Salary= - defsalary=new_salary $@salary =new_salary $ notify_observers - End - End the - #observersWuyi classTaxman the defUpdate obj -Puts"Taxman: #{obj.name} now have a salary of #{obj.salary}" Wu End - End About $ #observers - classEmployer - defUpdate obj -Puts"Employer: #{obj.name} now have a salary of #{obj.salary}" A End + End the -Jack = Employee.new ('Jack','Prgramer', 3000) $ jack.add_observer (taxman.new) the jack.add_observer (employer.new) theJack.salary = 3001 theJack.salary = 3002
The idea is to connect the observer and the Observer with a notify_observers .
The observer pattern in JavaScript is implemented
1 //The person being observed2 varObserver = {3 //Subscribe to add an observer,4Addsubscriber:function(callback) {5 This. subscribers[ This. subscribers.length] =callback;6 },7 //UNSUBSCRIBE Delete an observer,8Removesubscriber:function(callback) {9 for(vari = 0; I < This. subscribers.length; i++) {Ten if( This. subscribers[i] = = =callback) { One Delete( This. Subscribers[i]); A } - } - }, the //The publication was changed by the Observer, and a message was released -Publishfunction(what) { - for(vari = 0; I < This. subscribers.length; i++) { - if(typeof This. subscribers[i] = = = ' function ') { +Alert This. Subscribers[i]) - This. Subscribers[i] (what); + } A } at }, - //the object o has observer functionality -Makefunction(o) { - for(varIinch This) { -O[i] = This[i]; -O.subscribers = []; in } - } to }; + - varBlogger = { theRecommend:function(ID) { * varmsg = ' Dudu Recommended posts: ' +ID; $ This. Publish (msg);Panax Notoginseng } - }; the + varuser = { AVotefunction(ID) { the varmsg = ' Someone voted for!id= ' +ID; + This. Publish (msg); - } $ }; $ - Observer.make (blogger); - observer.make (user); the - //observersWuyi varTom = { theReadfunction(what) { -Console.log (' Tom sees the following message: ' +What ) Wu } - }; About $ varMM = { -Showfunction(what) { -Console.log (' mm saw the following message: ' +What ) - } A }; + //Subscribe the Blogger.addsubscriber (tom.read); - Blogger.addsubscriber (mm.show); $Blogger.recommend (123);//Invoke Publish the the //Unsubscribe the Blogger.removesubscriber (mm.show); theBlogger.recommend (456);//Invoke Publish - in //a subscription to another object the User.addsubscriber (mm.show); theUser.vote (789);//Invoke Publish
Observer patterns for Ruby and JavaScript