Resolve the application of proxy mode in Ruby design mode development, proxyruby

Source: Internet
Author: User

Resolve the application of proxy mode in Ruby design mode development, proxyruby

Proxy Mode
The Proxy mode is a structural design mode. It mainly solves the problems caused by direct access to objects. For example, the objects to be accessed are on a remote machine. In an object-oriented system, some objects are required for some reasons (for example, the object creation overhead is large, some operations require security control, or external access is required ), direct access brings a lot of trouble to the user or system structure. We can add an access layer for this object when accessing this object. For example:

 

For example, C and A are not on the same server. A needs to call C frequently. We can create A Proxy class Proxy on A and hand over the work of accessing C to Proxy. For, it is like directly accessing the C object. In the development of A, we can focus entirely on the implementation of the business.
GoF "Design Pattern" states that it provides a proxy for other objects to control access to this object.
Proxy mode structure:

Use the proxy mode to dynamically control the behavior of the target object in a transparent form.

Instance

class BankAccount  def deposit  p "store the money" end endclass Proxy  attr_accessor :bankAccount  def initialize bankAccount  @bankAccount = bankAccount end  def deposit  @bankAccount.deposit end end

Create a bank account class, and then create a proxy class. The proxy class aggregates the bank account class to provide the same behavior structure. For customers, the proxy class is a pseudo account class. When performing operations on the proxy class, in fact, it is actually doing operations on real banking classes.
Behavior Control:

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 end

In this way, we can add control code while calling the target object, but all of this is displayed to the customer in a transparent manner, just like calling the common BankAccount object method.

bankAccount = BankAccount.newbankAccount.depositproxy = Proxy.new bankAccountproxy.deposit

Key points of the Proxy mode:
1. "adding an indirect layer" is a common solution to many problems in the software system. In an object-oriented system, using some objects directly brings about many problems. As the proxy object of the indirect layer, it is a common method to solve this problem.
In our daily work, we often use the proxy mode. For example, for the DAL data access layer in a three-tier structure or N-tiers structure, it encapsulates the database access. The BLL business layer developers only call the methods in the DAL to obtain data.
For example, I looked at the AOP and Remoting documents some time ago. For access across application domains, I need to provide a TransparentProxy (transparent proxy) for the client application ), the client program actually accesses the actual type object by accessing this proxy.
2. The implementation methods and implementation granularity of specific proxy design patterns vary greatly. Some may control individual objects in fine granularity, and some may provide abstract proxy layers for component modules, proxy the object at the architecture level.
3. proxy does not necessarily need to maintain interface consistency. As long as indirect control is implemented, sometimes loss and some transparency are acceptable. For example, in the example above, the proxy type ProxyClass and the proxy type LongDistanceClass do not need to inherit from the same interface, as described in GoF design patterns: provides a proxy for other objects to control access to this object. The proxy type can also control the access of the proxy type.

 

Articles you may be interested in:
  • Example Analysis of Observer Pattern in Ruby programming in Design Pattern
  • Instance parsing Implementation of observer mode in Ruby design mode development
  • In-depth analysis of the usage of the command mode in Ruby Design Mode Programming
  • Application instance analysis of appearance mode in Ruby Design Mode Programming
  • Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
  • The template method in the design mode applies two examples in Ruby.
  • Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
  • The example explains how Ruby uses the decorator mode in the design mode.
  • Example of using Builder mode in Ruby Design Mode Programming
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses code instances in the proxy mode and decoration mode in the Design Mode
  • Ruby uses the simple factory mode and factory method mode in the Design Mode

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.