Created
June 30, 2012 04:23
-
-
Save gja/3022255 to your computer and use it in GitHub Desktop.
Proxy Pattern blog post
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 Bank | |
def bank_balance(account_number) | |
42 | |
end | |
end | |
bank = Bank.new | |
p bank.bank_balance("1234567") # ---> Gives me 42 |
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 LogBankBalanceFilter | |
def initialize(bank) | |
@bank = bank | |
end | |
def bank_balance(account_number) | |
p "#{account_number} is checking balance" | |
balance = @bank.bank_balance(account_number) | |
p "the balance was #{balance}" | |
return balance | |
end | |
end | |
bank = LogBankBalanceFilter.new(Bank.new) | |
p bank.bank_balance("1234567") # --> "123456 is checking balance", "the balance was 42", 42 |
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 DoubleBankBalanceFilter | |
def initialize(bank) | |
@bank = bank | |
end | |
def bank_balance(account_number) | |
2 * @bank.bank_balance(account_number) | |
end | |
end | |
bank = DoubleBankBalanceFilter.new(LogBankBalanceFilter.new(Bank.new)) | |
p bank.bank_balance("1234567") # --> "123456 is checking balance", "the balance was 42", 84 |
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
bank = LogBankBalanceFilter.new(DoubleBankBalanceFilter.new(Bank.new)) | |
p bank.bank_balance("1234567") # --> "123456 is checking balance", "the balance was 84", 84 |
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 CachingFilter | |
def initialize(bank) | |
@bank = bank | |
@cache = {} | |
end | |
def bank_balance(account_number) | |
if @cache[account_number].nil? | |
@cache[account_number] = @bank.bank_balance(account_number) | |
end | |
@cache[account_number] | |
end | |
end | |
bank = LogBankBalanceFilter.new(DoubleBankBalanceFilter.new(CachingFilter.new(Bank.new))) | |
p bank.bank_balance("1234567") # --> "123456 is checking balance", "the balance was 84", 84,. The second call will be cached |
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 Filter | |
def initialize(obj) | |
@obj = obj | |
end | |
def method_missing(sym, *args) | |
@obj.send(sym, *args) | |
end | |
end |
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 FooRackMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, body = @app.call env | |
[status, headers, body] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment