Created
June 12, 2011 12:41
-
-
Save monzou/1021506 to your computer and use it in GitHub Desktop.
lazy proxy
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
class VirtualAccountProxy | |
def initialize(&block) | |
@block = block | |
end | |
def deposit(amount) | |
return subject.deposit(amount) | |
end | |
def subject | |
@subject || (@subject = @block.call) | |
end | |
end | |
class BankAccount | |
def initialize | |
puts "initialize BankAccount" | |
end | |
def deposit(amount) | |
puts "deposit #{amount}" | |
end | |
end | |
proxy = VirtualAccountProxy.new{ BankAccount.new } | |
puts "initialize Proxy" | |
proxy.deposit(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment