3 ms·
Trivial Builders are often used in java due to lack of named parameters or object literals. Trivial Visitors are sometimes used due to the anemic switch/enum su
by singron 2y ago
Trivial Builders are often used in java due to lack of named parameters or object literals. Trivial Visitors are sometimes used due to the anemic switch/enum support, although it's gotten a little better with switch expressions and switch patterns. Singletons and factories are used since java metaprogramming usually requires an instance of an class and can't use the class itself. E.g. you can only call interface methods on an instance, unlike e.g. a rust trait where you can call a trait method on a type without an instance of that type.
In non-trivial cases, these are still useful patterns though in many languages. E.g. traversing a complex AST is often an ideal case for a Visitor. Builders are useful e.g. you want to validate relationships between objects, so you keep a hashmap while building so you can do O(1) validation while building and provide targeted error messages, but then throw away the hashmap after building. Builders are especially useful if you avoid mutable state since you can often contain the mutable parts to the Builder and make the built object entirely immutable.
A lot of the patterns are useful, but you would unknowingly "use" them when appropriate without ever having explicitly learned them. E.g. the Adapter pattern is an obvious solution to a particular problem. I think the only contribution the book has for these patterns is giving these names.