Upload
shifa-khan
View
86
Download
2
Tags:
Embed Size (px)
Citation preview
"Mercator projection SW" by Strebe - Own work. Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons
9000 miles
Design PatternsTried and tested solutions that a programmer can use to solve common problems encountered when designing an application or system.
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
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
Singleton
TemplateComposite
Observer
Strategy
Iterator
CommandAdapter
Proxy
Decorator
Factory
Builder
Strategy
-> Implemented when a part of the algorithmvaries, but the context remains same.
-> Step 3 of a 5-step process variesdepending on runtime values, everythingelse 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") endend
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") endend
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") endend
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) endend
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") endend
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") endend
student_report = StudentReport.new(TextStrategy.new)
student_report = StudentReport.new(HTMLStrategy.new)
student_report.printThe final implementation of the print method is independent of the strategy implementation
OR