--------of Java Design patterns > "proxy mode"

Source: Internet
Author: User
Tags throwable

01. What is proxy mode?

  Resolution: Proxy: The primary role of proxy mode is to provide a proxy for other objects to control access to this object. In some cases, an object does not want or cannot directly reference another object.

The proxy object can act as an intermediary between the client and the target object. The idea of proxy mode is to provide additional processing or different operations to insert a proxy object between the actual object and the caller.

These additional operations typically require communication with the actual object.

02. What is the composition of the agent?

  Parsing: Consists of three parts, namely: Abstract object (Subject interface), real object (Realsubject implements Subject interface), proxy object (Proxysubject)

  03. Proxy Object Action scenario?

Analytical:

   first , a remote proxy , which is a local representation of an object in a different address space. This hides the fact that an object exists in a different address space [DP]

Second : the need to fill the agent , is to create a very expensive object as needed. It is used to store the real object that takes a long time to instantiate [DP]. This allows for optimal performance.

04. How many agents are there?

Resolution: There are two types of agents, dynamic agents and static agents,

Dynamic proxies: Dynamic proxies, which use dynamic proxies in struts and spring frameworks to help programmers generate subclasses or classes of classes they want to use, so that the framework can do some action before the code,

such as pre-method enhancement, post-method enhancement, logging, exception capture and so on. dynamically created using the reflection mechanism while the program is running

(interpolation: AOP (aspectorientedprogramming): The logging, performance statistics, security control, transaction processing, exception handling and other code from the business logic code, through the separation of these behaviors ,

we want to be able to separate them into non-instructional methods of business logic, and then change these behaviors without affecting the code---Decoupling of business logic . )

Static Proxy: The programmer creates a proxy class or a specific tool to automatically generate the source code and then compile it. The. class file for the proxy class already exists before the program runs.

Dynamic proxy VS static proxy:

Advantages and disadvantages of static proxy classes

Advantages:

The proxy makes the client not need to know what the implementation class is, how to do it, and the client only needs to know the proxy (decoupling)

Disadvantages:

1) the proxy class and the delegate class implement the same interface, and the proxy class implements the same method through the delegate class. There is a lot of code duplication. If the interface adds a method,

In addition to implementing this method for all implementation classes, all proxy classes also need to implement this method. Increases the complexity of code maintenance.

2) The proxy object serves only one type of object if it is to serve multiple types of objects. It is bound to be a proxy for each object, static agent in the program is a little larger than the capacity of the.

The code above provides a proxy for access to the Usermanager class only, but if you also want to provide proxies for other classes such as the Department class, we need to add the proxy again Department 's proxy class.

 Static proxy----------> Case List

01. Create an interface:

Package cn.work.proxy;//Abstract Object public interface Subject {public String request ();}

02. Create an implementation class:

Package cn.work.proxy;//Real Object public class Realsubject implements Subject {public String request () {return "real object";}}

03. Create a Proxy object:

Package Cn.work.proxy;public class Proxysubject {//The fields that make up an abstract object to a proxy object private Subject sub;public Subject getsub () {return sub ;} public void Setsub (Subject sub) {this.sub = sub;} Public String reqstring () {System.out.println ("proxy enhancement"); return Sub.request ();}}

04. Create Test class:

Object Agent @testpublic void test09 () {Subject sub=new realsubject (); Proxysubject ps=new proxysubject ();p s.setsub (sub); System.out.println (Ps.reqstring ());}

  

Dynamic Agent Benefits:

  The biggest benefit of dynamic proxies compared to static proxies is that all methods declared in the interface are transferred to the calling processor in a centralized method (Invocationhandler.invoke). Such

When the number of interface methods is relatively high, we can handle it flexibly without having to relay every method like a static proxy. And the application of dynamic agents makes our class responsibilities more unitary and more reusable.

  

Dynamic Agent---------> instances

01. Create a DAO interface

Package Cn.work.dao;import Cn.work.entity.user;public interface Userdao {public void Add (user user);

02. Creating an implementation of a DAO interface

Package Cn.work.dao;import Cn.work.entity.user;public class Userimpl implements Userdao{public void Add (user user) { System.out.println ("Sava success");}}

03. Create a Test class

@Testpublic void Test10 () {User user=new User (); final Userdao userdao=new Userimpl (); Userdao u = (userdao) proxy.newproxyinstance (Userdao.getclass (). getClassLoader (), Userdao.getclass (). GetInterfaces ( ), new Invocationhandler () {public Object invoke (Object Proxy, Method method, object[] args) throws Throwable {SYSTEM.OUT.P RINTLN ("code Enhancement"); return Method.invoke (Userdao, args);}} ); U.add (user);}

Parameter description:

Proxy.newproxyinstance (loader, interfaces, h)

Loader: Class loader, passing in the object you want to proxy, through the object name. GetClass (). getClassLoader () to get the class loader for the object.

Interfaces: A collection of all implemented interfaces of the Proxied object

  Invocationhandler is the interface that is implemented by the calling handler for the proxy instance
Implement Invcationhandler's Mi Ming inner class,
New Invcationhandler () {
    Public object Invoke (Object proxy, Method method, object[] args) throws Throwable {
..... return ....;}
}

--------of Java Design patterns > "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.