定义
为其他的对象提供一个代理对象,并且由这个代理对象来控制这个对象的访问。
特点
- 抽象主题角色:定义了被代理角色和代理角色的共同接口或者抽象类
 - 被代理角色:实现或者继承抽象主题角色,定义实现具体业务逻辑的实现。
 - 代理角色:实现或者继承抽象主题角色,持有被代理角色的引用,控制和限制被代理角色实现,并且拥有自己的处理方法。(可能需要进行预处理和善后的工作)
 
示例
package test24;/*** Created By Intellij IDEA** @author Xinrui Yu* @date 2021/12/6 21:40 星期一*/public class Application {public static void main(String[] args) {People people = new People();Student student = new Student();// 会根据我们传入的对象的不同而调用对应的具体方法RunFactory runFactory = new RunFactory(people);runFactory.run();runFactory = new RunFactory(student);runFactory.run();}}interface RunInterface{public void run();}class RunFactory implements RunInterface{private RunInterface run;public RunFactory(RunInterface run) {this.run = run;}@Overridepublic void run(){System.out.println("**************************");run.run();System.out.println("**************************");}}class People implements RunInterface{@Overridepublic void run() {System.out.println("人在跑");}}class Student implements RunInterface{@Overridepublic void run() {System.out.println("学生在跑");}}


