靜態代理
角色分析
- 抽象角色 接口或者抽象類
- 真實角色 被代理的角色
- 代理角色 代理真實角色,一般會做一些附屬操作
- 客戶角色 使用代理角色進行一些操作
AbstractObject
- RealObject
- Proxy
Client
代碼實現
package com.pengshiyu.proxy;
// 租借接口
public interface IRent {
void rent();
}
package com.pengshiyu.proxy;
public class Host implements IRent{
@Override
public void rent() {
System.out.println("房東租房");
}
}
package com.pengshiyu.proxy;
// 中介
public class Proxy implements IRent{
private Host host;
public Proxy(Host host){
this.host = host;
}
@Override
public void rent() {
this.beforeRent();
this.host.rent();
this.afterRent();
}
private void beforeRent(){
System.out.println("中介帶看房");
}
private void afterRent(){
System.out.println("簽訂合同");
}
}
package com.pengshiyu.proxy;
// 客戶
public class Client {
public static void main(String[] args) {
Host host = new Host();
Proxy proxy = new Proxy(host);
proxy.rent();
}
}
使用靜態代理
好處:
- 使得真實角色處理業務更加純粹,不再關注公共的問題
- 公共業務由代理類完成,實現業務的分工
- 公共業務發生擴展時變得更加集中和方便
缺點
- 類多了,代理類,工作量變大,開發效率降低
動態代理
動態代理和靜態代理的角色是一樣的
動態代理的代理類是動態生成的
分類:
- 基於接口的動態代理 jdk 動態代理
- 基於類的動態代理 cglib、javasist
一個動態代理,一般代理某一類業務,可以代理多個類
package com.pengshiyu.proxy;
public interface IVehicle {
void run();
}
package com.pengshiyu.proxy;
public class Car implements IVehicle {
public void run() {
System.out.println("Car會跑");
}
}
package com.pengshiyu.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class VehicleInvocationHandler implements InvocationHandler {
private Object target;
public VehicleInvocationHandler(Object target){
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("---------before-------");
Object result = method.invoke(target, args);
System.out.println("---------after-------");
return result;
}
}
package com.pengshiyu.proxy;
import java.lang.reflect.Proxy;
// 客戶
public class Client {
public static void main(String[] args) {
IVehicle car = new Car();
IVehicle carProxy = (IVehicle)Proxy.newProxyInstance(
car.getClass().getClassLoader(),
car.getClass().getInterfaces(),
new VehicleInvocationHandler(car)
);
carProxy.run();
}
}