4 ms·
The Visitor pattern is useful for some designs and it can be quite natural with inheritance. GUIs also tend to lend themselves nicely to inheritance. Streams ar
by mehrdadn 6y ago
The Visitor pattern is useful for some designs and it can be quite natural with inheritance. GUIs also tend to lend themselves nicely to inheritance. Streams are also generally nice to implement with inheritance as well. More generally, dynamic components that interoperate with each other also inevitably lend themselves well to a class design. Lots of things are nice to implement with class hierarchies actually.
That said, you don't need an OOP language or "classes" to write an object-oriented program (with subtyping etc.); you can do it in C with message passing, callbacks, etc... Windows basically forces you to do this when using the Win32 GUI API. It's not really a counterexample though. It's not 100% exactly the same thing (and you might have some interesting discussions around this) but for the most part it can be quite similar in spirit and mostly just a matter of doing everything as classes would do, except manually.
If you haven't had a need for serious OOP then either your job involves writing a different type of code (e.g. data crunching might be better with just functions everywhere) or you need to invest more time learning object-oriented design. If it's just the usual "a Dog is an Animal" thing to you then you're missing out on an entire world. A huge slice of programmers either don't know to take it seriously, don't want to take it seriously, or don't have to take it seriously, and end up either hating OO design or finding it pointless or trivial. But it's quite a deep subject that is generally taught rather poorly and superficially (and possibly far, far too early) in courses; you're lucky if they even mention LSP, and there's so much beyond that that they likely won't touch at all. There's probably enough to it to talk about it in a graduate course if not more. And in practice, coming up with a good OO design can be quite challenging (and I'm talking about cases where OO is the solution); languages that force you to do everything as classes and objects often end up making a complete mockery out of it (like Java), but if you ever have to do things that are very dynamic (making a plugin architecture is often like this) it can be almost impossible to avoid.
P.S. Inheritance (which results in class hiearchies) is just a mechanism that's used for more than just OOP. It's also used for mixin purposes, which aren't really about OOP at all, but just about code reuse, especially in a language where there's no better solution. I haven't mentioned things like these above, but they come up a lot too. (e.g. the HTTP server in Python has a ThreadingMixin base class, though you can probably have a discussion about whether it's the best solution or not.)
- zozbot234 6y agoThe 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 6y 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 6y 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.