Skip to content

Instantly share code, notes, and snippets.

@kasei-san
Created July 24, 2014 23:11
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 kasei-san/9c8dc95f905de1df6962 to your computer and use it in GitHub Desktop.
Save kasei-san/9c8dc95f905de1df6962 to your computer and use it in GitHub Desktop.
class StringListerViewer
attr_reader :items, :type
def initialize(items, type)
@items = items
@type = type
end
def string_lister(items)
Object.const_get("#{type.to_s.capitalize}StringLister").new(items)
end
def display
puts string_lister(items).display
end
end
items = %w[abc def gih]
StringListerViewer2.new(items, :text).display
StringListerViewer2.new(items, :html).display
Factory Method パターンは、他のクラスのコンストラクタをサブクラスで上書き可能な自分のメソッドに置き換えることで、 アプリケーションに特化したオブジェクトの生成をサブクラスに追い出し、クラスの再利用性を高めることを目的とする。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment