3 ms·
The Visitor pattern uses inheritance in a rigorously controlled way to implement a glorified switch() statement. The underlying issue with implementation inheri
by zozbot234 7y ago
The Visitor pattern uses inheritance in a rigorously controlled way to implement a glorified switch() statement. The underlying issue with implementation inheritance is that it does not have sensible semantics in the general case: it introduces all sorts of long-range and unpredictable couplings (failures of modularity) between a base object and its future "derived" versions. Pure interface inheritance does not have this issue, because it inherently decouples the interface from its implementations.
- mehrdadn 7y agoYou can totally have modularity and lack of coupling with implementation inheritance. That doesn't mean you necessarily will in every problem (especially if you're not paying attention), but in general it's a false dichotomy. Also, calling a visitor a glorified switch() is technically correct but makes a mockery out of it. You generally wouldn't want to (well, shouldn't want to) implement it as a switch(), though in simpler situations there might not be much difference. It can have implications on extensibility, modularity, etc... which is kind of obvious, otherwise everyone would be doing that and not going through the trouble of making a visitor.
- yann-gael 7y agoOnly because of the confusion in most programming language between typing and reusing... In the best of world, "inheritance" should mean either typing (à la Java interfaces) or "reusing" through abstact/concrete classes.