When we meet these requirements:
1) Controlling access to an object
2) providing a location-independent way of getting at the object
3) delaying its creation
all three actually have a common solution: the Proxy pattern.
Code First:
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
the proxy object can treated as couterfeit object:
account = BankAccount.new(100)
account.deposit(50)
account.withdraw(10)
proxy = BankAccountProxy.new(account)
proxy.deposit(50)
proxy.withdraw(10)
The BankAccountProxy doesn't know much about the finance, he just send request to target object.
If we consider the access security issue, the folowing code will answer:
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 balance
check_access
return @subject.balance
end
def check_access
if Etc.getlogin != @owner_name
raise "Illegal access: #{Etc.getlogin} cannot access account."
end
end
end
The advantage of using a proxy for protection is that it gives us a nice separation of concerns: The proxy worries about who is or is not allowed to do what.
Protection proxies have another advantage over the naive “implement all of the security and functionality in one class” approach.
If we consider the remote proxy:
we can use the Ruby soap client for example:
require 'soap/wsdlDriver'
wsdl_url = 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'
proxy = SOAP::WSDLDriverFactory.new( wsdl_url ).create_rpc_driver
weather_info = proxy.GetWeatherByZipCode('ZipCode'=>'19128')
Once the proxy object is set up, the client code no longer has to worry about the fact that the service actually lives at www.webservicex.net. Instead, it simply calls GetWeatherByZipCode and leaves all of the network details to the proxy.
If we consider the lazy creating issue:
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
The lazy creating is really simple, we can create by proc passing from the contrusctor:
class VirtualAccountProxy
def initialize(&creation_block)
@creation_block = creation_block
end
# Other methods omitted ...
def subject
@subject || (@subject = @creation_block.call)
end
end
account = VirtualAccountProxy.new { BankAccount.new(10) }
The delegate by method_missing:
If you can sth.some_method if some_method is missing in sth instance, then it will climb up to its super classes to find it, if still not found, it will call the sth.method_missing, if sth.method_missing not found, it will call up to super classes till the Object class(Object has the method) it will raise a NoMethodError exception.
So we can eliminate the direct proxy method
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def method_missing(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
class VirtualProxy
def initialize(&creation_block)
@creation_block = creation_block
end
def method_missing(name, *args)
s = subject
s.send( name, *args)
end
def subject
@subject = @creation_block.call unless @subject
@subject
end
end
分享到:
相关推荐
Design Patterns in Ruby, Addison-wesley (2008). The Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform ...
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
Design Patterns in Ruby Dec 2007.rar
Design Patterns in Modern C++.pdf
design pattern in C# language
Introduction to Design Patterns in C With Qt4 Alan Ezust
Design Patterns in Modern C++,Design Patterns in Modern C++,Design Patterns in Modern C++
Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...
An introduction to Design Patterns in C++ with Qt 2nd Edition 中文版 C++ qt 设计模式 第二版, 中英文版 合集 Master C++ “The Qt Way” with Modern Design Patterns and Efficient Reuse This fully ...
Implement design patterns in .NET using the latest versions of the C# and F# languages. This book provides a comprehensive overview of the field of design patterns as they are used in today’s ...
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition.pdf
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Design Patterns in .NET .net设计模式 2019年最新版
and applications that run on various software and hardware platforms with little or no change in the underlying codebase, while still being a native application with native capabilities and speed....