Example analysis the realization of the observer pattern in the development of Ruby design Pattern _ruby Special Topic

Source: Internet
Author: User

In general, the definition of the observer pattern should be this: building a clean interface between the source of news that some object has changed and the Consumers O F that news.

The Observer pattern establishes a clean interface between the producer and the consumer of the message, thus making the coupling between the producer and the consumer of the message abstract. The observer can not know any observer, it only knows that they have implemented a common interface. Because the observer and the observed are not tightly coupled, they can belong to different levels of abstraction.

Observer mode supports broadcast communications, and the observer sends a broadcast change message to each registered observer. This mode of broadcasting may cause the observer to be circular and consume a great amount of resources.

2 roles should be defined in a generalized observer pattern.

Subject: The class that generates the message is Subject. In the previous example is the tester class;

Observer: Class that is interested in messages generated by subject. In the example of the last chapter is the Inland Revenue Department, the Social Security Bureau category;

If the analogy between subject and observer,subject in the relationship between men and women should be the girl who observer the boy's crush. The boy is infatuated with the girl, but the girl always if near if leave, conceal cover. So the boy said to the girl: I will wait for you, if you have a destination please notify me, otherwise I have been waiting. Here the boy is the girl's observer, he is interested in the news that the girl produces-whether has the destination. So when there is a fate of the message produced, the girl will inform the boy, a love Cup is born, but the complete observer mode will be achieved perfectly.

As subject, the following interfaces are what they should be implemented:

    • Increase Observer
    • Remove Observer
    • Notify Observer

As a observer, it needs to realize the specific performance of receiving notification.

Example:
A class Observer that implements the observer pattern in Ruby. It can be used to implement the observer pattern.

code example:

#-*-coding:gb2312-*- 
 
require ' observer ' 
# Observer mode (Ruby) Use example 
 
# observed by P 
class pobservable 
include Observable 
 
End 
 
# Observer a 
class Aobserver 
# Update method name is required for 
def update (ARG) 
  puts "aobserver was notified "+ Arg end 
" 
 
# Observer B 
class Bobserver 
# Update method name is required 
def update (ARG) 
  Puts "Bobserver was notified" + arg end 
 
# Observer initialization 
observer_a = aobserver.new 
Observer_b = bobserver . New 
 
# The Observer initializes 
obj = pobservable.new 
 
# Add a Monitor object 
obj.add_observer (observer_a) 
obj.add_ Observer (Observer_b) 
 
# The observed changed-> this code must be otherwise unable to be notified to the Observer 
Obj.changed 
 
# NOTIFY the Observer 
Obj.notify_ Observers ("Test") 

Output results:

Aobserver was notified that test
Bobserver was notified of the test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.