Skip to content

Instantly share code, notes, and snippets.

@meaganewaller
Created January 1, 2015 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meaganewaller/bb9e6b45a97635f2c30d to your computer and use it in GitHub Desktop.
Save meaganewaller/bb9e6b45a97635f2c30d to your computer and use it in GitHub Desktop.
template method pattern
#html_report
class HTMLReport < Report
def output_start
puts '<html>'
end
def output_head
puts ' <head>'
puts " <title>#{@title}</title>"
puts ' </head>'
end
def output_body_start
puts '<body>'
end
def output_line(line)
puts " <p>#{line}</p>"
end
def output_body_end
puts '</body>'
end
def output_end
puts '</html>'
end
end
# plain_text_report
class PlainTextReport < Report
def output_start
end
def output_head
puts "**** #{@title} ****"
puts
end
def output_body_start
end
def output_line(line)
puts line
end
def output_body_end
end
def output_end
end
end
# report
class Report
def initialize
@title = 'Monthly Report'
@text = ['Things are going', 'really, really well.']
end
def output_report
puts('<html>')
puts(' <head>')
puts(" <title>#{@title}</title>")
puts(' </head>')
puts(' <body>')
@text.each do |line|
puts(" <p>#{line}</p>" )
end
puts(' </body>')
puts('</html>')
end
end
require './report'
require './html_report'
require './plain_text_report'
report = HTMLReport.new
report.output_report
report = PlainTextReport.new
report.output_report
<html>
<head>
<title>Monthly Report</title>
</head>
<body>
<p>Things are going</p>
<p>really, really well.</p>
</body>
</html>
<html>
<head>
<title>Monthly Report</title>
</head>
<body>
<p>Things are going</p>
<p>really, really well.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment