Could there be a way to merge two quite similar functions together?
I hatethis kind of duplicate code:
public List get_A_List()
{
List allObjs = getAllObjs();// get all objects
loop (every obj in allObjs)
{
if (obj satisfies condition A)
{
returnList.add(obj);
}
return returnList;
}
}
public List get_B_List()
{
List allObjs = getAllObjs();// get all objects
loop (every obj in allObjs)
{
if (obj satisfies condition B)
{
returnList.add(obj);
}
return returnList;
}
}
The solution comes to my mind is likethis:
define a ConditionEnum{A,B}
and aprivate function:
private List getList(CondtionEnum condition)
{
if (condtion == ConditionEum.A)
{
get all objs that satisfies condition A
}
else (...)
{
...
}
return returnList;
}
public List get_A_List()
{
return getList(ConditionEum.A)
}
Any ideas?
[2147 byte] By [
bshya] at [2007-11-27 4:32:55]

Either that or pass a reflective reference to a method performing the check.
reflective reference? Could more details?
bshya at 2007-7-12 9:42:41 >

Why not add a method to your objects in the list such that it returns true for a particular condition?
Like this (not tested, sanity checks and exception handling omitted):
public void test() {
// example for case "A"
List l = getList(getClass().getMethod("checkA", new Class[] {Object.class}));
}
public List getList(Method check0r) {
List all = getAllObjs();
List ret = new ArrayList();
for (Iterator it = l.iterator();it.hasNext();) {
Object o = it.next();
if (((Boolean) check0r.invoke(this, new Object[] {o})).booleanValue()) {
ret.add(o);
}
}
return ret;
}
public boolean checkA(Object o) {
...
}
public boolean checkB(Object o) {
...
}
Try this
interface ListCondition {
boolean satisfied(Object obj);
}
...
public List getList(ListCondition condition) {
List allObjs = getAllObjs();
for(Iterator iter = allObjs.iterator(); iter.hasNext()) {
Object obj = iter.next();
if ( condition.satisfied(obj) ) {
returnList.add(obj);
}
return returnList;
}
// later that day
ListCondition conditionA = new ListCondition() {
public boolean satisfied(Object obj) {
return (CONDITION_IS_MET_EXPRESSION_HERE);
}
};
List list = getlist(conditionA);
// add others as you see fit
You can define as many conditions as you want, and even chain them together.
Isn't there a "filter pattern" that does just that, and looks suspiciously like georges previous... Goerge you've been cheating by studying again! Don't worry, I won't tell anyone.
Or you could do something like:
public abstract class ListFilter<E> {
protected abstract boolean test(E candiate);
pubic List<E> filter(List<? extends E> candidates) {
List<E> results = new ArrayList<E>(50);
for(E candidate : candidates)
if(test(candidate)
add(candidate);
return results;
}
> Isn't there a "filter pattern" that does just that,
> and looks suspiciously like georges previous...
> Goerge you've been cheating by studying again! Don't
> worry, I won't tell anyone.
Heh heh never heard of the filter pattern, I'd say that's a variation on the Visitor pattern meself. I replaced a bunch of hideously convoluted, nested-ten-deep methods in our project with something similar last year, and everyone went bananas for it. It's all over the place now, 1-0 to George :-)
> Or you could do something like:
>
> [code]
> public abstract class ListFilter<E> {
>
> otected abstract boolean test(E candiate);
>
> pubic List<E> filter(List<? extends E> candidates)
> {
>List<E> results = new ArrayList<E>(50);
> for(E candidate : candidates)
>if(test(candidate)
>add(candidate);
>return results;
>}
> code]
That's nice. You could decorate your filters with other filters, too
thanks.I like georgemc and malcolmmc 's solutions most.It seems that the OO is always in you guys' mind.
bshya at 2007-7-12 9:42:41 >
