The Proxy design pattern is a class functioning as an interface to another class or object.
A Proxy could be for anything, such as a network connection, an object in memory, a file, or anything else you need to provide an abstraction between.
Types of proxies,
Virtual Proxy: An object that can cache parts of the real object, and then complete loading the full object when necessary.
Remote Proxy: Can relay messages to a real object that exists in a different address space.
Protection Proxy: Apply an authentication layer in front of the real object.
Smart Reference: An object whose internal attributes can be overridden or replaced.
Additional functionality can be provided at the proxy abstraction if required. E.g., caching, authorization, validation, lazy initialization, logging.
The proxy should implement the subject interface as much as possible so that the proxy and subject appear identical to the client.
The Proxy Pattern can also be called Monkey Patching or Object Augmentation
Terminology
Proxy: An object with an interface identical to the real subject. Can act as a placeholder until the real subject is loaded or as gatekeeper applying extra functionality.
Subject Interface: An interface implemented by both the Proxy and Real Subject.
Real Subject: The actual real object that the proxy is representing.
Client: The client application that uses and creates the Proxy.
Proxy UML Diagram
Source Code
This concept example will simulate a virtual proxy. The real subject will be called via the proxy. The first time the request is made, the proxy will retrieve the data from the real subject. The second time it is called, it will return the data from the proxies own cache which it created from the first request.
// A Proxy Concept ExampleinterfaceISubject{// An interface implemented by both the Proxy and Real Subjectrequest():void// A method to implement}classRealSubjectimplementsISubject{// The actual real object that the proxy is representingenormousData:number[]constructor(){// hypothetically enormous amounts of datathis.enormousData=[1,2,3]}request(){returnthis.enormousData}}classProxySubjectimplementsISubject{// In this case the proxy will act as a cache for// `enormous_data` and only populate the enormous_data when it// is actually necessaryenormousData:number[]realSubject:RealSubjectconstructor(){this.enormousData=[]this.realSubject=newRealSubject()}request(){// Using the proxy as a cache, and loading data into it only if// it is neededif(this.enormousData.length===0){console.log('pulling data from RealSubject')this.enormousData=this.realSubject.request()returnthis.enormousData}console.log('pulling data from Proxy cache')returnthis.enormousData}}// The ClientconstPROXY_SUBJECT=newProxySubject()// Use the Subject. First time it will load the enormous amounts of dataconsole.log(PROXY_SUBJECT.request())// Use the Subject again, but this time it retrieves it from the local cacheconsole.log(PROXY_SUBJECT.request())
// The Proteus InterfaceexportdefaultinterfaceIProteus{// A Greek mythological character that can change to many formstellMeTheFuture():void// Proteus will change form rather than tell you the futuretellMeYourForm():void// The form of Proteus is elusive like the sea}
importIProteusfrom'./iproteus'importLeopardfrom'./leopard'importSerpentfrom'./serpent'exportdefaultclassLionimplementsIProteus{// Proteus in the form of a Lionname='Lion'tellMeTheFuture():void{// Proteus will change to something randomif(Math.floor(Math.random()*2)){Object.assign(this,newSerpent())this.tellMeTheFuture=Serpent.prototype.tellMeTheFuturethis.tellMeYourForm=Serpent.prototype.tellMeYourForm}else{Object.assign(this,newLeopard())this.tellMeTheFuture=Leopard.prototype.tellMeTheFuturethis.tellMeYourForm=Leopard.prototype.tellMeYourForm}}tellMeYourForm():void{console.log(`I am the form of ${this.name}`)}}
importIProteusfrom'./iproteus'importLeopardfrom'./leopard'importLionfrom'./lion'exportdefaultclassSerpentimplementsIProteus{// Proteus in the form of a Serpentname='Serpent'tellMeTheFuture():void{// Proteus will change to something randomif(Math.floor(Math.random()*2)){Object.assign(this,newLeopard())this.tellMeTheFuture=Leopard.prototype.tellMeTheFuturethis.tellMeYourForm=Leopard.prototype.tellMeYourForm}else{Object.assign(this,newLion())this.tellMeTheFuture=Lion.prototype.tellMeTheFuturethis.tellMeYourForm=Lion.prototype.tellMeYourForm}}tellMeYourForm():void{console.log(`I am the form of ${this.name}`)}}
importIProteusfrom'./iproteus'importLionfrom'./lion'importSerpentfrom'./serpent'exportdefaultclassLeopardimplementsIProteus{// Proteus in the form of a Leopardname='Leopard'tellMeTheFuture():void{// Proteus will change to something randomif(Math.floor(Math.random()*2)){Object.assign(this,newLion())this.tellMeTheFuture=Lion.prototype.tellMeTheFuturethis.tellMeYourForm=Lion.prototype.tellMeYourForm}else{Object.assign(this,newSerpent())this.tellMeTheFuture=Serpent.prototype.tellMeTheFuturethis.tellMeYourForm=Serpent.prototype.tellMeYourForm}}tellMeYourForm():void{console.log(`I am the form of ${this.name}`)}}
Proxy forwards requests onto the Real Subject when applicable, depending on the kind of proxy.
A virtual proxy can cache elements of a real subject before loading the full object into memory.
A protection proxy can provide an authentication layer. For example, an NGINX proxy can add Basic Authentication restriction to an HTTP request.
A proxy can perform multiple tasks if necessary.
A proxy is different from an Adapter. The Adapter will try to adapt two existing interfaces together. The Proxy will use the same interface as the subject.
It is also very similar to the Facade, except you can add extra responsibilities, just like the Decorator. The Decorator however can be used recursively.
The intent of the Proxy is to provide a stand in for when it is inconvenient to access a real subject directly.
The Proxy design pattern may also be called the Surrogate design pattern.