###前提・実現したいこと
ここ最近オブジェクト指向関連の稚拙な質問ばかりで申し訳ないです。
クラス設計についてどうすればいいのか分からないので教えて欲しいです。
##該当のソースコード
#####Report.py
python
1from Dict import Dictonary 2 3class Report: 4 def __init__(self,name,comment): 5 self._name = Name(name) 6 self._comment = Comment(comment) 7 8 def macthWords(self): 9 dict = Dictionary() 10 words = self._comment.parse() 11 return [word for word in words if dict.isMatch(word)] 12 13 14class Name: 15 def __init__(self,name): 16 self._name = name 17 18 def isSame(self,name): 19 return self._name == name 20 21class Comment: 22 def __init__(self,comment): 23 self._comment = comment 24 25 def parse(self): 26 #parse処理 27 return words
#####Dict.py
python
1class Dictionary: 2 def __init__(self): 3 self._dict = self.__loadDictionary() 4 5 def __loadDictionary(self): 6 #ファイルを読み込んで辞書を作成 7 return dict 8 9 def isMatch(self,word): 10 return word in self._dict
##この設計の問題点
今現状のクラス図はこのような感じでしょうか
NameとCommentはラップクラスです。
python
1 | 2 | 3 | 4[class Report:] ------------------------[class Dictionary:] 5 | 6 |--------------------┐ 7 | | 8[class Name:] [class Comment:]
問題が2つあります。
①ReportクラスのmatchWordsメソッドが呼ばれるたびに辞書を作成しまう。
②単一責任の原則に反しているのではないか?(commentでparseしたものをReportクラスで扱っている為)
②に関してはラップクラスとしてのCommentの責任からは離れているので問題ないのかなとは思っているのですが分からないので教えてください。
①は何か解決策がありましたら教えてください。
これ以外にも何かおかしかったり、こうした方がいいというのがあれば教えてください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/16 09:49
2017/06/16 09:57
2017/06/16 10:12
2017/06/16 10:17
2017/06/16 10:19
2017/06/16 10:26
2017/06/16 10:30
2017/06/16 10:36
2017/06/16 10:45
2017/06/16 10:59
2017/06/16 11:00