• 静态代理,动态代理,Cglib代理详解


    一.静态代理

    新建一个接口 定义一个玩家方法:

    package com."".proxy.staticc;
    
    public interface Iplayer {
    public void play();
    }
    

      建一个你自己玩游戏的类

    package com."".proxy.staticc;
    
    public class YourPlayer implements Iplayer {
    
    	public void play() {
    		System.out.println("登录");
    		System.out.println("刷图中======");
    		System.out.println("刷图中======");
    		System.out.println("刷图中======");
    		System.out.println("退出");
    		
    	}
    
    }
    

      建立代理类

    package com."".proxy.staticc;
    
    public class DaLiPlayer implements Iplayer{
    private  Iplayer player = new YourPlayer();
    
    public DaLiPlayer(Iplayer player){
        this.player = player;
    }
        
        
        //环绕通知
        public void play() {
            System.out.println("代练");//前置通知
            player.play();
            System.out.println("还号");//后置通知
        }
    
    }

    开始代理玩游戏

    package com."".proxy.staticc;
    
    public class Client {
    public static void main(String[] args) {
    	//传递玩家账号
    	YourPlayer yourPlayer = new YourPlayer();
    	
    	//找到代理人
    	DaLiPlayer daLiPlayer = new DaLiPlayer(yourPlayer);
    	
    	//开始玩游戏
    	daLiPlayer.play();
    }
    }
    

      

    二.动态代理

    动态代理的方法和静态比较类似 只是可以动态的代理目标 这里展示下具体代码

    package com."".jdkproxy;
    
    public interface Iplayer {
    	
    	String name = "123";
    
    	public abstract void play();
    	
    }
    

      

    package com."".jdkproxy;
    
    public class DnfPlayer implements Iplayer {
    
    	public void play() {
    		
    		System.out.println("我只玩DNF,把你们负责人叫出来!");
    	}
    
    	
    }
    

      

    package com."".jdkproxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class ProxyPlay  implements InvocationHandler{
    	private Object targetObject = new Object();
    	
    	public Object createProxy(Object targetObject){
    		this.targetObject = targetObject;
    	return	Proxy.newProxyInstance(
    			targetObject.getClass().getClassLoader(), 
    			targetObject.getClass().getInterfaces(), 
    			this);
    	}
    	
    	
    	
    
    	public Object invoke(Object proxy, Method method, Object[] args)
    			throws Throwable {
    		System.out.println("前置代理");
    		Object obj = method.invoke(targetObject, args);
    				System.out.println("后置代理");
    		
    		return obj;
    	}
    
    }
    

      

    package com."".jdkproxy;
    
    public class Test {
    public static void main(String[] args) {
    	
    //获得需求的玩家
    	Iplayer player = new DnfPlayer();
    	
    	//找对应的代理对象
    	ProxyPlay proxy = new ProxyPlay();
    	
    	//执行代练方法
    	Iplayer pl = (Iplayer) proxy.createProxy(player);
    	
    	pl.play();
    	
    	
    	//魔兽玩家
    	WowPlayer wow = new WowPlayer();
    	Iplayer pl2 = (Iplayer) proxy.createProxy(wow);
    	pl2.play();
    	
    		
    }
    }
    

      三.Cglib代理详解

    package com."".cglibproxy;
    
    public interface IPlayer {
    public void play();
    }
    package com."".cglibproxy;
    
    public class DNFPlayer implements IPlayer{
    
    	public void play() {
    		System.out.println("我只玩DNF 给我找个代练");
    		
    	}
    
    }
    

      

    package com."".cglibproxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import net.sf.cglib.proxy.Enhancer;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    //cglib代理
    public class ProxyHome implements MethodInterceptor{
        private Object tarObject;
        
        public Object createCglib(Object tarObject){
            this.tarObject = tarObject;
            
            Enhancer enhancer = new Enhancer();
            enhancer.setClassLoader(tarObject.getClass().getClassLoader());
            enhancer.setInterfaces(tarObject.getClass().getInterfaces());
            enhancer.setCallback(this);
            
            
            
            
            return enhancer.create();
            
        }
        
        
    
        public Object intercept(Object obj, Method method, Object[] args,
                MethodProxy arg3) throws Throwable {
            System.out.println("给你登录代练开始");
    Object object = method.invoke(tarObject, args);
    
    System.out.println("给你退出代练结束");
            
            return object;
        }
        
        
        
    
    }
    package com."".cglibproxy;
    
    public class Test {
    public static void main(String[] args) {
        IPlayer p2 = new  DNFPlayer();
        
        ProxyHome proxy = new ProxyHome();
        
        IPlayer tp = (IPlayer) proxy.createCglib(p2);
        tp.play();
        
    }
    }
  • 相关阅读:
    ubuntu sudo 号令有效处理
    shell脚本的主笔、存储、实行
    Oracle 11gR1中细粒度访谒收集处事(4)
    Solaris效力经管工具 SMF快速入门指南(1)
    Solaris 10完成宁静Kerberos身份验证(2)
    高质量C /C编程指南第2章 程序的版式
    Solaris 10下设置EJB 3.0景遇(1)
    高质量C /C编程指南序言
    Java学习之路:不走弯路,就是捷径 www.51testing.com
    两个结构体可以直接赋值吗? 回复 "JohnsonAnother" 的问题
  • 原文地址:https://www.cnblogs.com/charlypage/p/8878452.html
Copyright © 2020-2023  润新知