mercredi 20 septembre 2017

Persitst a composite pattern - ruby on rails

does anyone has a good end-to-end example of how to persist to the database a text book composite pattern from GoF such as the one below?

Component base class:

class Task
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def get_time_required
    0.0
  end
end

The composite class:

class CompositeTask < Task
  def initialize(name)
    super(name)
    @sub_tasks = []
  end

  def add_sub_task(task)
    @sub_tasks << task
  end

  def remove_sub_task(task)
    @sub_tasks.delete(task)
  end

  def get_time_required
    time = 0.0 
    @sub_tasks.each { |task| time += task.get_time_required }
    time
  end
end

1 commentaire:

  1. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Ruby on Rails Online Course Bangalore

    RépondreSupprimer