Skip to content

Instantly share code, notes, and snippets.

@yamano
Created January 26, 2012 02:21
Show Gist options
  • Save yamano/1680545 to your computer and use it in GitHub Desktop.
Save yamano/1680545 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# ビルダーパターン
#
# Java言語で学ぶデザインパターンと同様の例題をRubyで記述した。
#
# coded by Takehiro Kaga
#
# 本パターンは構造を持ったインスタンスを組み上げていく
# Builderのインターフェイスを持ち具体的にインスタンスを組み上げる
# ConcreateBuilderがある
# DirectorはBuiderのインターフェイスだけを使って指示を出し、ConcreateBuilderに
# インスタンスを組み上げさせる
# 従って、色々なConcreateBuilderがあってもDirectorは影響を受けず指示できる
# 本パターンの特徴はConcreateBuilderの交換可能性を持つことである
# 例えばプレインテキスト形式、HTML形式、XML形式を交換可能である
class Builder # インターフェイスを定めている
def makeTitle(title)
end
def makeString(str)
end
def makeItems(items)
end
def close
end
end
class Director # Builderのインターフェイスを使ってインスタンスを生成する
def initialize(builder)
@builder = builder
end
def construct
@builder.makeTitle("Greeting)
@builder.makeString("朝から昼にかけて")
@builder.makeItems(
[
"おはようございます。",
"こんにちは。"
])
@builder.makeString("夜で")
@builder.makeItems(
[
"こんばんは。",
"おやすみなさい。",
"さようなら。"
])
@builder.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment