|
|
Line 1: |
Line 1: |
| <b>Observer Pattern</b>
| | ''We have said that closures in Ruby can be used to implement the Command and Strategy patterns. Closures are useful in implementing various other design patterns too. Explain which patterns, and give examples, using pseudo-Ruby code (i.e., your code should illustrate how to solve the problem with closures in Ruby, but it doesn't actually have to run, if it would be too complicated to give a running example). '' |
|
| |
|
| <table id="toc" class="toc" summary="Contents">
| | __TOC__ |
| <tr>
| |
| <td>
| |
| <div id="toctitle">
| |
| <h2>Contents</h2>
| |
| </div>
| |
| <ul>
| |
| <li class='toclevel-1'><a href="#Closures"><span class="tocnumber">1</span> <span class="toctext">Properties</span></a></li>
| |
| <ul>
| |
| <li class='toclevel-2'><a href="#What_are_closures?"><span class="tocnumber">1.1</span> <span class="toctext">What are Closures?</span></a></li>
| |
| <li class='toclevel-2'><a href="#Closure_Example"><span class="tocnumber">1.2</span> <span class="toctext">Closure Example</span></a></li>
| |
| <li class='toclevel-2'><a href="#Adv_of_closures"><span class="tocnumber">1.3</span> <span class="toctext">Advantages of Closures</span></a></li>
| |
| </ul>
| |
| </li>
| |
| <li class='toclevel-1'><a href="#Design_patterns"><span class="tocnumber">2</span> <span class="toctext">Design Patterns</span></a></li>
| |
| <li class='toclevel-1'><a href="#Closures_and_design_patterns"><span class="tocnumber">3</span> <span class="toctext">Closures and Design Patterns</span></a></li>
| |
| <li class='toclevel-1'><a href="#Observer_Pattern"><span class="tocnumber">4</span> <span class="toctext">Observer Pattern</span></a></li>
| |
| <ul>
| |
| <li class='toclevel-2'><a href="#Observer_description"><span class="tocnumber">4.1</span> <span class="toctext">Description</span></a></li>
| |
| <li class='toclevel-2'><a href="#Observer_ClassDiagram"><span class="tocnumber">4.2</span> <span class="toctext">Class Diagram</span></a></li>
| |
| <li class='toclevel-2'><a href="#Example"><span class="tocnumber">4.3</span> <span class="toctext">Example</span></a></li>
| |
| </ul>
| |
| <li class='toclevel-1'><a href="#Iterator_Pattern"><span class="tocnumber">5</span> <span class="toctext">Iterator Pattern</span></a></li>
| |
| <ul>
| |
| <li class='toclevel-2'><a href="#Iterator_description?"><span class="tocnumber">5.1</span> <span class="toctext">Description</span></a></li>
| |
| <li class='toclevel-2'><a href="#Iterator_ClassDiagram"><span class="tocnumber">5.2</span> <span class="toctext">Class Diagram</span></a></li>
| |
| <li class='toclevel-2'><a href="#Iterator_Example"><span class="tocnumber">5.3</span> <span class="toctext">Example</span></a></li>
| |
| </ul>
| |
| <li class='toclevel-1'><a href="#References"><span class="tocnumber">6</span> <span class="toctext">References</span></a></li>
| |
| <li class='toclevel-1'><a href="#External_links"><span class="tocnumber">7</span> <span class="toctext">External links</span></a></li>
| |
| </ul>
| |
| </td>
| |
| </tr>
| |
| </table>
| |
| <a name="Observer_Pattern">Observer Pattern</a><h2>Observer Pattern</h2>
| |
| | |
| ----
| |
| | |
| An observer pattern defines a one to many dependency between objects so tht when one object changes state all its dependents are notified and updated accordingly.
| |
| | |
| In its simplified form, the observer design pattern consists of a subject,and one or more observers,which observe an event in the subject.An event can be a change in the state of the subject and can be defined accordingly.
| |
| | |
| The subject class maintains a list of observers and contains methods to add and delete observers from the list .It also contains a notify method which is used to notify the observers whenever an event occurs in.
| |
| The observer class on the other hand contains methods to handle such notfications.
| |
| | |
| Closures can be used to implement Observer pattern.The role of closures can be the basis of the following two applications of Observer design patterns
| |
| | |
| 1.A change in one object requires changing other objects.Also the number of objects to be changed is unknown.
| |
| | |
| 2.The object being changed should be able to notify other objects about the change without having any idea about the current state of the objects.
| |
| | |
| Following is a pseudocode for an application in which we intend to calculate the area and circumference of a circle for varying values of its radius.
| |
| | |
| | |
| class Subject
| |
| | |
| def initialize
| |
| | |
| @list=[]
| |
| | |
| end
| |
| | |
| def attach(&list)
| |
| | |
| @list << list
| |
| | |
| end
| |
| | |
| def notify
| |
| | |
| @list.each{|x| x.call(@radius)}
| |
| | |
| end
| |
| | |
| def update(r)
| |
| | |
| @radius=r
| |
| | |
| notify
| |
| | |
| end
| |
| | |
| end
| |
| | |
| | |
| class Observer1
| |
| | |
| def calc_area(radius)
| |
| | |
| @area=radius*radius*3.14
| |
| | |
| puts "Area #{@area}"
| |
| | |
| end
| |
| | |
| end
| |
| | |
| | |
| class Observer2
| |
| | |
| def calc_circumference(radius)
| |
| | |
| @circum=2*3.14*radius
| |
| | |
| puts "Circumference #{@circum}"
| |
| | |
| end
| |
| | |
| end
| |
| | |
| | |
| o1=Observer1.new
| |
| | |
| o2=Observer2.new
| |
| | |
| s=Subject.new
| |
| | |
| s.attach { |s| o1.calc_area(s)}
| |
| | |
| s.attach { |s| o2.calc_circumference(s)}
| |
| | |
| s.update(5)
| |
| | |
| | |
| | |
| The output is
| |
| | | |
| Area 78.5
| | ==Closures== |
| | |
| Circumference 31.4
| |
| | |
| | |
| Here we have a subject class that contains the following methods.
| |
| | |
| 1.Attach()
| |
| | |
| This method is used to update the list of observers
| |
| | |
| 2.Update()
| |
| | |
| This method updates the radius and calls the notify method.
| |
| | |
| 3.Notify()
| |
| | |
| This method notifies all the observers in the list about the event which is the change in the value of the radius.
| |
| | |
| We have two observers for calculating area and circumference.
| |
| On receiving notify,these functions return updated values of area and circumference depending on the new value of the radius.
| |
| | |
| | |
| | |
| | |
| '''ITERATOR PATTERN'''
| |
| | |
| ----
| |
| | |
| | |
| The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.An aggregate function can be an array or a hash.
| |
| | |
| Following is a simple example which illustrates the iterator pattern using the power of Ruby closures.
| |
| | |
| def find_birthdays_in_range(min,max)
| |
| birthdays = [ "1970-06-15", "1975-08-13", "1939-03-21", "2001-12-01" ]
| |
| birthdays.find_all { |birthday| birthday > min && birthday < max }
| |
| end
| |
| | |
| Here there are 3 functions here that are called from within each other.
| |
| | |
| A : find_birthdays_in_range
| |
| | |
| B : find_all
| |
| | |
| C : anonymous block
| |
| | |
| The third one is the closure ,it accepts one parameter called birthday which is defined in the first function.It iterates over the array(birthdays) and returns only those values in the given range,specified by min and max.
| |
| | |
| '''References'''
| |
| | |
| ----
| |
|
| |
|
| 1. [http://www.rubycentral.com/pickaxe/ Programming Ruby:The Pragmatic Programmers Guide]
| | === What are Closures?=== |
|
| |
|
| 2. [http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]
| | === A Simple Closure Example === |
|
| |
|
| 3. [http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612 Design Patterns - Elements Of Reusable Object Oriented Software]
| | === Advantages of Closures === |
|
| |
|
| | == Design Pattern == |
|
| |
|
| '''External References'''
| | == Closures And Design Patterns == |
|
| |
|
| ----
| | == Observer Pattern == |
|
| |
|
| 1. [http://www.ruby-doc.org/core/ Ruby Standard Library Documentation]
| | === Description === |
| | === Typical Uses=== |
| | === Examples === |
| | ===Terminology=== |
| | ===Structure === |
| | ==Closures and Observer Pattern== |
| | ===Pseudocode=== |
|
| |
|
| 2. [http://www.zorched.net/2006/02/19/ruby-features-youll-wish-you-had-in-other-languages/ Ruby Features]
| | == Iterator Pattern == |
|
| |
|
| 3. [http://www.rubygarden.org/Ruby/page/show/ClosuresAndBlocks Closures in Ruby]
| | === Description === |
| | === Typical Uses=== |
| | === Examples === |
| | ===Terminology=== |
| | ===Structure === |
| | ==Closures and Iterator Pattern== |
| | ===Pseudocode=== |
| | ==References== |
| | ==External Links== |