Ruby observer pattern

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


Design pattern is much more interesting in OO, especially for such as I like is not used much, although sometimes also can be used, but do not know is how to model. To understand the design pattern for several days, something is in fact will often use are summarized, and that, learning another important design pattern is, we use the design pattern we will know their own use, and also know how the design patterns.

As for this design pattern and some things, is found and not present, in other words, we can often combined with a new model named to several models together, it is a complex model, but also can use another model named. (reprinted retained Phodal's Blog Phodal's zenthink)

Design pattern is simplified in our many object oriented design in the early stage of system design, this sometimes will have a certain role, but most of the time for me, will use his time, mostly in the reconstruction of the time, because it is not very familiar with.

The observer pattern

The observer pattern is also called the publish subscribe (Publish/Subscribe) model, the model - view (Model/View) mode, the source - the listener (Source/Listener) or dependent pattern (Dependents) model.

The observer pattern defines a one to many dependencies, so multiple observer object while listening to one of the topic. The subject object changes in state, will inform all JObserver object, enabling them to automatically update themselves.

A software system often requires change when in a state of the object, some other object to make corresponding changes. Do a lot of design scheme of this point, but in order to make the system can easily reuse, should choose a design scheme of low coupling. Multiplexing to reduce coupling between objects of the system, but also the designers need to make the low coupling between objects to maintain the coordinated action, ensures a high degree of cooperation (Collaboration). The observer pattern is one of the most important design to meet the requirements of various.


Simple, is that when we detected an element changes, other elements according to this change.



Ruby observer pattern

The Ruby provides a library called observer for the realization of the Observer mode, the observer library provides the Observer module.

The API is shown as follows

Method name

Function

add_observer(observer)

Add the observer

delete_observer(observer)

Delete a particular observer

delete_observer

Remove the observer

count_observer

The number of observers

change(state=true)

Set the update flag is true

changed?

Check the update flag

notify_observer(*arg)

Notice the update, if the update flag is true, calling the observation method with parameter ARG


A simple example of Ruby observer

Here have to do is get a JSON data, the data will be updated.


Access to JSON data, at the same time analysis.

[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. require 'net/http'
  2. require 'rubygems'
  3. require 'json'
  4. class GetData
  5. attr_reader:res,:parsed
  6. def initialize(uri)
  7. uri=URI(uri)
  8. @res=Net::HTTP.get(uri)
  9. @parsed=JSON.parse(res)
  10. end
  11. def id
  12. @parsed[0]["id"]
  13. end
  14. def sensors1
  15. @parsed[0]["sensors1"].round(2)
  16. end
  17. def sensors2
  18. @parsed[0]["sensors2"].round(2)
  19. end
  20. def temperature
  21. @parsed[0]["temperature"].round(2)
  22. end
  23. def led1
  24. @parsed[0]["led1"]
  25. end
  26. end



The following is the key, and the observer is related, is the observed data, obtained by the.

Through the changed, at the same time, by the method of notify_observer told the observer

[html] view plaincopyprint?To see the code in CODEA piece of code to me


  1. require 'rubygems'
  2. require 'thread'
  3. require 'observer'
  4. require 'getdata'
  5. require 'ledstatus'
  6. class Led
  7. include Observable
  8. attr_reader:data
  9. def initialize
  10. @uri='http://www.xianuniversity.com/athome/1'
  11. end
  12. def getdata
  13. loop do
  14. changed()
  15. data=GetData.new(@uri)
  16. changed
  17. notify_observers(data.id,data.sensors1,data.sensors2,data.temperature,data.led1)
  18. sleep 1
  19. end
  20. end
  21. end


Then let's create an observer

[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. class LedStatus
  2. def update(arg,sensors1,sensors2,temperature,led1)
  3. puts "id:#{arg},sensors1:#{sensors1},sensors2:#{sensors2},temperature:#{temperature},led1:#{led1}"
  4. end
  5. end


Test

[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. require 'spec_helper'
  2. describe LedStatus do
  3. let(:ledstatus){LedStatus.new()}
  4. describe "Observable" do
  5. it "Should have a result" do
  6. led=Led.new
  7. led.add_observer(ledstatus)
  8. led.getdata
  9. end
  10. end
  11. end


The test results are shown as follows.

[html] view plaincopyprint?To see the code in CODEA piece of code to me


  1. phodal@linux-dlkp:~/tw/observer> rake
  2. /usr/bin/ruby1.9 -S rspec ./spec/getdata_spec.rb ./spec/ledstatus_spec.rb
  3. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:0
  4. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:1
  5. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:0
  6. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:1
  7. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:1
  8. id:1,sensors1:22.0,sensors2:11.0,temperature:10.0,led1:1

[html] view plaincopyprint?To see the code in CODEA piece of code to me




Summary

[html] view plaincopyprint?To see the code in CODEA piece of code to me


  1. The advantages of using the Ruby built-in Observer library, we can simplify the mutual dependence. At the same time, the structure also can simplify the program, compared to write their own observer case.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Bradley at January 14, 2014 - 2:17 AM