SlideShare a Scribd company logo
Hi !
pronounced as-
SHI-FU SHI-FA
( master shifu from
Kung fu Panda )
( newbie programmer )
=
ZEAL
"Mercator projection SW" by Strebe - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia
9000 miles
Design Patterns in
Ruby
- shifa khan
Design Patterns in
Ruby
Design Patterns
Tried and tested solutions that a
programmer can use to solve common
problems encountered when designing an
application or system.
Why ?
Yay! I’ve finally created
a circular friction reduction
device that is intended to
rotate on a central axial
bearing to transfer load
over distances
. .
Er.. You mean a
Wheel?
Knowledge of design patterns helps you
recognize frequent patterns in problems and
reuse proven solutions to solve them.
better code , less effort , less time
Smart Programmer
Smart
Programmers
Smart
Team
Discussions are like :
“Hey, lets use an observer for this
function”
Instead of:
“Lets add a function in class A to
alert class B if property x of an
object of class A exceeds the
threshold value . . .”
Knowledge of design patterns also helps you
form a vocabulary for communicating design
decisions among the team during development.
Especially during pair-programming, and other Agile
processes, where design is a shared activity.
talk less, communicate better, quick
decisions, faster development
How ?
Singleton
Template
Composite
Observer
Strategy
Iterator
Command
Adapter
Proxy
Decorator
Factory
Builder
Singleton
Observer
require ‘singleton’
require ‘observer’
Strategy
Strategy
-> Implemented when a part of the algorithm
varies, but the context remains same.
-> Step 3 of a 5-step process varies
depending on runtime values, everything
else remains same.
General Idea
class StudentReport
def initialize
@grades = Hash.new
@name
end
def add_name(name)
@name = name
end
def add_grade(subject, grade)
@grades[subject] = grade
end
def print
output = ["Name, #{@name}"]
output << ["Subject,Grade"]
grades.keys.each do |subject|
output << "#{subject},#{@grades[subject]}"
end
output.join("n")
end
end
class HtmlStudentReport
def initialize
@grades = Hash.new
@name
end
def add_name(name)
@name = name
end
def add_grade(subject, grade)
@grades[subject] = grade
end
def print
output = ["<p>Name: #{@name}</p>"]
output << "<table><th><td>Subject</td><td>Grade</td></th>"
grades.keys.each do |subject|
output << "<tr><td>#{subject}</td>"
output << "<td>#{@grades[subject]}</td></tr>"
end
output << "</table>"
output.join("n")
end
end
class HtmlStudentReport
def initialize
@grades = Hash.new
@name
end
def add_name(name)
@name = name
end
def add_grade(subject, grade)
@grades[subject] = grade
end
def print
output = ["<p>Name: #{@name}</p>"]
output << "<table><th><td>Subject</td><td>Grade</td></th>"
grades.keys.each do |subject|
output << "<tr><td>#{subject}</td>"
output << "<td>#{@grades[subject]}</td></tr>"
end
output << "</table>"
output.join("n")
end
end
All steps are same
except for print method
Extract the print step
from the algorithm
class StudentReport
def initialize(print_strategy)
@print_strategy = print_strategy.new
@grades = Hash.new
@name
end
def add_name(name)
@name = name
end
def add_grade(subject, grade)
@grades[subject] = grade
end
def print
puts @print_strategy.print(@name, @grades)
end
end
Define it within
a strategy
Rest of the context
remains unchanged
class HTMLStrategy
def print(name, grades)
output = ["<p>Name: #{name}</p>"]
output << "<table><th><td>Subject</td><td>Grade</td></th>"
grades.keys.each do |subject|
output << "<tr>"
output << "<td>#{subject}</td>"
output << "<td>#{grades[subject]}</td>"
output << "</tr>"
end
output << "</table>"
output.join("n")
end
end
Define print within
a strategy
class TextStrategy
def print(name, grades)
output = ["Name, #{@name}"]
output << ["Subject,Grade"]
grades.keys.each do |subject|
output << "#{subject},#{grades[subject]}"
end
output.join("n")
end
end
student_report = StudentReport.new(TextStrategy.new)
student_report = StudentReport.new(HTMLStrategy.new)
student_report.print
The final implementation of the
print method is independent of
the strategy implementation
OR
Design patterns
in Ruby
-by Russ Olsen
Thank You !

More Related Content

PDF
Cocoa Heads Tricity - Design Patterns
PDF
Isomorphic js - React in Rails
PPTX
React + Redux Introduction
PPTX
Docker Security Overview
PDF
React JS and why it's awesome
PDF
React redux workshop
ODP
Design Patterns in Ruby
PDF
Simplifying Code: Monster to Elegant in 5 Steps
Cocoa Heads Tricity - Design Patterns
Isomorphic js - React in Rails
React + Redux Introduction
Docker Security Overview
React JS and why it's awesome
React redux workshop
Design Patterns in Ruby
Simplifying Code: Monster to Elegant in 5 Steps

Similar to Design patterns - Using Ruby (20)

