Structural Design Patterns



Provide a surrogate or placeholder for another object to control acces to it.. (Gamma, 151)


The Proxy pattern can simulate another class until that class is actually needed. This allows developers to better manage resources. A basic example of this is how a modern browser loads a web page. It first just loads a structure of the page. Then it begins loading images and content that are currently visible on the screen. Eventually, it has all of the page loaded, but the user didn't have to wait for all of that to complete before they were able to view the page.


The design of the proxy class is to have two classes that inherit from a primary class "subject". One of them is the proxy class that pretends to be the "RealSubject" until that subject is actually needed or ready. Once it is ready, to proxy class returns gives the "RealSubject" when the Subject is requested.


Autobot Robotics is wanting to speed up the startup time of their application. They decide to move the bulk of the code into DLLs that can be loaded dynamically. Initially, just a basic framework of the program is loaded. As the user requests more sophistocated behavior, the software uses a proxy to load that code into memory as it is needed. This resulted in a dramatic reduction in startup time.


The detection of when an object is needed can complicate the design. As with most design patterns, the proxy pattern should only be used when it is needed.