Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Created September 6, 2020 20:50
Show Gist options
  • Save joalbertg/466f6d18843d72687cd557193d2ecf62 to your computer and use it in GitHub Desktop.
Save joalbertg/466f6d18843d72687cd557193d2ecf62 to your computer and use it in GitHub Desktop.
ruby: Strategy Pattern

Strategy Pattern

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.
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