Skip to content

Instantly share code, notes, and snippets.

@meaganewaller
Created January 1, 2015 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meaganewaller/de3081a9e27125ffef1b to your computer and use it in GitHub Desktop.
Save meaganewaller/de3081a9e27125ffef1b to your computer and use it in GitHub Desktop.
Proxy Pattern
require 'etc'
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def deposit(amount)
check_access
return @subject.deposit(amount)
end
def withdraw(amount)
check_access
return @subject.withdraw(amount)
end
def check_access
if Etc.getlogin != @owner_name
raise "Illegal access: #{Etc.getlogin} cannot access account."
end
end
end
# Protection Proxy
require './account_protection_proxy'
protection_proxy = AccountProtectionProxy.new(account)
protection_proxy.deposit(100)
protection_proxy.withdraw(25)
puts protection_proxy.balance
# account_protection_proxy.rb
require 'etc'
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def method_mssing(name, *args)
check_access
@subject.send(name, *args)
end
def check_access
if Etc.getlogin != @owner_name
raise "Illegal access: #{Etc.getlogin} cannot access account"
end
end
end
# account_proxy
class AccountProxy
def initialize(real_account)
@subject = real_account
end
def method_missing(name, *args)
puts "Delegating #{name} message to subject."
@subject.send(name, *args)
end
end
#virtual_proxy
class VirtualProxy
def initialize(&creation_block)
@creation_block = creation_block
end
def method_missing(name, *args)
subject.send(name, *args)
end
def to_s
subject.to_s
end
def subject
@subject ||= @creation_block.call
end
end
# main
require '../bank_account'
require './account_proxy'
require './account_protection_proxy'
proxy = AccountProxy.new(BankAccount.new(100))
proxy.deposit(25)
proxy.withdraw(50)
puts "account balance is now: #{proxy.balance}"
# Generic virtual proxy
require './virtual_proxy'
array = VirtualProxy.new { Array.new }
array.push('hello')
array.push('out')
array.push('there')
puts array.to_s
class BankAccount
attr_reader :balance
def initialize(starting_balance=0)
@balance = starting_balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount
end
end
class BankAccountProxy
def initialize(real_object)
@real_object = real_object
end
def balance
@real_object.balance
end
def deposit(amount)
@real_object.deposit(amount)
end
def withdraw(amount)
@real_object.withdraw(amount)
end
end
account = BankAccount.new(100)
account.deposit(50)
account.withdraw(10)
proxy = BankAccountProxy.new(account)
proxy.deposit(50)
proxy.withdraw(10)
class VirtualAccountProxy
def initialize(starting_balance=0)
@starting_balance = starting_balance
end
def deposit(amount)
s = subject
return s.deposit(amount)
end
def withdraw(amount)
s = subject
return s.withdraw(amount)
end
def balance
s = subject
return s.balance
end
def subject
@subject || (@subject = BankAccount.new(@starting_balance))
end
end
# Virtual Proxy
require './virtual_account_proxy'
virtual_proxy = VirtualAccountProxy.new { BankAccount.new(25) }
virtual_proxy.deposit(10)
virtual_proxy.withdraw(20)
puts virtual_proxy.balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment