Static and dynamic java proxy agents (learning example)

1. Interface

        public interface Channel {
            void send();
        }

2. The implementation class (may be of various different implementations)

        public class ChannelImpl implements Channel {
            @Override
            public void send() {
                System.out.println("send Message");
            }
        }

3. Static Agent: Agent class

        public class ChannelProxy implements Channel{
            //代理对象
            Channel channel;
            //传入代理对象
            public ChannelProxy(Channel channel){
                this.channel = channel;
            }
            @Override
            public void send() {
                System.out.println("start send message");
                channel.send();
                System.out.println("end send message");
            }
        }

3. Dynamic Agent: Agent class

        / ** Create agent implementation of InvocationHandler * / 
        public  class the MyHandler the implements of InvocationHandler {
             / ** 
             * target object 
             * / 
            Object target; 

            public the MyHandler (Object target) {
                 the this .target = target; 
            } 
            @Override 
            public Object Invoke (Object Proxy, Method, method, Object [] args) throws the Throwable { 
                System.out.println ( "Send Message Start" ); 
                System.out.println ( "call target object:" + target.getClass () + " , the target method:" + method .getName ());
                Object result = method.invoke(target);
                System.out.println("end send message");
                return result;
            }
        }

4. Call

        public class ProxyTest {
            public static void main(String [] args){
                System.out.println("=================静态代理===================");
                Channel channel = new ChannelImpl();
                ChannelProxy channelProxy = new ChannelProxy(channel);
                channelProxy.send();

                System.out.println("=================动态代理===================");
                MyHandler myHandler = new MyHandler(channel);
                Channel target = (Channel)(Proxy.newProxyInstance(channel.getClass().getClassLoader(),channel.getClass().getInterfaces(), myHandler));
                target.send();
            }
        }

5. Results

        ================= static proxy =================== 
        Start the Message the send 
        the send the Message 
        End the send the Message
         ==== ============= dynamic proxy =================== 
        Start the send the Message 
        calls the target object: class proxy.ChannelImpl, objective method: send 
        the Message the send 
        End the send the Message

Compare:

Static proxy:
proxy class do not care about which specific implementation class and how to implement the send method, only when you can call the proxy class, but the decoupling agent class and implementation class needs to implement all of the interface, if the interface is added, the agent class and implementation class. need to be added, the code redundancy and change agent of the proxy class only one interface, if you need other proxy interfaces, you need to create a new proxy class
dynamic proxy:
the static proxy basis, in addition to the static agent redundancy without implement the specified interface, any interface can be an agent, if there are other methods such as Channel interfaces create () method, invoke the MyHandler the same will be processed, so when the interface method is relatively long time, we need a method to the interface unified process, using dynamic process more convenient, more reusability

Guess you like

Origin www.cnblogs.com/lantuanqing/p/11302811.html