SlideShare a Scribd company logo
1 of 27
Download to read offline
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

Similar to Design Patterns in Ruby explained with examples

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1Jano Suchal
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpAlena Holligan
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmersAlexander Varwijk
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v RubyJano Suchal
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxcarliotwaycave
 
DataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsDataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsJoshua Erney
 
Hands on Mahout!
Hands on Mahout!Hands on Mahout!
Hands on Mahout!OSCON Byrum
 
Software Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasSoftware Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasBen Gotow
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfSahajShrimal1
 
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...ICS User Group
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Raffi Khatchadourian
 

Similar to Design Patterns in Ruby explained with examples (20)

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphp
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v Ruby
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
 
DataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsDataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language Fundamentals
 
Bp301
Bp301Bp301
Bp301
 
Hands on Mahout!
Hands on Mahout!Hands on Mahout!
Hands on Mahout!
 
Why ruby
Why rubyWhy ruby
Why ruby
 
Software Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasSoftware Engineering Best Practices @ Nylas
Software Engineering Best Practices @ Nylas
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
 
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

Design Patterns in Ruby explained with examples

  • 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