Created
May 11, 2021 16:39
-
-
Save armandofox/6a636b98e53b84c9628eba411d3d4086 to your computer and use it in GitHub Desktop.
demeter_delegation_1.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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