设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记——代理(Proxy)模式
@(设计模式)[设计模式, 代理模式, proxy]
- 设计模式学习笔记代理Proxy模式
- 基本介绍
- 代理案例
- 类图
- 实现代码
- Printable接口
- Printer类
- PrinterProxy类
- 测试类
- 运行结果
- 代理模式中的角色
- Subject主体
- Proxy代理人
- RealSubject实际的主体
- Client请求者
- 类图
基本介绍
代理模式可以简单的看作是有个中间人代替你进行工作,当中间人完成不了时,才会由你出马完成。
代理案例
类图
实现代码
Printable接口
package com.pc.proxy.example;/*** 可打印接口* Created by Switch on 2017/3/31.*/
public interface Printable {/*** 设置名字** @param name 名字*/void setPrinterName(String name);/*** 获取名字** @return 名字*/String getPrinterName();/*** 显示文字** @param string 文字*/void print(String string);
}
Printer类
package com.pc.proxy.example;/*** 打印类* Created by Switch on 2017/3/31.*/
public class Printer implements Printable {/*** 名字*/private String name;public Printer() {this.heavyJob("正在生成Printer的实例");}public Printer(String name) {this.name = name;this.heavyJob("正在生成Printer的实例(" + this.name + ")");}@Overridepublic void setPrinterName(String name) {this.name = name;}@Overridepublic String getPrinterName() {return this.name;}@Overridepublic void print(String string) {System.out.println("===" + this.name + "===");System.out.println(string);}/*** 工作** @param msg 消息*/private void heavyJob(String msg) {System.out.print(msg);for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.print(".");}System.out.println("结束。");}
}
PrinterProxy类
package com.pc.proxy.example;/*** 打印代理类* Created by Switch on 2017/3/31.*/
public class PrinterProxy implements Printable {/*** 名字*/private String name;/*** 本人*/private Printer real;public PrinterProxy(String name) {this.name = name;}@Overridepublic synchronized void setPrinterName(String name) {if (this.real != null) {this.real.setPrinterName(name);}this.name = name;}@Overridepublic String getPrinterName() {return this.name;}@Overridepublic void print(String string) {this.realize();this.real.print(string);}/*** 生成本人*/private synchronized void realize() {if (this.real == null) {this.real = new Printer(this.name);}}
}
测试类
package com.pc.proxy.example.test;import com.pc.proxy.example.Printable;
import com.pc.proxy.example.PrinterProxy;
import org.junit.Test;/*** PrinterProxy Tester.** @author Switch* @version 1.0*/
public class ProxyTest {/*** 测试代理模式*/@Testpublic void testProxy() {Printable p = new PrinterProxy("Alice");System.out.println("现在的名字是" + p.getPrinterName() + "。");p.setPrinterName("Bob");System.out.println("现在的名字是" + p.getPrinterName() + "。");p.print("Hello, World.");}}
运行结果
现在的名字是Alice。
现在的名字是Bob。
正在生成Printer的实例(Bob).....结束。
===Bob===
Hello, World.
代理模式中的角色
Subject(主体)
Subject
角色定义了使Proxy
角色和RealSubject
角色之间具有一致性的接口。由于存在Subject
角色,所以Client
角色不必在意它所使用的究竟是Proxy
角色还是RealSubject
角色。在案例中,由Printable
接口扮演此角色。
Proxy(代理人)
Proxy
角色会尽量处理来自Client
角色的请求。只有当自己不能处理时,它才会将工作交给RealSubject
角色。Proxy
角色只有在必要时才会生成RealSubject
角色。Proxy
角色实现了在Subject
角色中定义的接口(API
)。在案例中,由PrinterProxy
类扮演此角色。
RealSubject(实际的主体)
“本人”RealSubject
角色会在“代理人”Proxy
角色无法胜任工作时出场。它与Proxy
角色一样,也实现了在Subject
角色中定义的接口(API
)。在案例中,由Printer
类扮演此角色。
Client(请求者)
使用Proxy
模式的角色。在GoF
书中,Client
角色并不包含在Proxy
模式中。在案例中,由测试类扮演此角色。
类图
GitHub:DesignPatternStudy
——————参考《图解设计模式》
设计模式学习笔记——代理(Proxy)模式相关推荐
- 设计模式学习笔记——解释器(Interpreter)模式
设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...
- 设计模式学习笔记——命令(Command)模式
设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...
- 设计模式学习笔记——状态(State)模式框架
设计模式学习笔记--状态(State)模式框架 @(设计模式)[设计模式, 状态模式, State] 设计模式学习笔记状态State模式框架 基本介绍 状态案例 类图 实现代码 State接口 Day ...
- 设计模式学习笔记——备忘录(Memento)模式
设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...
- 设计模式学习笔记——观察者(Observer)模式
设计模式学习笔记--观察者(Observer)模式 @(设计模式)[设计模式, 观察者模式, Observer] 设计模式学习笔记观察者Observer模式 基本介绍 观察者案例 类图 实现代码 Ob ...
- 设计模式学习笔记——外观(Facade)模式
设计模式学习笔记--外观(Facade)模式 @(设计模式)[设计模式, 外观模式, facade] 设计模式学习笔记外观Facade模式 基本介绍 外观案例 类图 实现代码 Database类 ma ...
- 设计模式学习笔记——访问者(Visitor)模式
设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...
- 设计模式学习笔记——装饰(Decorator)模式
设计模式学习笔记--装饰(Decorator)模式 @(设计模式)[设计模式, 装饰模式, decorator] 设计模式学习笔记装饰Decorator模式 基本介绍 装饰案例 类图 实现代码 Dis ...
- 设计模式学习笔记——组合(Composite)模式
设计模式学习笔记--组合(Composite)模式 @(设计模式)[设计模式, 组合模式, composite] 设计模式学习笔记组合Composite模式 基本介绍 组合案例 类图 实现代码 Ent ...
最新文章
- [转]android的selector,背景选择器
- 【错误记录】反射内部类报错 ( Android 使用 Hook 时反射内部类报错 )
- 网络:HTTP的请求方法
- jQuery easyUI--选项卡右键菜单
- 分数诚可贵的飞鸽传书2012绿色版
- 阅读《构建之法》 5-7章
- 算法提高 求最大值java_算法笔记_096:蓝桥杯练习 算法提高 求最大值(Java)
- powershell局域网内同步文件夹,文件脚本
- 全国计算机二级报名入口新疆,新疆2019年3月全国计算机等级(NCRE)考试(第54次)报名入口...
- iOS开发Assertion failure in -[AFJSONRequestSerializer requestWithMethod:URLString:parameters:error:]
- 什么是重载?重载的定义是什么?如何判断方法是否重载?最详细解答博文
- VMware vCenter Server 7.0 U2b/6.7 U3n/6.5 U3p 修复高危安全漏洞
- 【科普】气垫船是如何浮在水面上行驶的
- 怎么删除html下面多余的空白页,word怎么删除空白页面(word删掉多余空白页方法)...
- 安装caffe依赖库时,出现“您希望继续执行吗? [Y/n] y 中止。”的解决办法
- 谷歌google自动打开开发调式工具问题DevTools
- 如何在 Win上写 Python 代码?最佳攻略来袭
- 【OJ每日一练】1114 - 坐大巴
- “你没有权限查看该对象的安全属性,即使你是管理用户“
- MyBatisPlus批量新增或修改执行器
热门文章
- 【JAVA 第三章 流程控制语句】课后习题 判断用户输入的数是否为质数
- 【Virtual DOM】虚拟 DOM 和 Snabbdom 库
- 【Java】实现矩阵的转置
- C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)
- apple开发过程中错误汇总ios+macos
- inode索引节点的概念
- houdini_通过卡通了解CSS Houdini的指南
- 如何开始使用centos_如何开始使用CentOS
- 募捐名单_年度捐款,以及为什么现在应该捐款,而不是以后
- python matplotlib图中插入表格