简介
代理模式的统一建模语言(UML)图 代理模式的LePUS3建模语言(英语:LePUS3) (图例)代理模式(英语:Proxy Pattern)是程序设计中的一种设计模式。所谓的代理者是指一个类别可以作为其它东西的接口。代理者可以作任何东西的接口:网络连接、存储器中的大对象、文件或其它昂贵或无法复制的资源。着名的代理模式例子为引用计数(英语:reference counting)指针对象。当一个复杂对象的多份副本须存在时,代理模式可以结合享元模式以减少存储器用量。典型作法是创建一个复杂对象及多个代理者,每个代理者会引用到原本的复杂对象。而作用在代理者的运算会转送到原本对象。一旦所有的代理者都不存在时,复杂对象会被移除。
示例
Java
以下Java示例解释”虚拟代理”模式。ProxyImage
类别用来访问远程方法。
import java.util.*; interface Image { public void displayImage();}//on System A class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() { System.out.println("Loading " + filename); } public void displayImage() { System.out.println("Displaying " + filename); }}//on System B class ProxyImage implements Image { private String filename; private Image image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if(image == null) image = new RealImage(filename); image.displayImage(); }} class ProxyExample { public static void main(String args) { Image image1 = new ProxyImage("HiRes_10MB_Photo1"); Image image2 = new ProxyImage("HiRes_10MB_Photo2"); image1.displayImage(); // loading necessary image2.displayImage(); // loading necessary }}
程序的输出为:
Loading HiRes_10MB_Photo1Displaying HiRes_10MB_Photo1Loading HiRes_10MB_Photo2Displaying HiRes_10MB_Photo2
C#
using System;namespace Proxy{ class Program { interface IImage { void Display(); } class RealImage : IImage { public RealImage(string fileName) { FileName = fileName; LoadFromFile(); } private void LoadFromFile() { Console.WriteLine("Loading " + FileName); } public String FileName { get; private set; } public void Display() { Console.WriteLine("Displaying " + FileName); } } class ProxyImage : IImage { public ProxyImage(string fileName) { FileName = fileName; } public String FileName { get; private set; } private IImage image; public void Display() { if (image == null) image = new RealImage(FileName); image.Display(); } } static void Main(string args) { IImage image = new ProxyImage("HiRes_Image"); for (int i = 0; i < 10; i++) image.Display(); } }}
程序的输出为:
Loading HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_ImageDisplaying HiRes_Image
另见
组合模式
修饰模式
惰性初始模式