David Menard
David Menard

Reputation: 2321

Decision making design pattern help

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

Upvotes: 0

Views: 4616

Answers (4)

Jai
Jai

Reputation: 1

Take a look at chain of responsibility. You separate out the responsibility of each Treat method, set up a command chain. Each command will attempt to handle the object.

Have a look here: http://www.dofactory.com/net/chain-of-responsibility-design-pattern

Upvotes: 0

Heath Lilley
Heath Lilley

Reputation: 383

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.

Upvotes: 3

JAB
JAB

Reputation: 21089

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

Upvotes: -3

Yuval Adam
Yuval Adam

Reputation: 165282

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();

Upvotes: 2

Related Questions