saasbook

demeter_delegation_2.rb

Aug 15th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.41 KB | None | 0 0
  1. class Wallet
  2.   attr_reader :credit_balance # no longer attr_accessor!
  3.   def withdraw(amount)
  4.      raise InsufficientFundsError if amount > @credit_balance
  5.      @credit_balance -= amount
  6.      amount
  7.   end
  8. end
  9. class Moviegoer
  10.   # behavior delegation
  11.   def pay(amount)
  12.     wallet.withdraw(amount)
  13.   end
  14. end
  15. class MovieTheater
  16.   def collect_money(moviegoer, amount)
  17.     @collected_amount += moviegoer.pay(amount)
  18.   end
  19. end
Add Comment
Please, Sign In to add comment