# Decorators let you layer on functionality to existing operations, i.e serve a similar purpose to callbacks. # For cases where including callback logic in the model would give the model too many responsibilities, a Decorator is useful. # Imagine we have this logic on stackoverflow: # A user can post a question on a stackoverflow, after posting an email notification is sent to # all users have interest in the question topic, a post is shared on the user profile with a link to the question he posted # BAD # app/models/question.rb after_create :handle_after_create_logic private def handle_after_create_logic # queue email to be sent # create post end # The question model should not be responsible for all this logic # Good class SOFQuestionNotifier def initialize(question) @question = question end def save if @question.save post_to_wall queue_emails end end private def post_to_wall end def queue_emails end end # app/controllers/questions_controller def create @question = SOFQuestionNotifier.new(Question.new(params[:question])) if @question.save # Success else # Error end end