Proxy pattern
A stand-in with the same interface as an object, which controls when and whether calls reach it.
Definition
A proxy can create the real object only when it's first needed (virtual proxy), check permissions (protection proxy), remember results (caching proxy) or forward calls to another machine (remote proxy). Callers can't tell it from the real object. Frameworks generate proxies for lazy ORM associations and for annotations such as @Transactional, which only work for calls that go through the proxy.
In practice
A LazyDocument shows its title from the file name and reads the file only when content() is called.
Related terms
- Lazy loadingLoading related data only when it is first used, instead of together with the object that refers to it.
- Decorator patternWrapping an object in another object with the same interface to add behavior, without changing the original.
- Facade patternOne simple class in front of a group of classes, offering a few high-level methods for the common tasks.