Some Rare but Invaluable Java Patterns 🧵 1) Null Object Pattern Avoid null checks by using a no-op object that implements expected behavior. It keeps code clean by avoiding conditionals around method calls (e.g., if (obj != null) everywhere). Use when returning null leads…
2) Parameter Object Pattern Bundle related parameters into a single object. You can pass the parameter object around different layers, ensuring consistency. Use when methods have too many related args.
3) Fluent Interface / Method Chaining Chain method calls for better readability. Communicates intent clearly (especially in builder-like objects). Use in builders or config-style setups.
4) Execute Around Method Pattern Abstract setup/teardown around an operation. Prevents resource leaks by always handling teardown properly. Use for resource management (e.g. opening/closing files/DB).
5. Initialization-on-Demand Holder (a variant of Singleton) Lazy-load a singleton safely without synchronized. Use when you want performant, thread-safe singleton.
Use no-op objects with caution as they just pretend to be real values. But they do not. In your sample, NullPayment::pay(), does nothing but the payment processor thinks the payment is completed. It 's better to use Optionals in most of the cases...
IMHO, somehow we must know if an object is null to know how we should proceed.
At line # 5, it doesn't avoid a conditional; only it is transformed, but not avoided. In certain cases, I think is necessary to check if an object is null; so, conditionals couldn't be avoided.
Bro... this is just hard to watch. Just use Kotlin already.
United States 趨勢
- 1. Jamaica 164K posts
- 2. JUNGKOOK 942K posts
- 3. #NationalFirstRespondersDay N/A
- 4. Wikipedia 84.5K posts
- 5. #10SET 1,221 posts
- 6. Grokipedia 132K posts
- 7. Good Tuesday 36.6K posts
- 8. $MSFT 50.2K posts
- 9. Atlantic 32.8K posts
- 10. Hurricane Melissa 129K posts
- 11. Nelson 24.5K posts
- 12. #tuesdayvibe 2,535 posts
- 13. Eastern Pacific 4,889 posts
- 14. Taco Tuesday 12.3K posts
- 15. $PYPL 36.7K posts
- 16. Riley Gaines 83.8K posts
- 17. #NationalChocolateDay N/A
- 18. Freddie 115K posts
- 19. Christian Zionism 5,019 posts
- 20. Jackie Robinson N/A
Something went wrong.
Something went wrong.