Example parsing: Implementation of observer mode in Ruby design mode development, and ruby Design Mode

Source: Internet
Author: User

Example parsing: Implementation of observer mode in Ruby design mode development, and ruby Design Mode

In general, the observer mode should be defined as follows: building a clean interface between the source of news that some object has changed and the consumers of that news.

The observer mode establishes a clean interface between the producer and consumer of the message, so that the coupling between the producer and consumer of the message is abstract. The observer does not know any observer, but only knows that they all implement a common interface. Since the observer and the observer are not closely coupled, they can belong to different abstract layers.

The observer mode supports broadcast communication. The observer sends a broadcast change message to each registered observer. This broadcast mode may cause the observer to be notified cyclically, consuming a lot of resources.

In the broad sense, two roles should be defined in the Observer mode.

Subject: The class that generates the message is subject. In the previous example, the Tester class is used;

Observer: Class of interest to messages generated by subject. In the previous example, the tax bureau and the Social Security Bureau are used;

If the relationship between men and women is analogous to subject and observer, subject should be the girl boy observer secretly loves. The boy is infatuated with the girl, but if the girl is always near, it will be invisible. So the boy said to the girl, "I will wait for you. If you have a home, please let me know. Otherwise, I will keep waiting. Here, the boy is the observer of the girl, and he is interested in the girl's message-whether it is shared by the girl. So when the message "home" is generated, the girl will notify the boy that a love cup is born, but the complete observer mode will be perfectly realized.

As a subject, the following interfaces should be implemented:

  • Add observer
  • Remove observer
  • Notification observer

As an observer, it must implement the specific performance when receiving notifications.

Example:
Class observer with built-in observer mode in ruby. You can use it to implement the observer mode.

Code example:

#-*-Coding: GB2312-*-require 'observer' # observer mode (ruby) example # P class PObservable include Observable end # observer A class AObserver # update method name is required def update (arg) puts "AObserver notified" + arg end # observer B class BObserver # update method name is required def update (arg) puts "BObserver notified" + arg end # The Observer initializes observer_a = AObserver. new observer_ B = BObserver. new # object = PObservable initialized by the observer. new # Add monitoring object obj. add_observer (observer_a) obj. add_observer (observer_ B) # changed by the observer-> This code must exist. Otherwise, the observer obj cannot be notified. changed # notify the observer obj. notify_observers ("Test ")

Output result:

AObserver was notified TestBObserver was notified Test

Articles you may be interested in:
  • Example Analysis of Observer Pattern in Ruby programming in Design Pattern
  • In-depth analysis of the usage of the command mode in Ruby Design Mode Programming
  • Application instance analysis of appearance mode in Ruby Design Mode Programming
  • Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
  • The template method in the design mode applies two examples in Ruby.
  • Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
  • The example explains how Ruby uses the decorator mode in the design mode.
  • Example of using Builder mode in Ruby Design Mode Programming
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses code instances in the proxy mode and decoration mode in the Design Mode
  • Ruby uses the simple factory mode and factory method mode in the Design Mode
  • Application of parsing proxy mode in Ruby design mode development

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.