DEV Community

Discussion on: Template design pattern in Ruby. Any reason not to use a module instead of a superclass?

Collapse
 
briankephart profile image
Brian Kephart

Thanks for the reply! I thought this seemed like a good approach to the pattern, so it's nice to hear someone else has used it effectively.

... end up using instance methods from the subclass (or inheritor) in the abstract class.

I thought abstract classes weren't meant to be instantiated, just to provide behavior to their subclasses. Have I misunderstood?

Collapse
 
dfockler profile image
Dan Fockler • Edited

You're definitely right, abstract classes are not meant to be instantiated. That does not mean that the methods they implement are clean from pulling in dependencies, and when mixins/modules end up depending on their child classes (same with abstract classes), then you have problems.

module Commentable
  def comment(text)
    # You now depend on methods of the child class
    self.comments << text 
  end
end

class Post
  include Commentable
end