パンくず
内容
リファクタリング名
委譲の隠蔽
適用ケース要約
クライアントがオブジェクト内の委譲クラスを呼び出している。
適用内容要約
サーバに委譲を隠すためのメソッドを作る
適用詳細
クライアント側がサーバー側のパラメータについて知っていると
両者の依存度が高くなります。
サーバー側のオブジェくトを返却する小さい委譲メソッドを用意することで、
この依存を無くすことが出来ます。
サンプル
HTMLのタグと属性を設定・出力する機能を実装します。
HTMLのタグを利用するHTMLのユーザーはタグと属性の両方を知っています。
この場合、属性の情報はHTMLのユーザーが知る必要ない情報のため
タグの中で委譲を行い結合度を低くします。
サンプルコード
class HtmlUser attr_accessor:tag def initialize(tag_name) @tag=Tag.new(tag_name) end def add_attribute(attribute_name,value) attribute=Attribute.new(attribute_name) attribute.value=value @tag.attribute=attribute end def output_html() return "<#{@tag.name} #{@tag.attribute.name}=\"#{@tag.attribute.value}\" ></#{@tag.name}>" end end class Tag attr_accessor:name,:attribute def initialize(name) @name=name end end class Attribute attr_accessor:name,:value def initialize(name) @name=name end end html_user=HtmlUser.new("div") html_user.add_attribute("disabled","true") puts html_user.output_html
class HtmlUser attr_accessor:tag def initialize(tag_name) @tag=Tag.new(tag_name) end def add_attribute(attribute_name,value) @tag.set_name attribute_name @tag.set_value value end def output_html() return "<#{@tag.name} #{@tag.get_name}=\"#{@tag.get_value}\" ></#{@tag.name}>" end end class Tag attr_accessor:name,:attribute def initialize(name) @name=name @attribute=Attribute.new("") end def get_name() return @attribute.name end def set_name(attribute_name) @attribute.name = attribute_name end def get_value() return @attribute.value end def set_value(value) @attribute.value = value end end class Attribute attr_accessor:name,:value def initialize(name) @name=name end end html_user=HtmlUser.new("div") html_user.add_attribute("disabled","true") puts html_user.output_html