tags:

views:

45

answers:

3

Hey,

I have a situation where (pseudo-code):

Action a;
Object o;
if(objectIsPartOfGroup(o, "Group1"))
  a = treatCaseGroup1();
if(a != indecisive)
  return a;

if(objectIsPartOfGroup(o, "Group2"))
  a = treatCaseGroup2();
if(a != indecisive)
  return a;

if(objectIsPartOfGroup(o, "Group3"))
  a = treatCaseGroup3();
if(a != indecisive)
  return a;
.
.
.

I was wondering if there is a pattern applicable to this situation, so that I don't have to repeat the "if(a != indecisive) return a;" check after every step? I find that repeating this code over and over is not very... professional? It adds a whole lot of code lines and doesn't at all help clarity, therefore I find it sucks.

EDIT: an object can be part of group1 and group2 and group3, etc... so say an object is part of group1 and the action is undecisive, since it is also part of group2, it will be treated again and again, untill all groups have been treated. At the end, the result CAN be undecisive too!

Thanks for your help!

Dave

+2  A: 

Check out the Visitor design pattern.

In essence, Visitor pattern is all about performing different operations on different static objects without binding them too tight.

Correctly refactoring your code, you would simply do:

o.treat();
Yuval A
sry I wasnt clear. If the object is part of group1, it can also be part of group2, group3, etc. so if I call treat(); on my object, it can still finish with "undecisive". Plus, the end result CAN be undecisive. Please correct me if i'm totally of course!
David Menard
A: 

It's not a pattern, and it's not anything in-depth (it resolves only the specific thing you asked about), but would something like this work?

Action a;
Object o;

while (a == indecisive)
{
    if(objectIsPartOfGroup(o, "Group1"))
        a = treatCaseGroup1();
    if(objectIsPartOfGroup(o, "Group2"))
        a = treatCaseGroup2();
    if(objectIsPartOfGroup(o, "Group3"))
        a = treatCaseGroup3();
 }

 return a
JAB
sry I wasnt clear. If the object is part of group1, it can also be part of group2, group3, etc. So that if I do this, o, beeing part of group1 and 2, will always treat group1 and always be undecisive.
David Menard
@David: The Visitor design pattern is probably the way you should go, based on what Yuval A said.
JAB
@JAB: I just don't see how the visitor can treat a single object multiple times if its part of different groups
David Menard
+1  A: 
public Action determimeAction( Object o, List<String> groups ) {
    for ( String group : groups ) {
        if ( ( ( GroupI ) o ).isPartOf( group ) ) {
            Action a = ( ( GroupI ) o ).treatCaseGroup( group );
            if ( a != indecisive ) {  // could this be a.isDecicive()
                return a;
            }
        }
    }
    return null; // or whatever
}

public interface GroupI () {
    public Action treatCaseGroup( String group );   // is implemented to know which Action to get.
    public Boolean isPartOf( Stirng group ); // is implemented the same as your example just move the method to your object
}

public class GroupImpl implements GroupI {
    public Boolean isPartOf( Stirng group ) {
    }
    public Action treatCaseGroup( String group ) {
        // use if, case, factory, or Dep Inection to get the correct action.
    }
}

Without knowing all the logic something like this should work.

HeathLilley
Thanks, this would work! i'll wait to see if any other ideas come up
David Menard