From: Jim Weirich Date: 2005-08-12T14:27:32+09:00 Subject: Re: Head First Design Patterns - Strategy Pattern [was: Java/C# "interface" in Ruby ?] On Thursday 11 August 2005 11:41 pm, dave.burt@gmail.com wrote: > If a Strategy is a single function, I'd probably do it more like this, > though: [... proc based solution elided ...] Agreed, unless the strategy object requires state. E.g. class Employee attr_accessor :payment_strategy def pay @payment_strategy.pay end end class HourlyStrategy def initialize(hourly_rate, timecards) @rate = hourly_rate @timecards = timecards end def pay @timecards.inject(0.0) { |s, tc| s + tc.calculate_pay(@rate) } end end class SalaryStrategy def initialize(salary) @weekly_salary = salary end def pay @weekly_salary end end -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)