热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

初探设计模式之代理模式:原理与应用解析

在设计模式中,代理模式通过一个代理对象来控制对真实对象的访问。UML图展示了代理类(如MathProxy)维护了一个引用,使得代理能够访问实际的主题对象。代理模式不仅能够延迟初始化昂贵的对象,还能在访问前后添加额外的操作,如权限检查或日志记录。这种模式在远程服务调用、虚拟代理和智能引用等方面有广泛应用。

UML:

2010042413083938.png

Proxy   (MathProxy)
  • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
  • 维持一个引用让代理访问真正的Subject。代理可以引用一个Subject,如果RealSubject和Subject接口是相同的。
  • provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
  • 提供等同于Subject 的接口,这样代理就能替代真正的Subject 
  • controls access to the real subject and may be responsible for creating and deleting it.
  • 控制进入真正的Subject 而且负责创建和删除它
  • other responsibilites depend on the kind of proxy:其他责任取决于代理的类型
    • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
    • 远程代理是负责编码空间的要求,其参数和真正的问题在一个不同的地址编码发送请求。
    • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
    • 虚拟代理可缓存的其他信息的真正Subject ,使他们可以推迟访问它。例如,ImageProxy的用处就是缓存实际图像的。
    • protection proxies check that the caller has the access permissions required to perform a request.
    • 保护代理检查调用方拥有访问权限的规定要求执行。
Subject   (IMath)
  • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • 定义RealSubject和代理的共同界面,使代理可以在任何地方使用RealSubject。
RealSubject   (Math)
  • defines the real object that the proxy represents. 
  • 定义了真正的对象,是代理代表的对象。

Sample:

IMath.cs

 

1 public interface IMath
2 {
3 double Add(double x, double y);
4 double Sub(double x, double y);
5 double Mul(double x, double y);
6 double Div(double x, double y);
7 }

 

 

 

Math.cs

 

1 class Math:IMath
2 {
3 #region IMath 成员
4
5 public double Add(double x, double y)
6 {
7 return x + y;
8 }
9
10 public double Sub(double x, double y)
11 {
12 return x - y;
13 }
14
15 public double Mul(double x, double y)
16 {
17 return x * y;
18 }
19
20 public double Div(double x, double y)
21 {
22 return x / y;
23 }
24
25 #endregion
26 }

 


 

MathProxy.cs

1 class MathProxy:IMath
2 {
3 private Math math = new Math();
4 #region IMath 成员
5
6 public double Add(double x, double y)
7 {
8 return math.Add(x, y);
9 }
10
11 public double Sub(double x, double y)
12 {
13 return math.Sub(x, y);
14 }
15
16 public double Mul(double x, double y)
17 {
18 return math.Mul(x, y);
19 }
20
21 public double Div(double x, double y)
22 {
23 return math.Div(x, y);
24 }
25
26 #endregion
27 }

 

Program.cs

1 #region Proxy
2 MathProxy proxy = new MathProxy();
3 Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));
4 Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));
5 Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));
6 Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));
7 #endregion

 





转:https://www.cnblogs.com/TivonStone/archive/2010/04/24/1719294.html



推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 在macOS环境下使用Electron Builder进行应用打包时遇到签名验证失败的问题,具体表现为签名后spctl命令检测到应用程序未通过公证(Notarization)。本文将详细探讨该问题的原因及解决方案。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 解读MySQL查询执行计划的详细指南
    本文旨在帮助开发者和数据库管理员深入了解如何解读MySQL查询执行计划。通过详细的解析,您将掌握优化查询性能的关键技巧,了解各种访问类型和额外信息的含义。 ... [详细]
author-avatar
Dearlily2046_394
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有