I've just read martin fowler's article on CollectionClosure. At first I was happy, as I have about six months ago I started implementing a select closure in some of my C# collections, i.e.
public abstract class SomethingFilter
{
public abstract bool Matches(Something s);
}
public class FilterNewSomethings : SomethingFilter
{
public override bool Matches(Something s)
{
return s.IsNew;
}
}
public class SomethingCollection : CollectionBase
{
public SomethingCollection Filter(SomethingFilter filter)
{
SomethingCollection matches = new SomethingCollection();
foreach (Something s in List)
{
if (filter.Matches(s))
matches.Add(s);
}
}
Hopefully your reading this and saying ooh nice use of a visitor pattern and ok so far so good. The question is whether I should HAVE to implement this or rather re-implement this.
The good news is that generics will make this even easier (that example left to the reader) you will be implementing an inherited List (maybe FilterableList) and then using FilterableList<YourClass>, so you should have to implement the pattern only once.