Design Patterns notes eleven proxy mode

Agent mode (Proxy)

Basic introduction proxy mode

  1. Proxy Mode: Provides a substitute for an object to control access to this object. That is accessed by the target through a proxy object benefits of doing so are: On the basis of the target object can be achieved on additional features to enhance the operation of the expansion of the target object's function.
  2. The proxy object may be a remote object, creating a large overhead object or objects required security controls
  3. There are different forms of proxy mode, there are three main static agents, dynamic proxies (JDK proxy interface agent) and Cglib agent (you can create objects in dynamic memory without the need to implement the interface, he belongs to the category of dynamic proxies).
  4. Schematic proxy mode
    Here Insert Picture Description

Static agents

Basic introduction static code mode

When using static agent, need to be defined interface or superclass, the proxy object (i.e., target object) and implement the same interface proxy object inherits the same or a parent with

Applications

 specific requirements

  1. Define an interface: ITeacherDao
  2. TeacherDAO the target object implements the interface ITeacherDAO
  3. Use a static proxy mode, you need to also realize ITeacherDAO in the proxy object TeacherDAOProxy
  4. When the call to invoke the target object through the proxy object method call.
  5. Special note: proxy object and the target object to implement the same interface, and then call the target object by calling the same method as the method

 idea analysis scheme (FIG class)
Here Insert Picture Description

Advantages and disadvantages of the static agent

  1. Advantages: function without modification in the target object, the proxy object via the target function expansion
  2. Cons: Because the proxy object needs to be targeted to achieve the same interface, so there will be many proxy class
  3. Once the interface method for increasing the target object and proxy object must be maintained

Dynamic Proxy

Basic introduction of the dynamic proxy mode

  1. Proxy object, do not need to implement the interface, but the target object to implement the interface, or can not use dynamic proxies
  2. Generating a proxy object, the JDK using the API, the dynamic proxy object constructed in memory
  3. Dynamic agent also known as: JDK agent, interface agent

Generating a proxy object of the JDK API

  1. Acting classes where the package: java.lang.reflect.Proxy
  2. JDK achieve newProxyInstance agent only need to use the method, this method requires three parameters, the wording is complete:
    static Object newProxyInstance (<?> Loader ClassLoader, Class [] the interfaces, of InvocationHandler H)

Dynamic proxy application examples

Application Examples claim Ø
will improve the foregoing static agent into a dynamic proxy mode (i.e.: JDK proxy mode)
Ø idea scheme (FIG class)
Here Insert Picture Description

Cglib agent

Basic introduction Cglib agency model

  1. Static agents and JDK proxy mode requires the target object is to implement an interface, but sometimes the target object is just a single object, and does not implement any interfaces, this time can be used to achieve the target object subclasses agent - this is Cglib agent
  2. Cglib agent also known as sub-agent class, it is to build a sub-class object in memory in order to achieve the target object extensions, some of the books will also be attributed to the dynamic Cglib proxy agent.
  3. Cglib is a powerful, high-performance code generator package, it can extend and run java classes implement java interface, which is widely used in many AOP framework, e.g. AOP the Spring, implementation interception
  4. How to choose the AOP proxy mode in programming:
  1. Target needs to implement the interface, using JDK proxies
  2. Audiences do not need to implement the interface, with Cglib agent
  1. Cglib underlying packet is to convert bytecode bytecode processing frame and generate a new class ASM

Cglib proxy mode implementation steps

  1. Jar files need to introduce cglib
    Here Insert Picture Description
  2. Dynamically constructed in memory sub-class, pay attention to the agent class can not be final, otherwise an error java.lang.IllegalArgumentException:
  3. Object 'If final / static, then it will not be intercepted, that method does not perform additional business audience.

Cglib proxy mode application examples

Application Examples claim 
the foregoing case implemented Cglib Proxy mode
 idea scheme (FIG class)
Here Insert Picture Description

Several common proxy mode Introduction - Several variants

  1. Firewall agent
    within the network through a proxy through the firewall, enabling access to the public network.

  2. Caching proxy
    such as: When requesting image files and other resources, first caching proxy to take, if resources are to take ok, if not get the resources, then take the public network or database, then the cache.

  3. Remote Agent
    local representative of the remote object, it can by remote object when the local object to call. Remote Agent to communicate information through the network and the real remote object.

  4. Synchronization Agent: mainly used in multi-threaded programming, a complete multi-thread synchronization between the synchronization proxy: mainly used in multi-threaded programming, a complete synchronization between multiple threads

Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104834165