我有一些自定义集合类。每个服务提供各种自定义类型的集合 - 一个自定义类型到一个自定义集合。自定义集合继承List<T>
[其中T在这种情况下是特定的自定义类型,而不是泛型]并提供一些附加功能。几乎具有共同代码的类
我以前没有使用自定义集合并在其他地方使用自定义方法,但是我发现在扩展代码时需要使用自己的方法进行集合。
这一切正常,一切都很开心。但它激怒了我,因为我知道我做得不好。问题是,每个类使用几乎相同的代码,只改变类型和参数,所以我觉得它可以实现为抽象类,或泛型,或List的扩展,或...但我没有真正理解这些差异或者如何去解决我需要的东西。
这里是我的两个数的集合,让你的想法:
// JourneyPatterns
public class JourneyPatterns : List<JourneyPattern>
{
private Dictionary<string, JourneyPattern> jpHashes; // This is a hash table for quick lookup of a JP based on its values
/* Add a journey pattern to the JourneyPatterns collection. Three methods for adding:
1. "Insert Before" (=at) a particular point in the list. This is the method used by all three methods.
2. "Insert After" a particular point in the list. This is "before" shifted by 1 e.g. "after 6" is "before 7"
3. "Append" to the end of the list. This is "before" with a value equal to the list count, and is the same as inherited "Add", but with checks
*/
public JourneyPattern InsertBefore(JourneyPattern JP, int before)
{
// check for a pre-existing JP with the same parameters (ignore ID). Do this by constructing a "key" based on the values to check against
// and looking it up in the private hash dictionary
JourneyPattern existingJP;
if (jpHashes.TryGetValue(JP.hash, out existingJP)) { return existingJP; }
else
{
// construct a new ID for this JP
if (string.IsNullOrWhiteSpace(JP.id)) JP.id = "JP_" + (Count + 1).ToString();
// next check that the ID specified isn't already being used by a different JPS
if (Exists(a => a.id == JP.id)) JP.id = "JP_" + (Count + 1).ToString();
// now do the add/insert
if (before < 0) { Insert(0, JP); } else if (before >= Count) { Add(JP); } else { Insert(before, JP); }
// finally add to the hash table for fast compare/lookup
jpHashes.Add(JP.hash, JP);
return JP;
}
}
public JourneyPattern InsertAfter(JourneyPattern JP, int after) { return InsertBefore(JP, after + 1); }
public JourneyPattern Append(JourneyPattern JP) { return InsertBefore(JP, Count); }
}
// JourneyPatternSections
public class JourneyPatternSections : List<JourneyPatternSection>
{
private Dictionary<string, JourneyPatternSection> jpsHashes; // This is a hash table for quick lookup of a JPS based on its values
/* Add a journey pattern section to the journeyPatternSections collection. Three methods for adding:
1. "Insert Before" (=at) a particular point in the list. This is the method used by all three methods.
2. "Insert After" a particular point in the list. This is "before" shifted by 1 e.g. "after 6" is "before 7"
3. "Append" to the end of the list. This is "before" with a value equal to the list count, and is the same as inherited "Add", but with checks
*/
public JourneyPatternSection InsertBefore(JourneyPatternSection JPS, int before)
{
// check for a pre-existing JPS with the same parameters (ignore ID). Do this by constructing a "key" based on the values to check against
// and looking it up in the private hash dictionary
JourneyPatternSection existingJPS;
if (jpsHashes.TryGetValue(JPS.hash, out existingJPS)) { return existingJPS; }
else
{
// construct a new ID for this JPS
if (string.IsNullOrWhiteSpace(JPS.id)) JPS.id = "JPS_" + (Count + 1).ToString();
// next check that the ID specified isn't already being used by a different JPS
if (Exists(a => a.id == JPS.id)) JPS.id = "JPS_" + (Count + 1).ToString();
// now do the add/insert
if (before < 0) { Insert(0, JPS); } else if (before >= Count) { Add(JPS); } else { Insert(before, JPS); }
// finally add to the hash table for fast compare/lookup
jpsHashes.Add(JPS.hash, JPS);
return JPS;
}
}
public JourneyPatternSection InsertAfter(JourneyPatternSection JPS, int after) { return InsertBefore(JPS, after + 1); }
public JourneyPatternSection Append(JourneyPatternSection JPS) { return InsertBefore(JPS, Count); }
}
正如你所看到的,什么是不同的类型(JourneyPattern,或JourneyPatternSection),和我是前缀用于类型(“JP_”或“JPS_”)的“id”属性。其他一切都很常见,因为确定“唯一性”(属性“散列”)的方法是定制类型的一部分。
我的一些自定义集合需要更多的参与和这些方法的不同实现,这是很好的,但这是最常见的,我已经实现了它大约6次,这似乎是没有意义的,和b)更难保持。
您的想法和帮助表示赞赏!
让你的类实现一些包含的接口s属性“id”和“hash”并且限制到那个接口的类型(class YourCollection:List where T:IYourInterface)。 –
Evk
好的,我可以看到如何工作。据推测,我还需要自定义类型来在界面中实现另一个属性,例如“id_pfx”这样的集合知道用什么前缀编号 – StuartR143
是的,还是让集合本身存储前缀,就像回答下面的建议。 – Evk