SlideShare a Scribd company logo
Design Patterns in Ruby
Observer Pattern
Copyright 2016. Jyaasa Technologies. All Right Reserved
http://jyaasa.com
Neha Suwal
Associate Software Engineer at
Jyaasa Technologies
Design Patterns
Design principles are core abstract principles which we are supposed to follow while
designing software architect
Observer Pattern:
● one object to inform other ‘interested’ objects when its state changes
The Observable module in the Ruby standard library provides the mechanism necessary
for us to implement this pattern, and it is fairly easy to use.
Example:
module Email
extend self
def send(subject, receiver)
mail to: “#{receiver}@example.com”, subject:
“#{subject}”
end
end
class Person
include Email
attr_reader :name
def initialize(name)
@name = name
end
def send_email(subject, receiver)
Email.send(subject, receiver)
end
end
bill = Person.new 'Bill'
bill.send_email 'Fishing Trip', 'Fred'
class Alert
def gotcha(person)
puts "!!! ALERT: #{person} SENT AN EMAIL !!!"
end
end
module Subject
attr_reader :observers
def initialize
@observers = []
end
def add_observer(*observers)
observers.each { |observer| @observers << observer }
end
private
def notify_observers
observers.each { |observer| observer.gotcha(self) }
end
end
class Person
include Email, Subject
attr_reader :name
def initialize(name)
# 'super' requires a parentheses because we're calling
# super on the superclass, 'Subject'
super()
@name = name
end
def send_email(subject, receiver)
Email.send(subject, receiver)
notify_observers
end
end
alert = Alert.new
bill = Person.new 'Bill'
bill.add_observer alert
bill.send_email 'Fishing Trip', 'Fred'
!!! ALERT: BILL SENT AN EMAIL !!!
THANK YOU!

More Related Content

PPTX
Using velocity Templates(An overview)
DOCX
Ruby Interview Questions
PPTX
3.1 file input and output
PDF
Build a game with javascript (april 2017)
PDF
Best Practice In Nutshell
PPTX
Js tips & tricks
PDF
Intro to javascript (5:2)
PPTX
Design Pattern - Observer Pattern
Using velocity Templates(An overview)
Ruby Interview Questions
3.1 file input and output
Build a game with javascript (april 2017)
Best Practice In Nutshell
Js tips & tricks
Intro to javascript (5:2)
Design Pattern - Observer Pattern

Viewers also liked (20)

PPT
Observer Pattern Khali Young 2006 Aug
PPTX
Observer pattern
PPT
Design patterns 4 - observer pattern
PPT
Design patterns - Observer Pattern
PPSX
Observer design pattern
PDF
Observer Pattern
PPTX
Observer pattern
PPT
Observer and Decorator Pattern
PPTX
Observer Pattern
PDF
Js design patterns
PPT
Observer pattern
PPTX
Design patterns
PPTX
Observer pattern, delegate, event, lambda expression
PDF
The Observer Pattern (Definition using UML)
ODP
Design Pattern - 2. Observer
PDF
Observer Pattern
PPTX
Observer & singleton pattern
PDF
Observer pattern
PDF
Design Patterns
PDF
Design Patterns in Cocoa Touch
Observer Pattern Khali Young 2006 Aug
Observer pattern
Design patterns 4 - observer pattern
Design patterns - Observer Pattern
Observer design pattern
Observer Pattern
Observer pattern
Observer and Decorator Pattern
Observer Pattern
Js design patterns
Observer pattern
Design patterns
Observer pattern, delegate, event, lambda expression
The Observer Pattern (Definition using UML)
Design Pattern - 2. Observer
Observer Pattern
Observer & singleton pattern
Observer pattern
Design Patterns
Design Patterns in Cocoa Touch
Ad

Similar to Design patterns: observer pattern (20)

