The classical understanding of static proxy mode of GOF23 proxy mode

Source: Internet
Author: User


The static proxy mode of proxy mode in design mode
Proxy mode (Proxypattern)
Core role:
Control access to objects through a proxy.
The method of accessing a (certain class of) object can be controlled by detailed control, pre-processing is done before calling this method, and the method is called after processing. (That is, the micro implementation of AOP )
  AOP (plane-oriented programming.) aspectoriented programming) The core implementation of the mechanism
Give an example to understand this pattern. For example, Star, my idol Jay sing this kind of thing. If a client wants Jay to sing. Of course, Jay as a Star class object, the interior can have
  1. Interview
  2. Contract Drafting
  3. Signature, prepayment
  4. Arranging air tickets and vehicles
  5. Sing
  6. Closure
Of course, this will also achieve customer demand. But we can see how tired Jay will be. And there are so many things in the process that you don't need to worry about. For example, interview, contract drafting and so on, Jay only need to care about is singing. We certainly don't allow Jay to be distracted by other chores, but everything else matters, and it's the process of sitting every concert. So there is no shortage.
Thus, there is the concept of brokers.
If, at the moment, a user wants to invite Jay to sing. All he needs to do is find Jay's agent to get the agent to interview, contract drafting, closing payments and so on. And in the customer requests the agent sings, in the concrete realization process, by the agent request Jay to sing. Of course, this structure is very convenient, because if this broker also has other stars, only need to modify the broker (agent) of the inside of the singing method can be.
Proxy mode (Proxypattern)
Core roles:
  • Abstract roles  define the proxy role and the public external method that is really a role. In other words, the proxy role and the real role have the same set of methods from the abstract role
  • Real roles  implement abstract roles, define the business logic to be implemented by real roles ( such as singing, Jay singing, and agents not singing ), for use  by agent roles to focus on real business logic (singing)
  • Proxy role:  The implementation of abstract roles, is the real role of the broker (broker), through the real role of the business logic (in other words, the proxy role needs to have a real role in the member variables to access the real role of business logic (such as singing) to implement abstract methods. And you can attach your own actions.  put unified Process control into the agent role for processing
Application Scenarios:
Security agents: Masking access to real-world roles
Remote Proxy: Handles remote method call (RMI)  lazy loading through the proxy class: Load the Lightweight proxy object first, and really need to load the real object again.
For example, you want to develop a large document viewing software, large documents have a large number of pictures, it is possible that a picture has 100M, when opening a file, it is impossible to display all the pictures, so that you can use the proxy mode, when you need to view the image, use proxy Make a big picture open.
Classification:
Static proxies (statically defined proxy classes), i.e. we implement them ourselves.
Dynamic proxy (dynamically generated proxy class): We do not need to customize, use the default
  • JDK self-brought dynamic agent
  • javaassist Byte Code operation library implementation
  • CGLIB
  • ASM ( low -level usage directives, poor maintainability ) 

The classical understanding of static proxy mode of GOF23 proxy mode

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.