Java proxy mode: static proxy VS dynamic proxy

1. Static proxy: To put it plainly, a static proxy means that the bytecode file of the proxy class already exists before the program runs. The relationship between the proxy class and the original class is determined before the program runs.

     The characteristics of static proxy: 1. The target object, that is, the proxy object must implement the interface; 2. The proxy object, must implement the same interface as the target object.

    //Interface
    public interface UserDAO {           void save();     }     //The target object is the proxy class     public class IUserDAO implements UserDAO {           public void save(){                 do something....            }      }     //The proxy class and the proxy class need Implement the same interface















    public class Proxy  implements  UserDAO {

        private UserDAO userDAO = new IUserDAO();
             public void save(){                   userDAO.save();//Execute the method of the target object                   do something....              }



    }


2. Dynamic proxy mode: The source code of the dynamic proxy class is dynamically generated during the running of the program through mechanisms such as JVM reflection, and the relationship between the proxy class and the delegate class is determined at runtime.

     

     // 接口
     public interface IUserDao {
           void save();
           void find();

     }


      //target

      class UserDao implements IUserDao{


      @Override
      public void save() {
           System.out.println("Simulation: save the user!");

      }


      @Override
       public void find() {
            System.out.println("查询");
      }

}


/**
* Dynamic proxy:
* The proxy factory, which generates proxy objects for multiple target objects!
*
*/
     class ProxyFactory {
         // Receive a target object
         private Object target;
         public ProxyFactory(Object target) {
               this.target = target;

         }


         // Return the proxy to the target object (proxy)
           public Object getProxyInstance() {
                  Object proxy = Proxy.newProxyInstance(
                  target.getClass().getClassLoader(), // The class loader used by the target object
                  target. getClass().getInterfaces(), // All interfaces implemented by the target object
                  new InvocationHandler() {// Trigger
                        @Override
                       public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                               / / Get the method name of the currently executed method
                              String methodName = method.getName();
                              // Method return value
                               Object result =;
                                if ("find".equals(methodName)) {
                                     // directly call the target object method
                                      result = method.invoke(target, args);
                                } else {
                                        System.out.println("Open transaction..." );
                                        // execute the target object method
                                         result = method.invoke(target, args);
                                          System.out.println("Submit transaction...");
                              }
                               return result;
                       }
                    }
                 );
               return proxy;
          }
   }


   public  static void main(String[] args) {

        //Create target object

        IUserDao    target = new  UserDao();

        System.out.println("Delegated object:" + target.getClass());

        //Create proxy object

        IUserDao    proxy = (IUserDao) new ProxyFactory.getProxyInstance();

        System.out.println("Proxy object:" + proxy.getClass());

        proxy.save();//Execute the method of the proxy class

  }

Guess you like

Origin blog.csdn.net/qq_37451441/article/details/80107330