Skip to content

Instantly share code, notes, and snippets.

@armandofox
Created May 11, 2021 16:39
Show Gist options
  • Save armandofox/6a636b98e53b84c9628eba411d3d4086 to your computer and use it in GitHub Desktop.
Save armandofox/6a636b98e53b84c9628eba411d3d4086 to your computer and use it in GitHub Desktop.
demeter_delegation_1.rb
# Better: delegate credit_balance so MovieTheater only accesses Moviegoer
class Moviegoer
def credit_balance
self.wallet.credit_balance # delegation
end
end
class MovieTheater
def collect_money(moviegoer,due_amount)
if moviegoer.credit_balance >= due_amount
moviegoer.credit_balance -= due_amount
@collected_amount += due_amount
else
raise InsufficientFundsError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment