设计模式中的观察者模式在Ruby编程中的运用实例解析

时间:2021-10-22 01:37:28

观察者模式(有时又被称为发布/订阅模式)是软件设计模式的一种。
在此种模式中,一个目标对象管理所有相依于它的观察者对象,并且在它本身的状态改变时主动发出通知。
这通常透过呼叫各观察者所提供的方法来实现。

实现观察者模式的时候要注意,观察者和被观察对象之间的互动关系不能
体现成类之间的直接调用,否则就将使观察者和被观察对象之间紧密的耦合起来,
从根本上违反面向对象的设计的原则。无论是观察者“观察”观察对象,
还是被观察者将自己的改变“通知”观察者,都不应该直接调用。

通俗点说就是A对象(被观察)通知另一个(一些)对象(观察者)自己发生改变了,改变了什么,至于你们这些对象要做什么就不关我的事了,你们自己做去吧!耦合度就此降低了。。。

下面的例子使用ruby的module实现了较为传统的观察者模式。使用module的好处是:subject类可能是其他基类的子类,mixin实现了类似多继承的效果。

?
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
45
46
47
module Subject
 def initialize
  @observers = []
 end
 
 def add_observer ob
  @observers << ob
 end
 
 def delete_observer ob
  @observers.delete ob
 end
 
 def notify_observers
  @observers.each do |ob|
   ob.update self
  end
 end
end
 
class Employee
 include Subject
 attr_reader :name, :title
 attr_reader :salary
 
 def initialize name, title, salary
  super()
  @name = name
  @title = title
  @salary = salary
 end
 
 def salary=new_salary
  @salary = new_salary
  notify_observers
 end
end
 
class Taxman
 def update obj
  puts "#{obj.name} now has a salary of #{obj.salary}"
 end
end
 
jack = Employee.new('jack', 'prgramer', 3000)
jack.add_observer(Taxman.new)
jack.salary = 3000

我们可以自己实现Subject module,不过这样做有些画蛇添足,因为ruby核心库本身就包含了Observable模块,我们只需要将其mixin代码既可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'observer'
 
class Employee
 include Observable
 
 attr_reader :name, :title, :salary
 
 def initialize name, title, salary
  @name = name
  @title = title
  @salary = salary
 end
 
 def salary=(new_salary)
  @salary = new_salary
  changed
  notify_observers(self)
 end # salary=
end # Employee

在notify_observers之前必须先调用changed方法,表示确实发生了改变,否则notify_observers方法是无效的。