Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created March 10, 2012 01:39
Show Gist options
  • Save takaheraw/2009629 to your computer and use it in GitHub Desktop.
Save takaheraw/2009629 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# conding: utf-8
class Task
attr_reader :name
def initialize(name)
@name = name
end
def get_time_required
0.0
end
end
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
class AddATask < Task
def initialize
super('AddATask')
end
def get_time_required
1.0
end
end
class AddBTask < Task
def initialize
super('AddBTask')
end
def get_time_required
1.0
end
end
class MixTask < CompositeTask
def initialize
super('MixTask')
add_sub_task(AddATask.new)
add_sub_task(AddBTask.new)
end
end
mixTask = MixTask.new
p mixTask.get_time_required
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment