Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Паттерны проектирования 4.2

Паттерны проектирования 4.2

Паттерн Заместитель (Прокси).

Vitaly Shlyaga

November 23, 2012
Tweet

More Decks by Vitaly Shlyaga

Other Decks in Education

Transcript

  1. Заместитель Паттерн проектирования, который представляет объект, который контролирует доступ к

    другому объекту, перехватывая все вызовы. пятница, 23 ноября 12 г.
  2. Заместитель 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 г.
  3. Заместитель 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 г.
  4. Заместитель 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 г.
  5. Заместитель 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 г.
  6. Заместитель 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 г.
  7. Заместитель 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 г.
  8. Заместитель 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 г.
  9. Заместитель 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 г.