Skip navigation links

Package de.team33.patterns.decision.leda

Provides classes and utilities for implementing or handling complex decisions.

See: Description

Package de.team33.patterns.decision.leda Description

Provides classes and utilities for implementing or handling complex decisions.

When decisions have to be made between several (more than two) possible results based on several independent criteria, confusing code often results. Example:

Given an Input parameter that offers three independent criteria ...

 public interface Input {

     boolean isConditionOne();

     boolean isConditionTwo();

     boolean isConditionThree();
 }
 

Now a choice should be made between five possible results {A, B, C, D, E} based on the criteria of the Input parameter ...

A classic normalized implementation might look something like this ...

 public enum Result {

     A, B, C, D, E;

     public static Result map(final Input input) {
         if (input.isConditionOne()) {
             if (input.isConditionTwo()) {
                 if (input.isConditionThree()) {
                     return C;
                 } else {
                     return D;
                 }
             } else {
                 if (input.isConditionThree()) {
                     return A;
                 } else {
                     return B;
                 }
             }
         } else {
             if (input.isConditionTwo()) {
                 if (input.isConditionThree()) {
                     return B;
                 } else {
                     return C;
                 }
             } else {
                 if (input.isConditionThree()) {
                     return E;
                 } else {
                     return A;
                 }
             }
         }
     }
 }
 

In practice, individual branches of the decision cascade can often be combined, which makes the code a little shorter but hardly clearer. Each additional criterion would roughly double the required code of the decision cascade.

The Decision classes defined in this module allows a different implementation of the same decision cascade ...

 public enum Result {

     A, B, C, D, E;

     private static final Variety<Input, Result> VARIETY =
             Variety.joined(Input::isConditionOne, Input::isConditionTwo, Input::isConditionThree)
                    .replying(A, B, C, D, E, A, B, C);

     public static Result map(final Input input) {
         return VARIETY.apply(input);
     }
 }
 

The interested programmer can decide for himself whether this is better. In my opinion, maintainability have been improved. Readability depends largely on whether the concept has been understood.

See Also:
Leda (Mond)
Skip navigation links

Copyright © 2025 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.