Design patterns can help everyone, even the newbies.
And they aren't as difficult/theoretical/ as they may seem.
This presentation talks about how Design Patterns can help you write better Ruby code with some examples.
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
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
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