Bob
Bob

Reputation: 71

what's the right design pattern for complicate decision making system

I am working on the design of a decision making system that invokes complicate logic, potentially I will need to use a lot of nested if/else statements,

I want to see if is there a better design patter than can help me to simplify the structure of the system and provide a certain extensibility for future improvement.

The problem of the project can be simpified as:

We are now need to making a decision for a request, which has 3 type of properties, and potentially more. They are PricePolicy (Contract/WholeSale/Retail/Discount), RequestType (Buy/Sell) and ProductType (Fashion/Household/Toys).

Each decision making is based on all the 3 properties of the request, because the type of price policy may change in the future, and more properties will be added to the decision making process.

So I am trying to avoid the making a large switch statement, which is ugly and deficult for future extensions. Such as:

switch(ProductType) {
 case Fashion:
   switch(PricePolicy) {
     case Contract:
       if(Request == Buy) {
         // making a decision.
       } else {
       }
     }
   }
}

Please share you idea and suggestions Thank you.

Cheers, Bob

Upvotes: 4

Views: 2362

Answers (3)

foood
foood

Reputation: 11

As ziplin suggested, I would consider a 3d array if there is a decision for each combination (ie. not a 'sparse' distribution of decisions - I think that's the correct term). This would require that you are able to encapsulate the decision logic in an expression which can be stored within the array. With a sparse distribution, an adjacency list could be used in a similar fashion.

Upvotes: 0

duffymo
duffymo

Reputation: 308928

Maybe you want a Rete rules engine. Try Drools.

Or a data-driven decision table.

If you want a class solution, think polymorphism. Replace all those if/then/else cases with classes like Strategy or Visitor.

The key is stable interfaces. If you can keep that stable, and change the implementations underneath, you've got it.

Upvotes: 6

Dlongnecker
Dlongnecker

Reputation: 3047

Use a n-dimensional array, and lookup/query/modify your decisions that way.

Upvotes: 0

Related Questions