Mid From PDF GoF Patterns Gang of Four Patterns

Proxy (ProxyImage):?

  • 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
{
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.

More from Design Patterns in C#

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details