The proxy pattern
The second pattern that we will talk about in this chapter is the proxy pattern. It is often used for security or optimization purposes.
Roles
The objective of the proxy pattern is to substitute an object (the subject) with another one that will control its access. The object that substitutes the subject shares the same interface, so it is transparent from the consumer's perspective. The proxy is often a small (public) object that stands in for a more complex (private) object that is activated once certain circumstances are clear. The proxy adds a level of indirection by accepting requests from a client object and passing them to the real subject as necessary.
The proxy pattern is used in object-oriented programming. There are several types of proxies, which are as follows:
- A virtual proxy: This allows you to create a "big" object at the appropriate time (used when the creation process is slow)
- A remote proxy: This allows you to access an object that is available...