パンくず
リファクタリング-プログラマーの体質改善テクニック
データの再編成
双方向関連の単方向への変更
内容
リファクタリング名
双方向関連の単方向への変更
適用ケース要約
相互に参照しているが、片方の参照が不要になった場合
適用内容要約
単方向に参照するように変更を行う。
適用詳細
A→B、B→Aへの参照が存在し、B→Aが使用されていない場合。
B→Aへの参照を削除する。
サンプル
著者と読者の関係を実装します。
著者が読者に関心を持つことを前提に設計したが
あまり関心を持たなかったため、著者から読者への参照を無くします。
※単方向関連の双方向への変更の逆
サンプルコード
# encoding: Shift_JIS require 'set' class BookReader attr_reader:author attr_accessor:name def initialize(author,name) @author=author set_author(author) @name=name end def answer_author_name() puts "私(#{@name})が読んでいる本の著者は#{@author.name}です" end def set_author(author) @author.friend_book_readers.delete(self) unless @author.nil? @author.friend_book_readers.add(self) unless @author.nil? end end class Author attr_accessor:name,:book_readers def friend_book_readers() @book_readers end def initialize(name) @name=name @book_readers=Set.new end def answer_book_readers message = "" book_readers.each {|reader| message << "#{reader.name}さん、" } message << "は私(#{@name})の本の読者です" puts message end end yuki = Author.new("結城浩") tanaka = BookReader.new(yuki,"田中") suzuki = BookReader.new(yuki,"鈴木") fowler = Author.new("マーティン・ファウラー") sato = BookReader.new(fowler,"佐藤") tanaka.answer_author_name suzuki.answer_author_name sato.answer_author_name yuki.answer_book_readers fowler.answer_book_readers
出力(リファクタリング前)
私(田中)が読んでいる本の著者は結城浩です 私(鈴木)が読んでいる本の著者は結城浩です 私(佐藤)が読んでいる本の著者はマーティン・ファウラーです 田中さん、鈴木さん、は私(結城浩)の本の読者です 佐藤さん、は私(マーティン・ファウラー)の本の読者です
# encoding: Shift_JIS require 'set' class BookReader attr_reader:author attr_accessor:name def initialize(author,name) @author=author @name=name end def answer_author_name() puts "私(#{@name})が読んでいる本の著者は#{@author.name}です" end end class Author attr_accessor:name def friend_book_readers() @book_readers end def initialize(name) @name=name @book_readers=Set.new end def add_book_reader(book_reader) @book_readers.add book_readers end end yuki = Author.new("結城浩") tanaka = BookReader.new(yuki,"田中") suzuki = BookReader.new(yuki,"鈴木") fowler = Author.new("マーティン・ファウラー") sato = BookReader.new(fowler,"佐藤") tanaka.answer_author_name suzuki.answer_author_name sato.answer_author_name
出力(リファクタリング後)
私(田中)が読んでいる本の著者は結城浩です 私(鈴木)が読んでいる本の著者は結城浩です 私(佐藤)が読んでいる本の著者はマーティン・ファウラーです