PDF
Refactoring Workshop (Rails Pacific 2014)
PDF
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
PDF
Introduction to Ruby
KEY
Intro to Ruby - Twin Cities Code Camp 7
PDF
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
PDF
Techtalk design patterns
PPT
Agile development with Ruby
PDF
Design Patterns in Ruby
PDF
A Gentle Introduction to Functional Paradigms in Ruby
PDF
Design Patterns
KEY
Desarrollando aplicaciones web en minutos
PDF
Let’s Talk About Ruby
PDF
Dpir 2014 abstractions
PPTX
ap assignmnet presentation.pptx
PDF
60780174 49594067-cs1403-case-tools-lab-manual
PDF
Introduction to Ruby Programming Language
ODP
Ruby Basics by Rafiq
PPTX
Patterns of Design, as Presented by Ritchey
PDF
Ruby — An introduction
Refactoring Workshop (Rails Pacific 2014)
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
Introduction to Ruby
Intro to Ruby - Twin Cities Code Camp 7
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Techtalk design patterns
Agile development with Ruby
Design Patterns in Ruby
A Gentle Introduction to Functional Paradigms in Ruby
Design Patterns
Desarrollando aplicaciones web en minutos
Let’s Talk About Ruby
Dpir 2014 abstractions
ap assignmnet presentation.pptx
60780174 49594067-cs1403-case-tools-lab-manual
Introduction to Ruby Programming Language
Ruby Basics by Rafiq
Patterns of Design, as Presented by Ritchey
Ruby — An introduction
Ad

Recently uploaded (20)

PPTX
sap open course for s4hana steps from ECC to s4
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
cuic standard and advanced reporting.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
sap open course for s4hana steps from ECC to s4
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
cuic standard and advanced reporting.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine Learning_overview_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Ad

Design patterns - Using Ruby

  • 2. pronounced as- SHI-FU SHI-FA ( master shifu from Kung fu Panda ) ( newbie programmer )
  • 4. "Mercator projection SW" by Strebe - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia 9000 miles
  • 7. Design Patterns Tried and tested solutions that a programmer can use to solve common problems encountered when designing an application or system.
  • 9. Yay! I’ve finally created a circular friction reduction device that is intended to rotate on a central axial bearing to transfer load over distances
  • 10. . . Er.. You mean a Wheel?
  • 11. Knowledge of design patterns helps you recognize frequent patterns in problems and reuse proven solutions to solve them. better code , less effort , less time Smart Programmer
  • 12. Smart Programmers Smart Team Discussions are like : “Hey, lets use an observer for this function” Instead of: “Lets add a function in class A to alert class B if property x of an object of class A exceeds the threshold value . . .”
  • 13. Knowledge of design patterns also helps you form a vocabulary for communicating design decisions among the team during development. Especially during pair-programming, and other Agile processes, where design is a shared activity. talk less, communicate better, quick decisions, faster development
  • 14. How ?
  • 18. Strategy -> Implemented when a part of the algorithm varies, but the context remains same. -> Step 3 of a 5-step process varies depending on runtime values, everything else remains same. General Idea
  • 19. class StudentReport def initialize @grades = Hash.new @name end def add_name(name) @name = name end def add_grade(subject, grade) @grades[subject] = grade end def print output = ["Name, #{@name}"] output << ["Subject,Grade"] grades.keys.each do |subject| output << "#{subject},#{@grades[subject]}" end output.join("n") end end
  • 20. class HtmlStudentReport def initialize @grades = Hash.new @name end def add_name(name) @name = name end def add_grade(subject, grade) @grades[subject] = grade end def print output = ["<p>Name: #{@name}</p>"] output << "<table><th><td>Subject</td><td>Grade</td></th>" grades.keys.each do |subject| output << "<tr><td>#{subject}</td>" output << "<td>#{@grades[subject]}</td></tr>" end output << "</table>" output.join("n") end end
  • 21. class HtmlStudentReport def initialize @grades = Hash.new @name end def add_name(name) @name = name end def add_grade(subject, grade) @grades[subject] = grade end def print output = ["<p>Name: #{@name}</p>"] output << "<table><th><td>Subject</td><td>Grade</td></th>" grades.keys.each do |subject| output << "<tr><td>#{subject}</td>" output << "<td>#{@grades[subject]}</td></tr>" end output << "</table>" output.join("n") end end All steps are same except for print method
  • 22. Extract the print step from the algorithm class StudentReport def initialize(print_strategy) @print_strategy = print_strategy.new @grades = Hash.new @name end def add_name(name) @name = name end def add_grade(subject, grade) @grades[subject] = grade end def print puts @print_strategy.print(@name, @grades) end end Define it within a strategy Rest of the context remains unchanged
  • 23. class HTMLStrategy def print(name, grades) output = ["<p>Name: #{name}</p>"] output << "<table><th><td>Subject</td><td>Grade</td></th>" grades.keys.each do |subject| output << "<tr>" output << "<td>#{subject}</td>" output << "<td>#{grades[subject]}</td>" output << "</tr>" end output << "</table>" output.join("n") end end Define print within a strategy
  • 24. class TextStrategy def print(name, grades) output = ["Name, #{@name}"] output << ["Subject,Grade"] grades.keys.each do |subject| output << "#{subject},#{grades[subject]}" end output.join("n") end end
  • 25. student_report = StudentReport.new(TextStrategy.new) student_report = StudentReport.new(HTMLStrategy.new) student_report.print The final implementation of the print method is independent of the strategy implementation OR