How can I refactor this to work without breaking the pattern horribly?
Posted
by SnOrfus
on Stack Overflow
See other posts from Stack Overflow
or by SnOrfus
Published on 2010-03-11T22:34:32Z
Indexed on
2010/05/24
9:01 UTC
Read the original article
Hit count: 212
I've got a base class object that is used for filtering. It's a template method object that looks something like this.
public class Filter
{
public void Process(User u, GeoRegion r, int countNeeded)
{
List<account> selected = this.Select(u, r, countNeeded); // 1
List<account> filtered = this.Filter(selected, u, r, countNeeded); // 2
if (filtered.Count > 0) { /* do businessy stuff */ } // 3
if (filtered.Count < countNeeded)
this.SendToSuccessor(u, r, countNeeded - filtered) // 4
}
}
Select(...)
, Filter(...)
are protected abstract methods and implemented by the derived classes.
Select(...)
finds objects in the based on x criteria,Filter(...)
filters those selected further.- If the remaining filtered collection has more than 1 object in it, we do some business stuff with it (unimportant to the problem here).
SendToSuccessor(...)
is called if there weren't enough objects found after filtering (it's a composite where the next class in succession will also be derived from Filter but have different filtering criteria)
All has been ok, but now I'm building another set of filters, which I was going to subclass from this. The filters I'm building however would require different params and I don't want to just implement those methods and not use the params or just add to the param list the ones I need and have them not used in the existing filters.
They still perform the same logical process though.
I also don't want to complicated the consumer code for this (which looks like this)
Filter f = new Filter1();
Filter f2 = new Filter2();
Filter f3 = new Filter3();
f.Sucessor = f2;
f2.Sucessor = f3;
/* and so on adding filters as successors to previous ones */
foreach (User u in users)
{
foreach (GeoRegion r in regions)
{
f.Process(u, r, ##);
}
}
How should I go about it?
© Stack Overflow or respective owner