Strategy::V1::HTMLFormatter
<html>
<head>
<title>Monthly Report</title>
</head>
<body>
<p>Things are going</p>
<p>really, really well.</p>
</body>
</html>
Strategy::V1::PlainFormatter
**** Monthly Report ****
Things are going
really, really well.
=============================================================================
Strategy::V2::HTMLFormatter
<html>
<head>
<title>Monthly Report</title>
</head>
<body>
<p>Things are going</p>
<p>really, really well.</p>
</body>
</html>
Strategy::V2::PlainFormatter
**** Monthly Report ****
Things are going
really, really well.
Created
September 6, 2020 20:50
-
-
Save joalbertg/466f6d18843d72687cd557193d2ecf62 to your computer and use it in GitHub Desktop.
ruby: Strategy Pattern
This file contains hidden or 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
module Strategy | |
module V1 | |
class Formatter | |
def output_report(title, text) | |
raise 'Abstract method called' | |
end | |
end | |
class HTMLFormater < Formatter | |
def output_report(title, text) | |
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 | |
class PlainTextFormatter < Formatter | |
def output_report(title, text) | |
puts("**** #{title} ****") | |
text.each do |line| | |
puts(line) | |
end | |
end | |
end | |
class Report | |
attr_reader :title, :text | |
attr_reader :formatter | |
def initialize(title, text, formatter) | |
@title = title | |
@text = text | |
@formatter = formatter | |
end | |
def output_report | |
formatter.output_report(title, text) | |
end | |
end | |
end | |
module V2 | |
class Formatter | |
def output_report(context) | |
raise 'Abstract method called' | |
end | |
end | |
class HTMLFormater < Formatter | |
def output_report(context) | |
puts('<html>') | |
puts(' <head>') | |
puts(" <title>#{context.title}</title>") | |
puts(' </head>') | |
puts(' <body>') | |
context.text.each do |line| | |
puts(" <p>#{line}</p>") | |
end | |
puts(' </body>') | |
puts('</html>') | |
end | |
end | |
class PlainTextFormatter < Formatter | |
def output_report(context) | |
puts("**** #{context.title} ****") | |
context.text.each do |line| | |
puts(line) | |
end | |
end | |
end | |
class Report < V1::Report | |
def output_report | |
formatter.output_report(self) | |
end | |
end | |
end | |
end | |
title = 'Monthly Report' | |
text = ['Things are going', 'really, really well.'] | |
#Strategy::V1::Formatter.new.output_report('title', 'text') | |
#Strategy::V1::HTMLFormater.new.output_report(title, text) | |
#Strategy::V1::PlainTextFormatter.new.output_report(title, text) | |
puts "\nStrategy::V1::HTMLFormatter\n " | |
report = Strategy::V1::Report.new(title, text, Strategy::V1::HTMLFormater.new) | |
report.output_report | |
puts "\nStrategy::V1::PlainFormatter\n " | |
report = Strategy::V1::Report.new(title, text, Strategy::V1::PlainTextFormatter.new) | |
report.output_report | |
puts "\n=============================================================================" | |
puts "\nStrategy::V2::HTMLFormatter\n " | |
report = Strategy::V2::Report.new(title, text, Strategy::V2::HTMLFormater.new) | |
report.output_report | |
puts "\nStrategy::V2::PlainFormatter\n " | |
report = Strategy::V2::Report.new(title, text, Strategy::V2::PlainTextFormatter.new) | |
report.output_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment