Code reference (in more detail, the code is annotated here for blog brevity) github.com/zhang-xiaox…

1 Static Proxy (Interface Proxy)

Demand (doing) : business processes of singing star agent, and stars to sing songs or left to star, the process took the singing is divided into two parts, 1: singer while singing, 2: agency is responsible for the char (interview, sign a contract, booking tickets, money), for the proxy singer, he liberated, equivalent to enhanced, not their interview, sign a contract Yao of, to acting company, I dozen miscellaneous went, earn point difference, sing not good, have to ask a singer (can’t whole acting), each act its own duty.

Figure out what needs to be done in all processes, and use interfaces to figure it out.

package com.demo.proxy.staticproxy; / * * * Star: proxy interface, this specifies the entire business need to do * (beginning everything interface, programming to an interface, first clear what to do, how to do the implementation class is behind, which facilitates decoupling, * also is the realization of the interview, for example, need to change the way that is not also have to modify the proxy class interview? This is directly defined as an interface, so the agent to change the agent only need to change the agent implementation class * does not need to modify the agent interface, to achieve the open and closed principle, open expansion, but modify the interface this agent interface Star is not allowed) * @author zhangxiaoxiang * @date 2019/8/9 */ public interface Star {/** ** ** ** ** */ void confer(); /** * void signContract(); /** */ void bookTicket(); /** * sing */ void sing(); /** * collectMoney(); }Copy the code

Know what to do (the above interface is clear), but how to do (implementation class), first a singer to open a solo concert needs to implement all the interface, and be both father and mother

//package com.demo.proxy.staticproxy; /** * RealStar: a real singer, in the absence of agency needs to achieve all, both father and mother * (obviously not unreasonable, for the singer, in addition to singing, other things, interviews, signing contracts are not necessary to do) ** @author Zhangxiaoxiang * @date 2019/8/9 */ public class RealStar implements Star {** ** public void confer() {/** * public void confer() System.out.println(" real object perform interview ===> realstar.confer ()"); } public void signContract() {system.out.println (" realstar.confer ()"); } public void confer() {system.out.println (" real object confer ===> realstar.confer ()"); } @override public void sing() {// Override public void sing() {// Override public void sing() System.out.println(" real object (Jay) perform singing ===> realstar.confer ()"); } public void confer() {system.out.println (" real object confer ===> realstar.confer ()"); }}Copy the code

As an angel, I will certainly come to rescue you. And I will not rob your singing glory. You sing your song, AND I will do some chores for you to earn some price difference

//package com.demo.proxy.staticproxy; * @author Zhangxiaoxiang * @date 2019/8/9 */ public class ProxyStar implements Star {/ * * * teach professional person to do professional things, such as here is equivalent to the singer sing * please note that this interface properties, composite properties than the implementation class (application) do better, because the interface properties can please many singers singing (withdrawal polymorphism), * But is the implementation class to do attributes (implementation oriented programming) coupling degree is too high, change the singer has to modify this class, open and close principle is to try not to modify this class, so here * is to use Star, rather than private RealStar Star * - static proxy is the interface is from here */ private Star star; /** * how can I help you? @param star */ public ProxyStar(star star) {this.star = star; } /** * -- to confer with a professional, to confer with a professional, to confer with a professional, to confer with a professional, to confer with a professional; System.out.println(" proxy interview ===> proxystar.bookticket ()"); } / / Override public void signContract() {Override public void signContract() { System.out.println(" proxy sign contract ===> proxystar.bookticket ()"); } public void bookTicket() {Override public void bookTicket() { System.out.println(" agent booking ===> proxystar.bookticket ()"); } / * * * sing = = = professional people do professional thing * / @ Override public void sing () {System. Out. Println (" * * * * * * * proxy objects don't rob hero aura * * * * * * * "); star.sing(); Public void collectMoney() {Override public void collectMoney() {Override public void collectMoney() { System.out.println(" proxystar.bookticket ()"); system.out.println (" proxystar.bookticket ()"); }}Copy the code

The cowhide is blown out, test it out, happy cooperation next time continue yo, dear, remember to give five star praise yo

package com.demo.proxy.staticproxy; * @author zhangxiaoxiang * @date 2019/8/9 */ public class Client {public static void The main (String [] args) {System. Out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- test static agent -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); Star real = new RealStar(); Star proxy = new ProxyStar(real); proxy.confer(); proxy.signContract(); proxy.bookTicket(); Proxy.sing (); // Perform the following operations: // Perform the following operations: // perform the following operations: // perform the following operations: // perform the following operations: proxy.collectMoney(); }}Copy the code

Review of the concert is as follows:

2 Dynamic proxy (proxy object) Star interface is the same interface (static proxy above),RealStar interface is the same interface (static proxy above)

 

An implementation class that calls the handler to the JDKCopy the code
//package com.demo.proxy.dynamicproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * StarHandler: This is an implementation class that calls the JDK handler for a proxied class for which the JDK generates a proxied object * @author zhangxiaoxiang * @date 2019/8/9 */ public ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * By the jury StarHandler to evaluate the first agent */ Star realStar; @realStar */ public StarHandler(realStar) {this.realStar = realStar; } /** * override JDK proxy methods (using reflection to generate response objects) * @param proxy evaluated qualified proxy objects * @param method method (real object needs to perform operations, sing) * @param args * @return  * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// Agent Object that has the proxy qualification after evaluation Object Object = NULL; System.out.println(" Real method before execution!" ); System.out.println(" interview, sign contract, advance payment, book air ticket "); If (method.getName().equals("sing")){object = method.invoke(realStar, args); } system.out.println (" Real method after execution!" ); System.out.println(" end "); return object; }}Copy the code
Classes that simulate dynamically generated proxiesCopy the code
//package com.demo.proxy.dynamicproxy; /** * public class ProxyStar implements Star {/** * public class ProxyStar implements Star {/** * public class ProxyStar implements Star {/** * */ StarHandler handler; */ StarHandler handler; */ StarHandler handler; */ StarHandler handler; */ StarHandler handler; /** @param handler */ public ProxyStar(StarHandler handler) {this.handler = handler; } /** * Override public void confer() {// handler.invoke(this, args); } @override public void signContract() {// handler.invoke(this, args); } @override public void bookTicket() {// handler.invoke(this, args); } @override public void sing() {// handler.invoke(this, args); } @override public void collectMoney() {// handler.invoke(this, args); }}Copy the code

Client testing

package com.demo.proxy.dynamicproxy; import java.lang.reflect.Proxy; @author zhangxiaoxiang * @date 2019/8/9 */ public class Client {public static void Main (String [] args) {System. Out. Println (" -- -- -- -- -- -- -- -- -- -- the test of dynamic proxy -- -- -- -- -- -- -- -- -- -- - "); Star realStar = new RealStar(); // Proxy class StarHandler handler = new StarHandler(realStar); / / proxy object generated by the JDK Star proxy = (Star) proxy. NewProxyInstance (this) getSystemClassLoader (), new Class [] {Star. Class}. handler); proxy.sing(); }}Copy the code

Dynamic proxy for the concert accident scene

3: additional agent, please refer to www.cnblogs.com/boboxing/p/…

 

Conclusion :AOP is a model for faceted programming, and will use it sooner or later.

Links: blog.csdn.net/Goskalrie/a…