» The Proxy pattern supports objects that control the creation of and access to other objects; » The proxy is often a small (public) object that stands in for a more complex (private) object; » Proxies are interfaces to classes that have sensitive data or slow operations.
Different Types of Proxies
Virtual proxies: » Hands the creation of an object over to another object; » Useful if the creation process might be slow or might prove unnecessary.
Authentication proxies: » Checks that the access permissions for a request are correct.
Remote proxies: » Encodes requests and sends them across a network.
Smart proxies: » Adds to or changes requests before sending them on.
Use When:
» You have objects that are expensive to create; » You have objects that need access control; » You have objects that access remote sites; » You have objects that need to perform some action whenever they are accessed.
public classPictureAccessor { //Private field not necessary for Proxy pattern but required in order to get the loaded image name in the console application. private static string _imageName;
//Define the IPicture interface represents the "blueprint" for a set of complex classes (objects). public interfaceIPicture { Image GetImage(); }
//The LoadPicture class (object) inherits from the IPicture interface and represents one type of "complex" class. private classLoadPicture : IPicture { publicImage GetImage() { //Prepare extraction of the embeded image file Assembly assembly = Assembly.GetExecutingAssembly(); string resourcePath = $"PatternsConsole.Structural.Proxy.Images.pictureloading.jpg";
using (Stream stream = assembly.GetManifestResourceStream(resourcePath)) { if (stream != null) { _imageName = "pictureloading.jpg"; return newBitmap(stream); } }
return null; } }
//The FinalPicture class (object) inherits from the IPicture interface and represents another type of "complex" class. private classFinalPicture : IPicture { publicImage GetImage() { //Prepare extraction of the embeded image file Assembly assembly = Assembly.GetExecutingAssembly(); string resourcePath = $"PatternsConsole.Structural.Proxy.Images.picturefinal.jpg";
using (Stream stream = assembly.GetManifestResourceStream(resourcePath)) { if (stream != null) { _imageName = "picturefinal.jpg"; return newBitmap(stream); } }
return null; } }
//The PictureProxy class manages the creation of the "complex" classes and provides a single interface for the client. public classPictureProxy : IPicture { //Public field not necessary for Proxy pattern but required in order to get the loaded image name in the console application. public string ImageName;
public classPictureAccessor { //Private field not necessary for Proxy pattern but required in order to get the loaded image name in the console application. private static string _imageName;
//Define the IPicture interface represents the "blueprint" for a set of complex classes (objects). public interfaceIPicture { Image GetImage(); }
//The LoadPicture class (object) inherits from the IPicture interface and represents one type of "complex" class. private classLoadPicture : IPicture { publicImage GetImage() { //Prepare extraction of the embeded image file Assembly assembly = Assembly.GetExecutingAssembly(); string resourcePath = $"PatternsConsole.Structural.Proxy.Images.pictureloading.jpg";
using (Stream stream = assembly.GetManifestResourceStream(resourcePath)) { if (stream != null) { _imageName = "pictureloading.jpg"; return newBitmap(stream); } }
return null; } }
//The FinalPicture class (object) inherits from the IPicture interface and represents another type of "complex" class. private classFinalPicture : IPicture { publicImage GetImage() { //Prepare extraction of the embeded image file Assembly assembly = Assembly.GetExecutingAssembly(); string resourcePath = $"PatternsConsole.Structural.Proxy.Images.picturefinal.jpg";
using (Stream stream = assembly.GetManifestResourceStream(resourcePath)) { if (stream != null) { _imageName = "picturefinal.jpg"; return newBitmap(stream); } }
return null; } }
//The PictureProxy class manages the creation of the "complex" classes and provides a single interface for the client. public classPictureProxy : IPicture { //Public field not necessary for Proxy pattern but required in order to get the loaded image name in the console application. public string ImageName;