Example - Enum as constants public void onSomethingAction(int status) { switch (status) { case Status.OK: // => int Log.d(TAG, "status: OK"); break; case Status.ERROR: // => int Log.d(TAG, "status: ERROR"); break; } }
* type safe ! - "int enum pattern" is fragile - Add/Remove a new item easliy * Prefixes of names are unnecessary - Because each enum types has its own namespace Enum as constants
Enum that has member constants/methods public enum Status { OK("Succeed the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Enum that has member constants/methods public enum Status { OK("Succeed the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Enum that has member constants/methods public enum Status { OK("Succeed the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Example - Enum that has member constants/methods public void onSomethingAction(int status) { switch (status) { case Status.OK: Log.d(TAG, "Succeed the process!"); break; case Status.ERROR: Log.d(TAG, "Error has occurred..."); break; } }
Example - handling click event public void onClick(View v) { swtich(v.getId()) { case R.id.button1: // do something... break; case R.id.button2: // do something... break; // ... } }
* Delegates event handling from controller - We can avoid fat controller !!!! * Improves testability - Because they are referentially transparent (?) * Other usecases - BLE (BluetoothGattCharacteristic), HTTP response, etc. Event handling with enum