Proxy

The final pattern presented in this chapter is the proxy. In the previous section, I mentioned how it is expensive to create objects and how we would like to avoid creating too many of them. The proxy pattern provides a method of controlling the creation and use of expensive objects. The following is the class diagram of the proxy pattern:

Proxy

As you can see, the proxy mirrors the interface of the actual instance. It is substituted for the instance in all the clients and, typically, wraps a private instance of the class. There are a number of places where the proxy pattern can be of use:

  • Lazy instantiation of an expensive object
  • Protection of secret ...

Get Mastering JavaScript Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.