大まかに言えば、私が作成しているプログラムには、分類可能な少数のエントリ (おそらく常に 30 未満) の格納が含まれます。これらのエントリを表示できるようにしたいのですが、それらを使用してクラスの外部から変更されないようにしたいと考えています。変更可能な Entry というクラスと、Entry オブジェクトのラッパーである ReadOnlyEntry という別のクラスを作成しました。これらの Entry オブジェクトを整理する最も簡単な方法はList<List<Entry>>
、それぞれList<Entry>
がカテゴリである を作成することです。しかし、そのデータを読み取り専用で公開することは、面倒で複雑になりました。次の各タイプのオブジェクトを 1 つずつ用意する必要があることに気付きました。
List<List<Entry>> data;
List<List<ReadOnlyEntry>> // Where each ReadOnlyEntry is a wrapper for the Entry in the same list and at the same index as its Entry object.
List<IReadOnlyCollection<ReadOnlyEntry>> // Where each IReadOnlyCollection is a wrapper for the List<ReadOnlyEntry> at the same index in data.
IReadOnlyCollection<IReadOnlyCollection<ReadOnlyList>> readOnlyList // Which is a wrapper for the first item I listed.
リストの最後の項目はパブリックとして公開されます。1 つ目ではエントリを変更でき、2 つ目ではエントリを追加または削除でき、3 つ目ではカテゴリを追加または削除できます。データが変更されるたびに、これらのラッパーを正確に保つ必要があります。これは私には複雑に思えるので、これを処理する露骨に良い方法があるかどうか疑問に思っています。
編集 1: 明確にするために、私は List.asReadOnly() の使用方法を知っており、上記で提案したことは私の問題を解決します。より良い解決策を聞きたいだけです。コードを教えてください。
class Database
{
// Everything I described above takes place here.
// The data will be readable by this property:
public IReadOnlyCollection<IReadOnlyCollection<ReadOnlyList>> Data
{
get
{
return readOnlyList;
}
}
// These methods will be used to modify the data.
public void AddEntry(stuff);
public void DeleteEntry(index);
public void MoveEntry(to another category);
public void AddCategory(stuff);
public void DeleteCategory(index);
}