zoukankan      html  css  js  c++  java
  • jdk-动态代理

    1.HelloWorld

    package reflect.proxy;
    
    public interface HelloWorld {
        void print();
    }

    2.HelloWorldImpl

    package reflect.proxy;
    
    public class HelloWorldImpl implements HelloWorld{
    
        @Override
        public void print() {
            // TODO Auto-generated method stub
            System.out.println("hello world!");
        }
        
    }

    3.HelloWorldInvocationHandle

    package reflect.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class HelloWorldInvocationHandle implements InvocationHandler{
        private HelloWorld helloWorld;
        public HelloWorldInvocationHandle(HelloWorld helloWorld) {
            this.helloWorld = helloWorld;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("动态代理-begin");
            Object result = method.invoke(helloWorld, args);
            System.out.println("动态代理-end");
            return result;
        }
    
    }

    4.HelloWorldProxyFactory

    package reflect.proxy;
    
    import java.lang.reflect.Proxy;
    
    public class HelloWorldProxyFactory {
        public static HelloWorld getProxy(HelloWorld helloWorld) {
            HelloWorldInvocationHandle handle = new HelloWorldInvocationHandle(helloWorld);
            Object obj = Proxy.newProxyInstance(helloWorld.getClass().getClassLoader(), new Class[]{HelloWorld.class}, handle);
            return (HelloWorld)obj;
        }
        
        public static void main(String[] args) {
            HelloWorld helloWorld = new HelloWorldImpl();
            HelloWorld proxy = HelloWorldProxyFactory.getProxy(helloWorld);
            System.out.println(proxy.getClass().getName());
            proxy.print();
        }
    }
  • 相关阅读:
    实体类、边界类和控制类
    面向对象分析和面向对象设计的区别
    面向对象分析与设计的步骤
    用例图:从用户角度描述系统功能,并指各功能的操作者
    面向对象分析和设计(OOA/D)
    在UML系统开发中有三个主要的模型
    UML建模之活动图介绍(Activity Diagram)
    活动图本质上就是流程图
    流程图
    流程、业务与事务
  • 原文地址:https://www.cnblogs.com/jerry19890622/p/3287326.html
Copyright © 2011-2022 走看看