PPTX
Design Patterns in Ruby
PPTX
Software Design Trilogy Part II - Design Patterns for Rubyists
PPTX
Observer Software Design Pattern
PPTX
Observer
PDF
JavaScript Observer Pattern Presentation (1).pdf
PDF
Tarefapatterns
PPTX
Observer Design Pattern to Observe Something
PPTX
Observer pattern
PPTX
Design patterns
PDF
Dpir 2014 abstractions
PPT
JAVA design patterns and Basic OOp concepts
ODP
Design Patterns
PPTX
Observer dp
ODP
Design Patterns in Ruby
PPT
Design Patterns.ppt
PPTX
6 Object Oriented Programming
PPTX
Object-oriented in ruby
PPT
Agile development with Ruby
PPT
07 bestpractice
PPTX
Design pattern-presentation
Design Patterns in Ruby
Software Design Trilogy Part II - Design Patterns for Rubyists
Observer Software Design Pattern
Observer
JavaScript Observer Pattern Presentation (1).pdf
Tarefapatterns
Observer Design Pattern to Observe Something
Observer pattern
Design patterns
Dpir 2014 abstractions
JAVA design patterns and Basic OOp concepts
Design Patterns
Observer dp
Design Patterns in Ruby
Design Patterns.ppt
6 Object Oriented Programming
Object-oriented in ruby
Agile development with Ruby
07 bestpractice
Design pattern-presentation
Ad

More from Jyaasa Technologies (20)

PPTX
Incident management with jira
PPTX
Extreme programming practices ( xp )
PPTX
The myth of 'real javascript developer'
PPTX
Microservices
PPTX
Facade pattern in rails
PPTX
Scrum ceromonies
PPTX
An introduction to bitcoin
PPTX
Tor network
PPTX
Collective ownership in agile teams
PPTX
Push notification
PPTX
The Design Thinking Process
PPTX
PPTX
Design sprint
PPTX
Data Flow Diagram
PPTX
OKRs and Actions Overview
PPTX
Active record in rails 5
PPTX
Design Patern::Adaptor pattern
PDF
Association in rails
PDF
Web design layout pattern
Incident management with jira
Extreme programming practices ( xp )
The myth of 'real javascript developer'
Microservices
Facade pattern in rails
Scrum ceromonies
An introduction to bitcoin
Tor network
Collective ownership in agile teams
Push notification
The Design Thinking Process
Design sprint
Data Flow Diagram
OKRs and Actions Overview
Active record in rails 5
Design Patern::Adaptor pattern
Association in rails
Web design layout pattern

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPT
Introduction Database Management System for Course Database
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administraation Chapter 3
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
wealthsignaloriginal-com-DS-text-... (1).pdf
ai tools demonstartion for schools and inter college
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Computer Software and OS of computer science of grade 11.pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction Database Management System for Course Database
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms II-SECS-1021-03
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Odoo Companies in India – Driving Business Transformation.pdf
Designing Intelligence for the Shop Floor.pdf
Introduction to Artificial Intelligence
System and Network Administraation Chapter 3
Which alternative to Crystal Reports is best for small or large businesses.pdf

Design patterns: observer pattern

  • 1. Design Patterns in Ruby Observer Pattern Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com
  • 2. Neha Suwal Associate Software Engineer at Jyaasa Technologies
  • 3. Design Patterns Design principles are core abstract principles which we are supposed to follow while designing software architect
  • 4. Observer Pattern: ● one object to inform other ‘interested’ objects when its state changes The Observable module in the Ruby standard library provides the mechanism necessary for us to implement this pattern, and it is fairly easy to use.
  • 5. Example: module Email extend self def send(subject, receiver) mail to: “#{receiver}@example.com”, subject: “#{subject}” end end class Person include Email attr_reader :name def initialize(name) @name = name end def send_email(subject, receiver) Email.send(subject, receiver) end end
  • 6. bill = Person.new 'Bill' bill.send_email 'Fishing Trip', 'Fred'
  • 7. class Alert def gotcha(person) puts "!!! ALERT: #{person} SENT AN EMAIL !!!" end end
  • 8. module Subject attr_reader :observers def initialize @observers = [] end def add_observer(*observers) observers.each { |observer| @observers << observer } end private def notify_observers observers.each { |observer| observer.gotcha(self) } end end
  • 9. class Person include Email, Subject attr_reader :name def initialize(name) # 'super' requires a parentheses because we're calling # super on the superclass, 'Subject' super() @name = name end def send_email(subject, receiver) Email.send(subject, receiver) notify_observers end end
  • 10. alert = Alert.new bill = Person.new 'Bill' bill.add_observer alert bill.send_email 'Fishing Trip', 'Fred' !!! ALERT: BILL SENT AN EMAIL !!!