CSC/ECE 517 Fall 2007/wiki1b 3 an: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 1: Line 1:
[[Observer Pattern]]
<b>Observer Pattern</b>


 
<table id="toc" class="toc" summary="Contents">
'''Observer Pattern'''
<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>


----
----

Revision as of 23:28, 10 October 2007

Observer Pattern

Contents

  • <a href="#Closures">1 Properties</a>
    • <a href="#What_are_closures?">1.1 What are Closures?</a>
    • <a href="#Closure_Example">1.2 Closure Example</a>
    • <a href="#Adv_of_closures">1.3 Advantages of Closures</a>
  • <a href="#Design_patterns">2 Design Patterns</a>
  • <a href="#Closures_and_design_patterns">3 Closures and Design Patterns</a>
  • <a href="#Observer_Pattern">4 Observer Pattern</a>
    • <a href="#Observer_description">4.1 Description</a>
    • <a href="#Observer_ClassDiagram">4.2 Class Diagram</a>
    • <a href="#Example">4.3 Example</a>
  • <a href="#Iterator_Pattern">5 Iterator Pattern</a>
    • <a href="#Iterator_description?">5.1 Description</a>
    • <a href="#Iterator_ClassDiagram">5.2 Class Diagram</a>
    • <a href="#Iterator_Example">5.3 Example</a>
  • <a href="#References">6 References</a>
  • <a href="#External_links">7 External links</a>

<a name="Observer_Pattern">Observer Pattern</a>

Observer Pattern


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

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. Programming Ruby:The Pragmatic Programmers Guide

2. Head First Design Patterns

3. Design Patterns - Elements Of Reusable Object Oriented Software


External References


1. Ruby Standard Library Documentation

2. Ruby Features

3. Closures in Ruby