Application of analytic proxy proxy mode in the development of Ruby design pattern _ruby Special Topics

Source: Internet
Author: User
Tags require

Agent mode
Proxy proxy mode is a structural design pattern, the main problem is: The problem of direct access to the object, such as: the object to be accessed on the remote machine. In object-oriented systems, there are some objects that for some reason (such as object creation overhead, or some operations require security control, or require out-of-process access), direct access can cause a lot of trouble for the user or system structure, and we can add a layer of access to this object when we access it. The following figure:

For example, C and a are not on a server, a to frequently call C, we can do a proxy class proxy, the access to C of the work to proxy, so for a, it is like a direct access to the object of C. In the development of a we can focus entirely on the implementation of the business.
GoF "design pattern" says: Provides a proxy for other objects to control access to this object.
Structure of the proxy pattern:

To control the behavior of target object dynamically in the form of customer transparency through agent mode

Instance

Class BankAccount
 
 def deposit
  P ' Store the money '
 end

class Proxy
 
 attr_accessor: BankAccount
 
 def initialize bankAccount
  @bankAccount = BankAccount
 end
 
 def deposit
  @ Bankaccount.deposit
 End
 


Create a bank account class, and then create a proxy class, proxy class aggregation bank account class, provide the same behavior structure, to the customer agent class is a pseudo account class, to do the operation of the proxy class, in fact, is in the real bank operations.
To be controlled by behavior:

Class Proxy
 
 attr_accessor:bankaccount
 
 def initialize bankAccount
  @bankAccount = BankAccount
 End
 
 def deposit
  check_something
  @bankAccount. Deposit
 End
 
 def check_something
  #do some Checking code
 End
 

This allows us to increase the control code while invoking the target object, but all of this is presented to the customer in a transparent manner, as is the case with calling the normal BankAccount object method

BankAccount = bankaccount.new
bankaccount.deposit
proxy = proxy.new BankAccount
proxy.deposit

Main points of proxy mode:
1, "adding a layer of indirect layer" is a common solution to many responsible problems in software systems. In the object-oriented system, the direct use of some objects will bring many problems, as the indirect layer of proxy object is a common means to solve this problem.
In our day-to-day work is also often used in proxy mode, such as for the three-tier structure or n-tiers structure of the DAL data access layer, it encapsulates access to the database. The BLL business layer developer simply invokes the method in the Dal to get the data.
For example, some time ago looked at the AOP and remoting aspects of the information, for access across the application domain, to provide a transparentproxy for the client application (transparent proxy), the client program is actually access to this agent to access the actual type object.
2, the implementation of the specific proxy design mode, the implementation of granularity are very different, some may be a single object for fine-grained control, some may provide the Component Module Abstract Agent layer, at the architecture level to the object proxy.
3, proxy does not necessarily require the consistency of the interface, as long as the realization of indirect control, sometimes the loss of some transparency is acceptable. For example, the example above, the proxy type Proxyclass and the proxy type Longdistanceclass can not inherit from the same interface, as described in Gof design pattern, which provides a proxy for other objects to control access to this object. The proxy type can also play a role in controlling access to the proxy type in some ways.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.