java代理简介

二、代理

1.1、代理概念

代理(Proxy)是一种设计模式,提供了对目标对象的另外的访问方式。

代理意义:可以再目标对象代码实现的基础上,增强额外的功能代码。

1.2、核心原理图

java代理简介_第1张图片

1.3、Java中三种创建代理对象方式

1.3.0、接口

接口是对行为的抽象,将所有方法都抽象(没有属性只有方法)

类和接口之间关键字是implements实现

java代理简介_第2张图片

1.3.0.1、思路图

java代理简介_第3张图片

1.3.1、静态代理

静态代理比较死板不灵活,一般情况下不适用

目标类接口:

public interface ByClothes {
	public void clothes(String size);
}

目标类:

public class ClothesFactory implements ByClothes{
	@Override
	public void clothes(String size) {
		// TODO Auto-generated method stub
		System.out.println("已经定制了一套"+size+"的cos服");
	}
}

代购类:

public class BuyClothes implements ByClothes{
	//目标对象
	public ClothesFactory clothesFactory;
	//构造方法赋值
	public BuyClothes(ClothesFactory clothesFactory) {
		this.clothesFactory = clothesFactory;
	}
	
	@Override
	public void clothes(String size) {
		// TODO Auto-generated method stub
		frontService();
		System.out.println("已经定制了一套"+size+"的cos服");
		backService();
	}
	public void frontService() {
		System.out.println("售前服务");
	}
	public void backService() {
		System.out.println("售后服务");
	}

}

测试及结果:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClothesFactory clothesFactory=new ClothesFactory();
		BuyClothes buyClothes=new BuyClothes(clothesFactory);
		buyClothes.clothes("4xL");
		System.out.println("************");
		clothesFactory.clothes("5xL");
	}
 
 结果:
     售前服务
    已经定制了一套4xL的cos服
    售后服务
    ************
    已经定制了一套5xL的cos服

1.3.2、基于jdk(接口)的动态代理

接口:

public interface ByShoot {
	void byShoot(String size);
}

目标类:

public class ShootFactory implements ByShoot{

	@Override
	public void byShoot(String size) {
		// TODO Auto-generated method stub
		System.out.println("定制了一双"+size+"码的鞋");
	}

}

动态代理类:

public class Company implements InvocationHandler{
	
	private Object object;
	
	/**
	 * @return the object
	 */
	public Object getObject() {
		return object;
	}
	/**
	 * @param object the object to set
	 */
	public void setObject(Object object) {
		this.object = object;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		frontService();
		method.invoke(object, args);
		backService();
		return null;
	}
	public void frontService() {
		System.out.println("售前服务");
	}
	public void backService() {
		System.out.println("售后服务");
	}
	//获取一个代购人员
	public Object getProxyInstance() {
		Object obj=Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
		return obj;
		
	}

测试:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClothesFactory clothesFactory=new ClothesFactory();
		ShootFactory shootFactory=new ShootFactory();
		Company company=new Company();
		//代理卖衣服
		company.setObject(clothesFactory);
		ByClothes object=(ByClothes)company.getProxyInstance();
		object.clothes("4xL");
		System.out.println("************");
		//代理卖鞋
		company.setObject(shootFactory);
		ByShoot object1=(ByShoot)company.getProxyInstance();
		object1.byShoot("43");
	}

1.3.3、基于CGLIB的动态代理

你可能感兴趣的:(java,代理模式,开发语言)