Skip to content

Instantly share code, notes, and snippets.

@monzou
Created June 12, 2011 12:41
Show Gist options
  • Save monzou/1021506 to your computer and use it in GitHub Desktop.
Save monzou/1021506 to your computer and use it in GitHub Desktop.
lazy proxy
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