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. Brian Kelly 15.9K posts
- 2. Yamamoto 71.4K posts
- 3. #VERZUZ 56.7K posts
- 4. Colorado 35K posts
- 5. Birdman 15.3K posts
- 6. No Limit 36.5K posts
- 7. Deion 2,217 posts
- 8. Mia X 577K posts
- 9. Cash Money 30.3K posts
- 10. Salter 1,808 posts
- 11. Wayne 37.4K posts
- 12. Texas A&M 14.9K posts
- 13. Coach Prime 1,476 posts
- 14. Death Valley 5,912 posts
- 15. Mizkif 19.2K posts
- 16. Michigan 39.2K posts
- 17. Turk 571K posts
- 18. Aggies 5,563 posts
- 19. #GoBlue 3,686 posts
- 20. Baton Rouge 3,274 posts
Something went wrong.
Something went wrong.