saasbook

demeter_delegation_1.rb

Aug 15th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.40 KB | None | 0 0
  1. # Better: delegate credit_balance so MovieTheater only accesses Moviegoer
  2. class Moviegoer
  3.   def credit_balance
  4.     self.wallet.credit_balance  # delegation
  5.   end
  6. end
  7. class MovieTheater
  8.   def collect_money(moviegoer,amount)
  9.     if moviegoer.credit_balance >= amount
  10.       moviegoer.credit_balance -= due_amount
  11.       @collected_amount += due_amount
  12.     else
  13.       raise InsufficientFundsError
  14.     end
  15.   end
  16. end
Add Comment
Please, Sign In to add comment