What is a JavaScript “Proxy Pattern”?

前端 未结 5 1807
感情败类
感情败类 2020-12-13 09:21

I\'ve come across the notion of \'Proxy Pattern\' today on jQuery.com, but could not make anything of it. Apparently it is of great use, but I do not understand the idea at

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 09:37

    The Proxy pattern provides a surrogate or placeholder object for another object and controls access to this other object.

    In object-oriented programming, objects do the work they advertise through their interface (properties and methods). Clients of these objects expect this work to be done quickly and efficiently. However, there are situations where an object is severely constrained and cannot live up to its responsibility. Typically this occurs when there is a dependency on a remote resource (resulting in network latency) or when an object takes a long time to load.

    In situations like these you apply the Proxy pattern and create a proxy object that ‘stands in’ for the original object. The Proxy forwards the request to a target object. The interface of the Proxy object is the same as the original object and clients may not even be aware they are dealing with a proxy rather than the real object.

    enter image description here

    Client -- In sample code: the run() function

    • calls Proxy to request an operation.

    Proxy -- In sample code: GeoProxy

    • provides an interface similar to the real object

    • maintains a reference that lets the proxy access the real object

    • handles requests and forwards these to the real object

    RealSubject -- In sample code: GeoCoder

    • defines the real object for which service is requested

提交回复
热议问题