Created
January 1, 2015 01:54
-
-
Save meaganewaller/de3081a9e27125ffef1b to your computer and use it in GitHub Desktop.
Proxy Pattern
This file contains 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
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 |
This file contains 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
# 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 |
This file contains 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 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 |
This file contains 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 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 |
This file contains 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
account = BankAccount.new(100) | |
account.deposit(50) | |
account.withdraw(10) | |
proxy = BankAccountProxy.new(account) | |
proxy.deposit(50) | |
proxy.withdraw(10) |
This file contains 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(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