Help us understand the problem. What is going on with this article?

デザインパターン Template Method

More than 3 years have passed since last update.

概要

親クラスに抽象的な処理のみを作成し、サブクラスで具体的な実装をすることで、処理の追加・変更に強くする。

ポイント:

1. 変更が起きるものと起きないものでコードを分ける
変更がない処理 → 親クラス
変更がある   → サブクラス

Tempalate Methodを使う前と後

Template Method使う前:

template_method_before.rb
#!/usr/bin/ruby

# About Template Method

class Report
  def initialize
        @title = "月次レポート"
        @text = ["順調","OK"]
  end

  def output_reort
    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

report = Report.new
report.output_reort

Template Method使う後:

#!/usr/bin/ruby

# About Template Method

class Report
  def initialize
      @title = "月次レポート"
      @text = ["順調","OK"]
  end

  def output_report
      output_start
      output_head
      output_body_start
      output_body
      output_body_end
      output_end
  end

  def output_start
    raise "Called abst method. output_start"
  end

  def output_head
    raise "Called abst method. output_head"
  end

  def output_body_start
    raise "Called abst method. output_body_start"
  end

  def output_body
    raise "Called abst method. output_body"
  end

  def output_body_end
    raise "Called abst method. output_body_start"
  end

  def output_end
    raise "Called abst method. output_end"
  end

end


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_body
    @text.each do |line|
      puts "<p>#{line}</p>"
    end
  end

  def output_body_end
    puts "</body>"
  end

  def output_end
    puts "</html>"
  end

end

親クラスには抽象的な処理のみ、サブクラスには具体的な処理を記載することで追加が容易になる。
たとえば、HTML出力だけではなく、テキスト出力もする必要があれば
以下のように書ける

class TextReport < Report
  def output_start
    puts "##### This is text report file #####"
  end

  def output_head
    puts "#{@title}"
  end

  def output_body_start
  end

  def output_body
  end

  def output_body_end
  end

  def output_end
  end
end

を加えるだけでよい。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした