Pattern · Core

Proxy

A stand-in that implements the same interface as a real object and CONTROLS access to it — to defer expensive work, check permissions, cache, or reach a remote object.

Asked atAmazonGoogleCloudflare
step 1 / 7
1/2Same interface, a stand-in
controls
«interface»
Image
+ display()
RealImage
- 5 MB bitmap
+ display()
ImageProxy
- real: RealImage
+ display()
Same interface, a stand-in
1interface Image { void display(); }
2class RealImage implements Image {
3 RealImage(String f){ /* load 5 MB bitmap */ }
4 public void display(){ /* paint pixels */ }
5}
6class ImageProxy implements Image {
7 private RealImage real; // the controlled subject
8 public void display(){ real.display(); }
9}
State
RealImageloads 5 MB on create

Image is the interface clients depend on. RealImage is heavy — it loads a 5 MB bitmap up front, the moment it exists.