The observer pattern in the Design Pattern uses the example parsing in Ruby programming, and the observer ruby
The observer mode (sometimes referred to as the publish/subscribe mode) is a software design mode.
In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes.
This is usually done by calling the methods provided by various observers.
When implementing the observer mode, note that the interaction between the observer and the observed object cannot
Directly calling between classes. Otherwise, Close coupling between the observer and the observed object will be made,
This fundamentally violates the object-oriented design principles. Whether it is an observer's "observed" object,
Or the observer should not directly call the change "notification" Observer.
In layman's terms, object A (observed) notifies another (some) object (observer) of changes and changes, as for what you have to do, it's none of my work. You can do it yourself! The coupling degree is reduced...
The following example uses the ruby module to implement the traditional observer mode. The advantage of using the module is that the subject class may be a subclass of other base classes, and mixin achieves similar multi-inheritance effects.
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 endendclass 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 endendclass Taxman def update obj puts "#{obj.name} now has a salary of #{obj.salary}" endendjack = Employee.new('jack', 'prgramer', 3000)jack.add_observer(Taxman.new)jack.salary = 3000
We can implement the Subject module by ourselves. However, this is a little superfluous, because the ruby core library itself contains the Observable module. We only need to configure its mixin code.
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
Before policy_observers, you must call the changed method to indicate that the change has indeed occurred. Otherwise, the policy_observers method is invalid.
Articles you may be interested in:
- Instance parsing Implementation of observer mode in Ruby design mode development
- 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