设计模式——代理模式
名称 | Proxy |
结构 | |
意图 | 为其他对象提供一种代理以控制对这个对象的访问。 |
适用性 |
|
Code Example |
// Factory Method // Intent: "Provide a surrogate or placeholder for another object to // control access to it". // For further information, read "Design Patterns", p207, Gamma et al., // Addison-Wesley, ISBN:0-201-63361-2 /* Notes: * When there is a large CPU/memory expense attached to handling an object * directly, it can be useful to use a lightweight proxy in front of it, * which can take its place until the real object is needed. */ namespace Proxy_DesignPattern { using System; using System.Threading; /// <summary> /// Summary description for Client. /// </summary> abstract class CommonSubject { abstract public void Request(); } class ActualSubject : CommonSubject { public ActualSubject() { // Assume constructor here does some operation that takes quite a // while - hence the need for a proxy - to delay incurring this // delay until (and if) the actual subject is needed Console.WriteLine("Starting to construct ActualSubject"); Thread.Sleep(1000); // represents lots of processing! Console.WriteLine("Finished constructing ActualSubject"); } override public void Request() { Console.WriteLine("Executing request in ActualSubject"); } } class Proxy : CommonSubject { ActualSubject actualSubject; override public void Request() { if (actualSubject == null) actualSubject = new ActualSubject(); actualSubject.Request(); } } public class Client { public static int Main(string[] args) { Proxy p = new Proxy(); // Perform actions here // . . . if (1==1) // at some later point, based on a condition, p.Request();// we determine if we need to use subject return 0; } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 99%的人不知道,桥接模式失败的真正原因是它!
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能