4 ms·
I think the word "strategy" encapsulates more about the semantics than "passing in a function". For example, the "visitor" pattern could also be described as "
by maffydub 10y ago
I think the word "strategy" encapsulates more about the semantics than "passing in a function".
For example, the "visitor" pattern could also be described as "passing in a function", but there's a significant difference: a visitor is intended to be called on each element of a data structure in turn (i.e. a bit like an iterator callback), while a strategy defines an algorithm that you're expecting the caller to select at run-time (e.g. different cipher implementations).
Syntactically, they may well be the same ("passing in a function") but semantically, they're very different.
It can be useful to be able to have this shared vocabulary when talking about designs.
- emodendroket 10y ago"Visitor" has always bothered me too. If you look it up it's tons and tons of text to describe "pass in a function and descend a graph calling it on each node."
- maffydub 10y agoSure, so some documentation isn't great (and, by having lots of documentation, you might be a bit disappointed at how simple it ends up being). ...but going back to my original point, isn't it more valuable to have a common vocabulary, meaning that I can say "visitor" rather than "pass in a function and descend a graph calling it on each node"?
- emodendroket 10y agoWell, in my opinion, no, because the fancy terminology obscures the simple idea. Also, the naming of the pattern, along with implementation, may push people to copy a design that would be the best way to do it in C++ or Java ca. 2000 but unnatural in whatever they're working in.
- gnaritas 10y agoIt's a simple word, visitor; that's not fancy terminology. Giving things names is not fancy, it's practical and it aids in communicating with other people who do the same thing. You're basically objecting to the fact that people like to give names to their idioms so they communicate better.
- emodendroket 10y agoNo, I'm objecting to the use of terms which I feel make communication worse, rather than better. You're free to disagree.
- gnaritas 10y ago> If you look it up it's tons and tons of text to describe "pass in a function and descend a graph calling it on each node." No, it's one word: visitor. That's the point, we can now avoid saying "pass in a function and descend a graph calling it on each node" and instead simply say visitor and everyone knows what we mean without the big long explanation.
- munificent 10y agoThat's actually not what the visitor pattern is about, though the way it's typically presented and the poor name tends to confuse the fact. Visitor isn't about traversing trees. It's about letting you define class-specific behavior outside of the definition of the class itself. It gives you a way to "add a method" to a set of classes without actually stuffing them in the class definitions themselves. In practice, most visitor patterns are used with AST classes or other types that are stored in a tree-like fashion, but that's coincidence. The visitor pattern itself is really about using virtual dispatch to avoid an ugly and non-type-safe type switch.
- Munksgaard 10y agoSo pattern matching?
- munificent 10y agoSort of. It's a way to emulate a simple subset of pattern matching in languages that lack it.
- emodendroket 10y agoI'm not sure I'm following what you describe. Like an extension method or mix-in?