69期-Java SE-025-动态代理
2020-12-13 06:17
标签:app info bsp object icp 获取 nts port obj Apple.java Benz.java BMW.java Car.java House.java HuaWei.java MyInvocationHandler.java Num.java Phone.java Test.java Cal.java CalImpl.java MyInvocationHandler.java Test.java Apple.java Benz.java BMW.java Car.java CarProxy.java House.java Huawei.java Phone.java PhoneProxy.java Proxy.java Test.java Test2.java Test3.java 69期-Java SE-025-动态代理 标签:app info bsp object icp 获取 nts port obj 原文地址:https://www.cnblogs.com/HiJackykun/p/11173369.htmlpackage com.southwind.dymicproxy;
public class Apple implements Phone {
@Override
public String salePhone() {
// TODO Auto-generated method stub
return "销售iPhone手机";
}
}
package com.southwind.dymicproxy;
public class Benz implements Car {
@Override
public String saleCar() {
// TODO Auto-generated method stub
return "销售奔驰汽车";
}
}
package com.southwind.dymicproxy;
public class BMW implements Car {
@Override
public String saleCar() {
// TODO Auto-generated method stub
return "销售宝马汽车";
}
}
package com.southwind.dymicproxy;
public interface Car {
public String saleCar();
}
package com.southwind.dymicproxy;
public interface House {
public String saleHouse();
}
package com.southwind.dymicproxy;
public class HuaWei implements Phone {
@Override
public String salePhone() {
// TODO Auto-generated method stub
return "销售华为手机";
}
}
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);
}
}
package com.southwind.dymicproxy;
public class Num implements House {
@Override
public String saleHouse() {
// TODO Auto-generated method stub
return "卖别墅";
}
}
package com.southwind.dymicproxy;
public interface Phone {
public String salePhone();
}
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());
}
}
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);
}
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;
}
}
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;
}
}
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));
}
}
package com.southwind.staticproxy;
public class Apple implements Phone {
@Override
public String salePhone() {
// TODO Auto-generated method stub
return "销售iPhone手机";
}
}
package com.southwind.staticproxy;
public class Benz implements Car {
@Override
public String saleCar() {
// TODO Auto-generated method stub
return "销售奔驰汽车";
}
}
package com.southwind.staticproxy;
public class BMW implements Car {
@Override
public String saleCar() {
// TODO Auto-generated method stub
return "销售宝马汽车";
}
}
package com.southwind.staticproxy;
public interface Car {
public String saleCar();
}
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();
}
}
package com.southwind.staticproxy;
public interface House {
public String saleHouse();
}
package com.southwind.staticproxy;
public class HuaWei implements Phone {
@Override
public String salePhone() {
// TODO Auto-generated method stub
return "销售华为手机";
}
}
package com.southwind.staticproxy;
public interface Phone {
public String salePhone();
}
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();
}
}
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;
}
}
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());
}
}
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());
}
}
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());
}
}