Skip to main content
8 events
when toggle format what by license comment
Feb 17, 2020 at 14:15 comment added davidbak @R.Schmitz - correct: courses too and how inheritance is used in practice except in Java codebases where you frequently (and stupidly) have multiple "parallel" inheritance trees for model/data access/uI. BTW - should have mentioned this before, but look at Kotlin for a language (only one I know of) with decent support for composition. Hopefully the idea will spread and even be improved on.
Feb 17, 2020 at 10:40 comment added R. Schmitz @davidbak Books, and courses! Think university, schools etc. And they all focus on it a lot when in reality the problem that it solves is... an edge case. Every "real" professional codebase I have seen uses inheritance in less than 1% of the code.
Feb 16, 2020 at 20:16 comment added davidbak Composition is woefully underused - for two reasons: 1) because most books promoting O-O design promote inheritance to a much greater degree, and 2) because most programming languages supply built-in constructs to support inheritance and nothing built-in to support composition (e.g., automatically forwarding method calls). (So composition requires manual "effort" where inheritance is practically free to the programmer.) Nevertheless - composition should be used much more often, and here's an example of where it should be used.
Feb 16, 2020 at 8:07 comment added Adam B Agreed but something like enemy movement is very liable to change. And be extended to other classes. Like ally. So using this protects you from such future changes.
Feb 16, 2020 at 6:09 comment added Bergi @AdamB If the only difference between those enemy types is that they use different move styles, you should indeed just inject the move style into the Enemy class.
Feb 15, 2020 at 20:00 comment added Adam B Not quite. You still have inheritance for your enemy types. But you use a composition style for their movement.
Feb 15, 2020 at 19:51 comment added Frank So would this be moving toward the "composition > inheritance" style? In "composition > inheritance," I would just implement a "Movable" interface (which would contain a movement strategy) that you would pass in to create a new game object.
Feb 15, 2020 at 19:35 history answered Adam B CC BY-SA 4.0