A question like this has been asked before, but I am specifically asking about using composition as an alternative to using modular mixins.
class Helper def do_somthing end end
If I need to βuseβ a class but not inherit it, I simply compose it and use it.
class MyStuff def initialize helper = Helper.new helper.do_something end end
Why do I need a module for this:
module Helper def do_something end end class MyStuff include Helper end
The only difference that I see will not be many Helper objects if I use modules. But I do not see anything with a large number of objects lying around larger objects.
Also, I don't know if I need to subclass it in the future. So, how do I decide if my library users want to use the mixin module or want to use composition?
inheritance ruby multiple-inheritance mixins composition
codeObserver
source share