Example analysis of the implementation of observer pattern in Ruby design pattern development

Time:2022-1-5

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

The observer pattern establishes a clean interface between message producers and consumers, which makes the coupling between message producers and consumers abstract. The observed can not know any observer, it only knows that they all implement a common interface. Because the observer and the observed are not closely coupled, they can belong to different levels of abstraction.

The observer mode supports broadcast communication, and the observed will send broadcast change messages to each registered observer. This broadcast mode may cause the observer to be notified circularly and consume a lot of resources.

Two roles should be defined in the generalized observer model.

Subject: the class that generates the message is subject. In the example of the previous article, it is the tester class;

Observer: class interested in the message generated by subject. In the example above, it is the tax bureau and the Social Security Bureau;

If the relationship between men and women is used to compare subject and observer, the subject should be the girl whom the boy secretly loves. Boys are infatuated with girls, but girls are always close and secretive. So the boy said to the girl: I will wait for you. If you have a destination, please inform me, otherwise I will wait all the time. Here, the boy is the girl’s observer. He is interested in the news generated by the girl – whether there is a destination. So when the message of destination is generated, the girl will inform the boy that a love cup is born, but the complete observer mode will be perfectly realized.

As subjects, the following interfaces should be implemented:

  • Add observer
  • Remove observer
  • Notify observer

As an observer, it needs to realize the specific performance when receiving notifications.

Example:
Ruby comes with an observer class that implements the observer pattern. It can be used to implement observer mode.

Code example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: GB2312 -*-
 
require 'observer'
#Examples of using the observer pattern (Ruby)
 
#Observer p
class PObservable
include Observable
 
end
 
#Observer a
class AObserver
#The update method name is required
def update(arg)
  puts "Aobserver notified" + arg
end
end
 
#Observer B
class BObserver
#The update method name is required
def update(arg)
  puts "Bobserver was notified" + arg
end
end
 
#Observer initialization
observer_a = AObserver.new
observer_b = BObserver.new
 
#Observer initialization
obj = PObservable.new
 
#Add monitoring object
obj.add_observer(observer_a)
obj.add_observer(observer_b)
 
#The observed person has changed - > this code must have, otherwise the observer cannot be notified
obj.changed
 
#Notify the observer
obj.notify_observers("Test")
 

Output results:

?
1
2
Aobserver was notified to test
Bobserver was notified to test