In some cases, it is undesirable or not possible to reference an object directly. In such instances, an intermediary known as a proxy can be used to achieve indirect referencing. The proxy object can act as a mediator between the client and the target object, and can filter out content and services that the client cannot see or add additional services that the client requires. Introducing a new object to manipulate the operations of the real object or to act as a stand-in for the real object constitutes the implementation mechanism of the proxy pattern.
The proxy pattern can coordinate the caller and the called party, thereby reducing the coupling of the system to a certain extent.
Remote proxies enable clients to access objects on a remote machine, which may have better computational performance and processing speed, allowing for quick response and handling of client requests.
Virtual proxies can reduce system resource consumption by using a small object to represent a large object, optimizing the system and improving runtime speed.
Protecting proxies can control access permissions to the real object.
Remote Proxy: Provides a local proxy object for an object located in a different address space, which could be within the same host or on another host. A remote proxy is also known as an ambassador.
Virtual Proxy: If there is a need to create an object with high resource consumption, a relatively small object is created to represent it, and the real object is only created when needed.
Copy-on-Write Proxy: This is a type of virtual proxy that defers the copying operation until it is actually needed by the client. Generally, deep cloning of objects is a costly operation, and the copy-on-write proxy can delay this operation until the object is actually used.
Protect or Access Proxy: Controls access to an object and can provide different levels of access permissions to different users.
Cache Proxy: Provides temporary storage space for the result of a specific operation, allowing multiple clients to share these results.
Firewall Proxy: Protects the target from malicious users.
Synchronization Proxy: Allows several users to use an object simultaneously without conflicts.
Smart Reference Proxy: Provides additional operations when an object is referenced, such as recording the number of times this object is called.
classSubject{constructor(){this.name =null;this.__id =null;}say(){thrownewError("Abstract method cannot be called");}}classRealSubjectextendsSubject{constructor(){super();this.name ="real subject";this.__id =1;}say(){ console.log(this.name);}}classSubjectProxy{constructor(){this.instance =newRealSubject;}say(){this.instance.say();}getProperty(key){if(/^_{1,2}.*$/.test(key))thrownewError("Properties beginning with _ or __ are not allowed to be accessed");returnthis.instance[key];}}(function(){var subject =newSubjectProxy(); subject.say();// real subject console.log(subject.getProperty("name"));// real subject// console.log(subject.getProperty("__id")); // Uncaught Error: Properties beginning with _ or __ are not allowed to be accessed})();