Created
July 24, 2014 23:11
-
-
Save kasei-san/9c8dc95f905de1df6962 to your computer and use it in GitHub Desktop.
Factory Method パターン ref: http://qiita.com/kasei-san/items/2d56fd1114b98b9b9905
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
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 |
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
Factory Method パターンは、他のクラスのコンストラクタをサブクラスで上書き可能な自分のメソッドに置き換えることで、 アプリケーションに特化したオブジェクトの生成をサブクラスに追い出し、クラスの再利用性を高めることを目的とする。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment