次のタイプのデータがあるとします。
class Customer {
String id; // unique
OtherCustData someOtherData;
}
class Service {
String url; // unique
OtherServiceData someOtherData;
}
class LastConnection {
Date date;
OtherConnData someOtherData; // like request or response
}
次に、各顧客が各サービスにいつ接続したかを覚えておく必要があります。
私は構造を作ります:
Map<Customer, Map<Service, LastConnection>> lastConnections;
または、id で検索できるようにするには、equal() と hashCode() をすべて記述する必要はありません。
Map<String, Map<String, LastConnection>> lastConnections;
これで LastConnection データにアクセスできるようになりました
LastConnection connection = lastConnections.get(custId).get(srvUrl);
特に、LastConnections のマップのマップを期待する数十のメソッドにパラメーターとして渡す必要があるため、次のような独自のクラスを作成することを考えています。
class CustomerConnections extends HashMap<String, LastConnection> {
}
class AllConnections extends HashMap<String, CustomerConnections> {
public LastConnection get(String custId, String srvUrl) {
return get(custId).get(srvUrl);
}
}
継承が 3v1l であることはすでにわかったので、構成を試してみましょう。
class CustomerConnections {
Map<String, LastConnection> customerConnections;
LastConnection get(String srvUrl) {
return customerConnections.get(srvUrl);
}
... // all other needed operations;
}
class AllConnections {
Map<String, CustomerConnections> allConnections;
public LastConnection get(String custId, String srvUrl) {
return get(custId).get(srvUrl);
}
public CustomerConnection get(String custId) {
return allConnections.get(custId);
}
... // all other needed operations;
}
問題は、SOLID の原則とすべてのベスト プラクティスを尊重する最善のアプローチが何であるかがわからないことです。既存のコレクションを拡張する以外に何もしないクラスを作成することは、必要以上にエンティティを増やすように見えますが、私のコードをより明確にします (特に次のレベルがある場合 - 月ごとの AllConnections のマップなど)。道順は?