二十三种设计模式分类
一、概述
在代理模式(Proxy Pattern
)中,一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。
代理模式的定义:由于某些原因需要给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标对象,代理对象作为访问对象和目标对象之间的中介。
优点
- 代理模式在客户端与目标对象之间起到一个中介作用和保护目标对象的作用;
- 代理对象可以扩展目标对象的功能;
- 代理模式能将客户端与目标对象分离,在一定程度上降低了系统的耦合度;
缺点
- 在客户端和目标对象之间增加一个代理对象,会造成请求处理速度变慢;
- 增加了系统的复杂度;
场景
1、远程代理。
2、虚拟代理。
3、Copy-on-Write
代理。
4、保护(Protect or Access
)代理。
5、Cache
代理。
二、实现
1. 结构图
代理模式的主要角色如下。
- 抽象主题(
Subject
)类:通过接口或抽象类声明真实主题和代理对象实现的业务方法。 - 真实主题(
Real Subject
)类:实现了抽象主题中的具体业务,是代理对象所代表的真实对象,是最终要引用的对象。 - 代理(
Proxy
)类:提供了与真实主题相同的接口,其内部含有对真实主题的引用,它可以访问、控制或扩展真实主题的功能
PS
:UML
结构图可以参考,例子实现并不根据UML
图来完成,灵活实现即可;
2. 实现
2.1 静态代理
- 接口定义
package cn.missbe.model.proxy;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
* @author lyg 2020/4/28 下午2:18
* description:
* 可移动的(形容词)接口
**/
public interface Movable {
public void go();
}
- 被代理对象类
package cn.missbe.model.proxy;
import cn.missbe.model.factory.Moveable;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
* @author lyg 2020/4/28 下午2:18
* description:
* 被代理对象Car
**/
public class Car implements Moveable {
@Override
public void go() {
System.out.println("car is moving wu wu wu....");
}
}
- 日志代理类
package cn.missbe.model.proxy;
import cn.missbe.model.factory.Moveable;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
* @author lyg 2020/4/28 下午2:20
* description:
**/
public class CarLogProxy implements Moveable {
/**被代理的对象*/
Moveable moveable;
public CarLogProxy(Moveable moveable) {
this.moveable = moveable;
}
@Override
public void go() {
///before process
System.out.println("static proxy log start.....");
this.moveable.go();
///after process
System.out.println("static proxy log end.....");
}
}
- 时间代理类
package cn.missbe.model.proxy;
import cn.missbe.model.factory.Moveable;
import java.util.Random;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
*
* @author lyg 2020/4/28 下午2:21
* description:
**/
public class CarTimeProxy implements Moveable {
/**
* 被代理的对象
*/
Moveable moveable;
public CarTimeProxy(Moveable moveable) {
this.moveable = moveable;
}
@Override
public void go() {
///before process
long start = System.currentTimeMillis();
this.moveable.go();
try {
Thread.sleep(new Random().nextInt(10_000));
} catch (InterruptedException e) {
e.printStackTrace();
}
///after process
long end = System.currentTimeMillis();
System.out.println("static proxy car run time:" + (end - start));
}
}
- 静态代理测试主类
package cn.missbe.model.proxy;
import cn.missbe.model.factory.Moveable;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
*
* @author lyg 2020/4/28 下午2:18
* description:
* 静态代理
**/
public class StaticProxyMain {
public static void main(String[] args) {
System.out.println("==========No Proxy=============");
Car car = new Car();
car.go();
System.out.println("==========Proxy=============");
///静态代理的组合,先记录日志再计算运行时间
Moveable moveable = new CarLogProxy(new CarTimeProxy(new Car()));
moveable.go();
}
}
2.2 动态代理
- 时间统计代理
package cn.missbe.model.proxy.dynamic;
import cn.missbe.model.factory.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Random;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
* @author lyg 2020/4/28 下午2:36
* description:
**/
public class CarTimeInvocationHandler implements InvocationHandler {
/**被代理对象*/
private Moveable moveable;
public CarTimeInvocationHandler(Moveable moveable) {
this.moveable = moveable;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
///before process
long start = System.currentTimeMillis();
Object o = method.invoke(moveable, args);
try {
Thread.sleep(new Random().nextInt(10_000));
} catch (InterruptedException e) {
e.printStackTrace();
}
///after process
long end = System.currentTimeMillis();
System.out.println("static proxy car run time:" + (end - start));
return o;
}
}
- 日志代理
package cn.missbe.model.proxy.dynamic;
import cn.missbe.model.factory.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
* @author lyg 2020/4/28 下午2:33
* description:
**/
public class CarLogInvocationHandler implements InvocationHandler {
/**被代理对象*/
private Moveable moveable;
public CarLogInvocationHandler(Moveable moveable) {
this.moveable = moveable;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
///before process
System.out.println("static proxy log start.....");
Object o = method.invoke(moveable, args);
///after process
System.out.println("static proxy log end.....");
return o;
}
}
- 动态代理测试主类
package cn.missbe.model.proxy.dynamic;
import cn.missbe.model.factory.Moveable;
import cn.missbe.model.proxy.Car;
import java.lang.reflect.Proxy;
/**
* Copyright (c) 2020.
* Email: love1208tt@foxmail.com
*
* @author lyg 2020/4/28 下午2:31
* description:
**/
public class DynamicMain {
public static void main(String[] args) {
Car car = new Car();
///保存生成的代理类
// System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Moveable carLog = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), new Class[]{Moveable.class}, new CarLogInvocationHandler(car));
System.out.println("================LOG==================");
carLog.go();
Moveable carTime = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), new Class[]{Moveable.class}, new CarTimeInvocationHandler(car));
System.out.println("=================Time=================");
carTime.go();
}
}
-
JDK
动态代理生成的代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.sun.proxy;
import cn.missbe.model.factory.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy0 extends Proxy implements Moveable {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final void go() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("cn.missbe.model.factory.Moveable").getMethod("go");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}