JEP 354: Switch Expressions (Preview) in JDK-13 and JEP 361: Switch Expressions (Standard) in JDK-14 will extend the switch statement so it can be used as an expression. I will post any updates or enhancements to this Gist on Github. Once nice thing about this approach is that Switch/Case statements are all about side affects, this encapsulates the side effects in Classes so they can be managed, and re-used better, it ends up being more like Pattern Matching in a Functional language and that isn't a bad thing. This is just a quick straw man that I whipped up in a few minutes, a more sophisticated implementation might allow for some kind of Command Pattern to be injected into the Case implementations instances to make it more of a call back IoC style. combining both types of Case implementations ("Case %s is between %s and %s, break = %s\n", type, this.start, this.end, super.breakOnCompletion) and example of how to do standard "switch/case" logic with this pattern.įinal Switch inRangeCaseSwitch = new Switch() įinal Case rangeCase = new InRangeCase(5, boolean of(final Integer type) more instances for each matching pattern, granted this will get verbose with lots of options but is just ("Case %d, break = %s\n", type, super.breakOnCompletion) Public static void main(final String args)įinal Switch integerSwitch = new Switch() įinal Case case1 = new boolean of(final Integer type) * args command line arguments aren't used in this example * Show how to apply a Chain of Responsibility Pattern to implement a Switch/Case construct (pareTo(this.end) = EQUALS || pareTo(this.end) = LESS_THAN) Return (pareTo(this.start) = EQUALS || pareTo(this.start) = GREATER_THAN) & Public InRangeCase(final T start, final T end, final boolean breakOnCompletion) Public InRangeCase(final T start, final T end) Private final static int GREATER_THAN = 1 Public static abstract class InRangeCase> extends AbstractCase * Concrete example of an advanced Case conditional to match a Range of values Public EqualsCase(final T type, final boolean breakOnCompletion) Public static abstract class EqualsCase> extends AbstractCase * Example of standard "equals" case condition Protected AbstractCase(final boolean breakOnCompletion) Protected final boolean breakOnCompletion Public static abstract class AbstractCase> implements Case * Generic enabled Object Oriented Switch/Case construct Here is an example implementation that is also Type Safe using Generics. SwitchDemoFallThrough shows statements in a switch block that fall through.One Object Oriented option to replace excessively large switch and if/else constructs is to use a Chain of Responsibility Pattern to model the decision making. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered. Control flow continues with the first statement following the switch block. Each break statement terminates the enclosing switch statement. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.Īnother point of interest is the break statement. You could also display the name of the month with if-then-else statements:ĭeciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. The switch statement evaluates its expression, then executes all statements that follow the matching case label. A statement in the switch block can be labeled with one or more case or default labels. The body of a switch statement is known as a switch block. In this case, August is printed to standard output.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |