69期-Java SE-025-动态代理

Posted hijackykun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了69期-Java SE-025-动态代理相关的知识,希望对你有一定的参考价值。

 

技术图片

技术图片

Apple.java

package com.southwind.dymicproxy;

public class Apple implements Phone 

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        return "销售iPhone手机";
    

 

Benz.java

package com.southwind.dymicproxy;

public class Benz implements Car 

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        return "销售奔驰汽车";
    

 

BMW.java

package com.southwind.dymicproxy;

public class BMW implements Car 

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        return "销售宝马汽车";
    

 

Car.java

package com.southwind.dymicproxy;

public interface Car 
    public String saleCar();

 

House.java

package com.southwind.dymicproxy;

public interface House 
    public String saleHouse();

 

HuaWei.java

package com.southwind.dymicproxy;

public class HuaWei implements Phone 

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        return "销售华为手机";
    

 

MyInvocationHandler.java

package com.southwind.dymicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler 
    private Object obj;
    
    //返回代理对象
    public Object bind(Object obj) 
        this.obj = obj;
        return Proxy.newProxyInstance(MyInvocationHandler.class.getClassLoader(), this.obj.getClass().getInterfaces(), this);  
    

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable 
        // TODO Auto-generated method stub
        System.out.println("日志。。。");
        return method.invoke(this.obj, args);
    

 

Num.java

package com.southwind.dymicproxy;

public class Num implements House 

    @Override
    public String saleHouse() 
        // TODO Auto-generated method stub
        return "卖别墅";
    

 

Phone.java

package com.southwind.dymicproxy;

public interface Phone 
    public String salePhone();

 

Test.java

package com.southwind.dymicproxy;

public class Test 
    public static void main(String[] args) 
        Phone phone = new Apple();
        //获取代理对象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
        Phone proxy = (Phone)myInvocationHandler.bind(phone);
        System.out.println(proxy.salePhone());
        Car car = new BMW();
        Car carProxy = (Car)myInvocationHandler.bind(car);
        System.out.println(carProxy.saleCar());
        House house = new Num();
        House houseProxy = (House)myInvocationHandler.bind(house);
        System.out.println(houseProxy.saleHouse());
    

技术图片

Cal.java

package com.southwind.proxy;

public interface Cal 
    public int add(int num1,int num2);
    public int sub(int num1,int num2);
    public int mul(int num1,int num2);
    public int div(int num1,int num2);

 

CalImpl.java

package com.southwind.proxy;

public class CalImpl implements Cal 
    
    @Override
    public int add(int num1, int num2) 
        // TODO Auto-generated method stub
        int result = num1+num2;
        return result;
    
    
    @Override
    public int sub(int num1, int num2) 
        // TODO Auto-generated method stub
        int result = num1-num2;
        return result;
    

    @Override
    public int mul(int num1, int num2) 
        // TODO Auto-generated method stub
        int result = num1*num2;
        return result;
    

    @Override
    public int div(int num1, int num2) 
        // TODO Auto-generated method stub
        int result = num1/num2;
        return result;
    

 

MyInvocationHandler.java

package com.southwind.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyInvocationHandler implements InvocationHandler 
    //委托对象
    private Object object;
    
    //返回代理对象
    public Object bind(Object object) 
        this.object = object;
        return Proxy.newProxyInstance(MyInvocationHandler.class.getClassLoader(), this.object.getClass().getInterfaces(), this); 
    

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable 
        // TODO Auto-generated method stub
        System.out.println(method.getName()+"方法的参数是:"+Arrays.toString(args));
        Object result = method.invoke(this.object, args);
        System.out.println(method.getName()+"方法的执行结果是:"+result);
        return result;
    

 

Test.java

package com.southwind.proxy;

public class Test 
    public static void main(String[] args) 
        Cal cal = new CalImpl();
        //获取动态代理对象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
        Cal proxy = (Cal) myInvocationHandler.bind(cal);
        System.out.println(proxy.add(10, 1));
        System.out.println(proxy.sub(20, 2));
        System.out.println(proxy.mul(30, 3));
        System.out.println(proxy.div(40, 4));
    

技术图片

Apple.java

package com.southwind.staticproxy;

public class Apple implements Phone 

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        return "销售iPhone手机";
    

 

Benz.java

package com.southwind.staticproxy;

public class Benz implements Car 

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        return "销售奔驰汽车";
    

 

BMW.java

package com.southwind.staticproxy;

public class BMW implements Car 

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        return "销售宝马汽车";
    

 

Car.java

package com.southwind.staticproxy;

public interface Car 
    public String saleCar();

 

CarProxy.java

package com.southwind.staticproxy;

public class CarProxy implements Car 
    
    private Car car;
    
    public CarProxy(Car car) 
        this.car = car;
    

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        System.out.println("汽车日志。。。");
        return this.car.saleCar();
    

 

House.java

package com.southwind.staticproxy;

public interface House 
    public String saleHouse();

 

Huawei.java

package com.southwind.staticproxy;

public class HuaWei implements Phone 

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        return "销售华为手机";
    

 

Phone.java

package com.southwind.staticproxy;

public interface Phone 
    public String salePhone();

 

PhoneProxy.java

package com.southwind.staticproxy;

public class PhoneProxy implements Phone 
    
    private Phone phone;
    
    public PhoneProxy(Phone phone) 
        this.phone = phone;
    

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        System.out.println("日志。。。");
        return this.phone.salePhone();
    

 

Proxy.java

package com.southwind.staticproxy;

public class Proxy implements Car,Phone,House
    
    private Object obj;
    
    public Proxy(Object obj) 
        this.obj = obj;
    

    @Override
    public String saleCar() 
        // TODO Auto-generated method stub
        System.out.println("汽车日志。。。");
        if(obj instanceof Car) 
            Car car = (Car) obj;
            return car.saleCar();
        else 
            System.out.println("数据类型错误");
            return null;
        
    

    @Override
    public String salePhone() 
        // TODO Auto-generated method stub
        System.out.println("手机日志。。。");
        if(obj instanceof Phone) 
            Phone phone = (Phone) obj;
            return phone.salePhone();
        else 
            System.out.println("数据类型错误");
            return null;
        
    

    @Override
    public String saleHouse() 
        // TODO Auto-generated method stub
        return null;
    

 

Test.java

package com.southwind.staticproxy;

public class Test 
    public static void main(String[] args) 
        Phone phone1 = new Apple();
        
        Phone phone2 = new HuaWei();
        PhoneProxy phoneProxy = new PhoneProxy(phone1);
        System.out.println(phoneProxy.salePhone());
    

 

Test2.java

package com.southwind.staticproxy;

public class Test2 
    public static void main(String[] args) 
        Car car = new BMW();
        CarProxy carProxy = new CarProxy(car);
        System.out.println(carProxy.saleCar());
    

 

Test3.java

package com.southwind.staticproxy;

public class Test3 
    public static void main(String[] args) 
        Phone phone = new Apple();
        Proxy proxy = new Proxy(phone);
        System.out.println(proxy.salePhone());
        Car car = new Benz();
        proxy = new Proxy(car);
        System.out.println(proxy.saleCar());
    

 

以上是关于69期-Java SE-025-动态代理的主要内容,如果未能解决你的问题,请参考以下文章

java开发必学知识:动态代理

AOP与JAVA动态代理

java代理模式

动态代理

动态代理

Java--Spring之AOP面向切面编程