Mid
From PDF
GoF Patterns
Gang of Four Patterns
Proxy (ProxyImage):?
Short answer: The ProxyImage class also implements the IImage interface and controls access to the RealImage. It is responsible for lazy loading the real image only when needed (i.e., the first time Display() is called). public class ProxyImage : IImage
Example code
{
private readonly string _filename;
private RealImage _realImage;
public ProxyImage(string filename) => _filename = filename;
public void Display()
{
if (_realImage == null)
{
_realImage = new RealImage(_filename);
} _realImage.Display(); }
} Behavior: The proxy holds a reference to a RealImage and initializes it only when the Display() method is called for the first time. This delays the loading of the image, making it more efficient if the Display() method is not called frequently.
Real-world example (ShopNest)
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png