Created
January 1, 2015 01:42
-
-
Save meaganewaller/bb9e6b45a97635f2c30d to your computer and use it in GitHub Desktop.
template method pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './report' | |
require './html_report' | |
require './plain_text_report' | |
report = HTMLReport.new | |
report.output_report | |
report = PlainTextReport.new | |
report.output_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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