Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 071a256fae3752f52482e7dd018e1448ea9594bd unknown committed Nov 15, 2009
@@ -0,0 +1,9 @@
+class AddDryIngredientsTask < Task
+ def initialize
+ super('Add dry ingredients')
+ end
+
+ def get_time_required
+ 1.0
+ end
+end
@@ -0,0 +1,9 @@
+class AddLiquidTask < Task
+ def initialize
+ super('Add Liquid Task')
+ end
+
+ def get_required_time
+ 2.0 # It takes 2 seconds to add liquid
+ end
+end
@@ -0,0 +1,24 @@
+class CompositeTask < Task
+ def initialize(name)
+ super(name)
+ @sub_tasks = []
+ end
+
+ def <<(task)
+ @sub_tasks << task
+ end
+
+ def remove_sub_task(task)
+ @sub_tasks.delete(task)
+ end
+
+ def [](index)
+ @sub_tasks[index]
+ end
+
+ def get_time_required
+ time = 0.0
+ @sub_tasks.each { |task| time += task.get_time_required }
+ time
+ end
+end
@@ -0,0 +1,15 @@
+require 'task'
+require 'add_dry_ingredients_task'
+require 'add_liquid_task'
+require 'mix_task'
+require 'composite_task'
+
+class MakeBatterTask < CompositeTask
+ def initialize
+ super('Make Batter')
+ @sub_tasks = []
+ add_sub_task(AddDryIngredientsTask.new)
+ add_sub_task(AddLiquidTask.new)
+ add_sub_task(MixTask.new)
+ end
+end
@@ -0,0 +1,9 @@
+class MixTask < Task
+ def initialize
+ super('Mix that batter up!')
+ end
+
+ def get_time_required
+ 3.0 # Mix for 3 minutes
+ end
+end
@@ -0,0 +1,4 @@
+require 'make_batter_task'
+
+main_task = MakeBatterTask.new
+puts "The main task will take #{main_task.get_time_required} minutes"
@@ -0,0 +1,11 @@
+class Task
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ end
+
+ def get_time_required
+ 0.0
+ end
+end
@@ -0,0 +1,23 @@
+require 'subject'
+
+class Employee
+ include Subject
+
+ attr_reader :name
+ attr_accessor :title, :salary
+
+ def initialize(name, title, salary)
+ super()
+ @name = name
+ @title = title
+ @salary = salary
+ end
+
+ def salary=(new_salary)
+ if salary == new_salary
+ return
+ end
+ @salary = new_salary
+ notify_observers
+ end
+end
@@ -0,0 +1,42 @@
+require 'employee'
+
+describe Employee do
+
+ before(:each) do
+ @employee = Employee.new('Fred', 'Crane Operator', 30000)
+ end
+
+ context "notifies observer" do
+
+ it "should notify its observer about a salary change" do
+ observer_mock = mock(:observer_mock)
+ observer_mock.should_receive(:update).with(@employee)
+
+ @employee.add_observer(observer_mock)
+ @employee.salary = 35000
+ end
+
+ it "should notify two observers about a salary change" do
+ observers = []
+
+ observers << mock()
+ observers[0].should_receive(:update).with(@employee)
+
+ observers << mock()
+ observers[1].should_receive(:update).with(@employee)
+
+ observers.each do |observer|
+ @employee.add_observer(observer)
+ end
+
+ @employee.salary = 36000
+ end
+
+ it "should not notify if the new salary is the same as the old" do
+ observer_mock = mock(:observer_mock)
+
+ @employee.add_observer(observer_mock)
+ @employee.salary = 30000
+ end
+ end
+end
@@ -0,0 +1,6 @@
+class Payroll
+ def update(changed_employee)
+ puts("Cut a new check for #{changed_employee.name}!")
+ puts("His salary is now #{changed_employee.salary}!")
+ end
+end
@@ -0,0 +1,24 @@
+require 'employee'
+require 'payroll'
+require 'tax_man'
+
+=begin
+fred = Employee.new('Fred', 'Crane Operator', 30000)
+
+payroll = Payroll.new
+fred.add_observer(payroll)
+
+tax_man = TaxMan.new
+fred.add_observer(tax_man)
+
+fred.salary = 35000
+=end
+
+# Use code block as observer instead of a real class
+fred = Employee.new('Fred', 'Crane Operator', 30000)
+fred.add_observer do |changed_employee|
+ puts("Cut a new check for #{changed_employee}!")
+ puts("His salary is now #{changed_employee.salary}!")
+end
+
+fred.salary = 35000
@@ -0,0 +1,19 @@
+module Subject
+ def initialize
+ @observers = []
+ end
+
+ def add_observer(observer)
+ @observers << observer
+ end
+
+ def remove_observer(observer)
+ @observers.delete(observer)
+ end
+
+ def notify_observers
+ @observers.each do |observer|
+ observer.update(self)
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class TaxMan
+ def update(changed_employee)
+ puts("Send #{changed_employee.name} a new tax bill!")
+ end
+end
@@ -0,0 +1,26 @@
+require 'report'
+
+HTML_FORMATTER = lambda do |context|
+ puts('<html>')
+ puts(' <head>')
+ puts(" <title>#{context.title}</title>")
+ puts(' </head>')
+ puts(' <body>')
+ context.text.each do |line|
+ puts(" <p>#{line}</p>")
+ end
+ puts(' </body>')
+ puts('</html>')
+end
+
+report = Report.new &HTML_FORMATTER
+report.output_report
+
+report = Report.new do |context|
+ puts("***** #{context.title} *****")
+ context.text.each do |line|
+ puts(line)
+ end
+end
+
+report.output_report
@@ -0,0 +1,14 @@
+class Report
+ attr_reader :title, :text
+ attr_accessor :formatter
+
+ def initialize(&formatter)
+ @title = 'Monthly Report'
+ @text = ['Things are going', 'really, really well.']
+ @formatter = formatter
+ end
+
+ def output_report
+ @formatter.call(self)
+ end
+end
@@ -0,0 +1,29 @@
+require 'report'
+
+class HTMLReport < Report
+ def output_start
+ puts('<html>')
+ end
+
+ def output_head
+ puts(' <head>')
+ puts(" <title>#{@title}</title>")
+ puts(' </head>')
+ end
+
+ def output_body_start
+ puts(' <body>')
+ end
+
+ def output_line(line)
+ puts (" <p>#{line}</p>")
+ end
+
+ def output_body_end
+ puts(' </body>')
+ end
+
+ def output_end
+ puts('</html>')
+ end
+end
@@ -0,0 +1,11 @@
+class PlainTextReport < Report
+
+ def output_head
+ puts("*** #{@title} ***")
+ end
+
+ def output_line(line)
+ puts line
+ end
+
+end
@@ -0,0 +1,8 @@
+require 'html_report'
+require 'plain_text_report'
+
+report = HTMLReport.new
+report.output_report
+
+report = PlainTextReport.new
+report.output_report
@@ -0,0 +1,42 @@
+class Report
+ def initialize
+ @title = 'Monthly Report'
+ @text = [ 'Things are going', 'really, really well.']
+ end
+
+ def output_report
+ output_start
+ output_head
+ output_body_start
+ output_body
+ output_body_end
+ output_end
+ end
+
+ def output_body
+ @text.each do |line|
+ output_line(line)
+ end
+ end
+
+ def output_start
+ end
+
+ def output_head
+ raise 'Called abstract method: output_head'
+ end
+
+ def output_body_start
+ end
+
+ def output_line(line)
+ raise 'Called abstract method: output_line'
+ end
+
+ def output_body_end
+ end
+
+ def output_end
+ end
+end
+

0 comments on commit 071a256

Please sign in to comment.