The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. It is a structural design pattern.
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.
Subject
: Abstract Subject role.Proxy
: Proxy Subject role.RealSubject
: Real Subject role.
- 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.
- Introducing a proxy object between the client and the real subject may cause a slowdown in request processing for certain types of proxy patterns.
- Implementing the proxy pattern requires additional work, and some implementations of proxy patterns can be quite complex.
- 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.
class Subject{
constructor(){
this.name = null;
this.__id = null;
}
say(){
throw new Error("Abstract method cannot be called");
}
}
class RealSubject extends Subject{
constructor(){
super();
this.name = "real subject";
this.__id = 1;
}
say(){
console.log(this.name);
}
}
class SubjectProxy{
constructor(){
this.instance = new RealSubject;
}
say(){
this.instance.say();
}
getProperty(key){
if(/^_{1,2}.*$/.test(key)) throw new Error("Properties beginning with _ or __ are not allowed to be accessed");
return this.instance[key];
}
}
(function() {
var subject = new SubjectProxy();
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
})();
https://github.com/WindrunnerMax/EveryDay
https://juejin.im/post/6844903555036364814
https://www.runoob.com/design-pattern/proxy-pattern.html
https://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/singleton.html