0

I'm trying to transform a simple if-else statement that I've found myself using all over the place to a more elegant lambda expression. But I'm having a hard time coming up with a solution from what I've been reading.

The simple statement goes like:

if (status.getStatus() == 'A') then
  handleA();
else if (status.getStatus() == 'B') then
  handleB();
else
  handleEverythingElse();

I know I could use the Command pattern with a map, but I'm sure there's some Java 8 elegance that I'm missing. Could someone show me the interface & impl, as well as it's usage in the body of the code (I learn by example)?

Any help would be much appreciated!

9
  • 7
    For starters you could use a switch statement... But why do you want a lambda anyway? If it works, don't fix it, right? Commented Aug 17, 2016 at 13:11
  • What are you expecting exactly? A usage of Function? Commented Aug 17, 2016 at 13:12
  • 2
    () -> { /* your code above */ }. Commented Aug 17, 2016 at 13:12
  • I think you are trying to overengineer your problem using some fancy new feature. In reality I don't think that it's a good idea, the only change to make is maybe use a switch instead of an if, even then I'm not sure if that's necessary. Commented Aug 17, 2016 at 13:14
  • 1
    Eh, so that is inside a switch. Time to rethink your approach, maybe? Commented Aug 17, 2016 at 13:15

2 Answers 2

2

If/else or even switch are not the "good OO design" answer. Neither are lambdas. Retrieving the status from somewhere, to make then a decision based on that - that is procedural programming; not OO.

You see, what you actually have in front of you - is a state machine. You have different states; and the point is: if your "thing" is in state A; then you want to do something ( invoke handleA() ). If your are in state B; you want to do something too ( like invoke handleB() ).

So, what you actually have is:

abstract class State {
  abstract void doIt();
...

StateA extends State {
  @Override
  void doIt() { handleA(); }

So, from the client side, you just call doIt() on same object of class State.

Thus: if you are really interested in improving your code base, learn how to use polymorphism to get rid of your if/else/switch statements.

You can watch this video to get an idea what I am talking about.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer to what looks to be an XY Problem.
@HovercraftFullOfEels Thanks. I remember my own face when I first was told about something like that in my own code. To then figure: creating good OO designs especially around this kind of problems can be so rewarding ... that I can't resist to share such ideas as often as possible.
0

Maybe I hadn't fully explained my problem, but my colleague proposed another solution that I really like. He suggested I make the if-else block a java.util.Supplier, and invoke it in the Entity class where I need it.

So my code went from this block of logic sprinkled everywhere:

public class ServiceImpl {
  ...
  if (entity.getStatus) == 'A' then
    finalStatus = handleA();
  else if (entity.getStatus() == 'B') then
    finalStatus = handleB();
  else
    finalStatus = handleEverythingElse();
  ...
}

To this nicely compacted form:

public class ServiceImpl {
  finalStatus = entity.getFinalStatus(this::handleStatus);

  public int handleStatus() {
    return dao.getStatus();
  }
}

With the implementation in my Entity class:

public class Entity {
  public int handleStatus(Supplier<Integer> s) {
    int finalStatus;
    if (status) == 'A' then
      finalStatus = handleA();
    else if (status() == 'B') then
      finalStatus = handleB();
    else
      finalStatus = supplier.get();
    return status;
  }
}

I hope this make sense...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.