当前位置: 首页 > news >正文

面向对象设计模式:结构型模式之代理模式

一、引入

  • 访问 FB:代理服务器
    在这里插入图片描述

二、代理模式

aka Surrogate

2.1 Intent 意图

  • Provide a surrogate (代理) or placeholder for another object to control access to it. 为另一个对象提供一个代理或占位符,以控制对它的访问。
  • 代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用

2.2 Motivation 动机

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it.
控制对对象的访问的一个原因是要延迟其创建和初始化的全部成本,直到我们实际需要使用它为止。即只在必要时生成开销大的对象。
Example:
An image proxy creates the real image only when the document editor asks it to display itself. The proxy forwards subsequent requests directly to the image.
只有在文档编辑器要求其显示图像时,图像代理才会创建真实的图像,代理会将后续请求直接转发到图像。

在这里插入图片描述

2.2 Applicability 适用性

  • A remote proxy provides a local representative for an object in a different address space. 远程代理为一个不同地址空间中的对象提供了一个本地代表.

  • A virtual proxy creates expensive objects on demand. 虚拟代理在必要时创建开销大的对象.

  • A protection proxy controls access to the original object. 保护代理可控制对原始对象的访问.

  • A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. 在访问对象时,可以执行其他操作的智能引用是裸指针的替代品。

    • counting the number of references to the real object so that it can be freed automatically when there are no more references (smart pointers).
    • loading a persistent object into memory when it’s first referenced.
    • checking that the real object is locked before it’s accessed to ensure that no other object can change it.

2.3 类图

在这里插入图片描述

2.4 Consequences 重要性

  • 远程代理:隐藏对象位于不同地址空间中的事实
  • 虚拟代理:优化,仅需要时才创建对象
  • 保护代理 & 智能引用:提供额外功能,如访问控制
  • Copy-on-write 写时复制:Copying a large and complicated object can be an expensive operation. If the copy is never modified, then there’s no need to incur this cost. By using a proxy to postpone the copying process, we ensure that we pay the price of copying the object only if it’s modified.

2.5 应用实例:糖果机

http://www.lryc.cn/news/31676.html

相关文章:

  • CCF大数据专家委员会十周年纪念庆典纪实:拥抱数字时代,展望科技未来
  • Qt学习3-Qt Creator四则运算计算器(哔站视频学习记录)
  • 学习 Python 之 Pygame 开发魂斗罗(九)
  • 最简单的SpringBoot+MyBatis多数据源实现
  • Spring Boot 3.0系列【8】核心特性篇之SpringApplication
  • Nginx的搭建与核心配置
  • Java学习笔记 --- jQuery
  • 华为OD机试题,用 Java 解【字符串加密】问题
  • 软聚类算法:模糊聚类 (Fuzzy Clustering)
  • Java Web 实战 02 - 多线程基础篇(1)
  • C/C++开发,无可避免的多线程(篇三).协程及其支持库
  • 高级信息系统项目管理(高项 软考)原创论文项目背景合集
  • 锁屏面试题百日百刷-Hive篇(十一)
  • 一看就懂,等保2.0工作流程这么做
  • Kerberos 域委派攻击之非约束性委派
  • 【容器运行时】一文理解 OCI、runc、containerd、docker、shim进程、cri、kubelet 之间的关系
  • spark兼容性验证
  • docker逃逸复现--pid=host模式下的逃逸
  • 【环境配置】Windows系统下搭建Pytorch框架
  • Dockerfile简单使用入门
  • 什么是CCC认证3C强制认证机构
  • C语言-基础了解-18-C共用体
  • Vue基础18之github案例、vue-resource
  • UE4 c++ Mediaplayer取消自动播放,运行时首帧为黑屏的问题
  • C语言-基础了解-17-C结构体
  • Python爬虫实践:优志愿 院校列表
  • Java框架学习 | MySQL和Maven笔记
  • C++入门教程||C++ 变量作用域||C++ 常量
  • 想找工作,这一篇15w字数+的文章帮你解决
  • Mac brew搭建php整套开发环境