Share
Embed
Transcript
13 slides
-
Заместитель Паттерн проектирования, который представляет объект, который контролирует доступ к
другому объекту, перехватывая все вызовы. пятница, 23 ноября 12 г. -
Заместитель 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 пятница, 23 ноября 12 г. -
Заместитель account = BankAccount.new(100) account.deposit(50) account.withdraw(10) proxy = BankAccountProxy.new(account) proxy.deposit(50)
proxy.withdraw(10) пятница, 23 ноября 12 г. -
Заместитель 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 "Доступ запрещён: #{Etc.getlogin} не имеет прав доступа." end end end пятница, 23 ноября 12 г. -
Заместитель 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'=>'127550') пятница, 23 ноября 12 г. -
Заместитель 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 пятница, 23 ноября 12 г. -
Заместитель class VirtualAccountProxy def initialize(&creation_block) @creation_block = creation_block end #
... def subject @subject || (@subject = @creation_block.call) end end account = VirtualAccountProxy.new { BankAccount.new(10) } пятница, 23 ноября 12 г. -
Заместитель class AccountProxy def initialize(real_account) @subject = real_account end def
method_missing(name, *args) puts("Делегирую вызов #{name}.") @subject.send(name, *args) end end пятница, 23 ноября 12 г. -
Заместитель 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 "Доступ запрещён: #{Etc.getlogin} не имеет прав доступа." end end end пятница, 23 ноября 12 г. -
Заместитель 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 array = VirtualProxy.new { Array.new } array << 'Привет' пятница, 23 ноября 12 г. -
Заместитель Позволяет спрятать объект. Позволяет защитить объект. Может быть ленивым.
пятница, 23 ноября 12 г.