Created
March 10, 2022 00:59
-
-
Save pbkwee/78505c78a635b3d682100780db294cfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Using a Consumer pattern vs. a constructor to populate an Enum instance. Handy if you | |
have a good number of fields and don't want long argument lists. Or if you need to do | |
some processing/method calling. | |
final fields would cause issues and would need to be done via a constructor without a callback. | |
public enum SomeEnum { | |
A((c)->{c.someField="A";}), | |
B((c)->{c.lotsMoreFields="B";}), | |
/*Syntax error | |
C(null) { | |
this.someField="a"; | |
}*/ | |
; | |
SomeEnum(Consumer<SomeEnum> c) { | |
c.accept(this); | |
} | |
String someField; | |
String lotsMoreFields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment