| 1 | public class ImageProxy implements Image { |
| 2 | private String fileName; |
| 3 | private HighResolutionImage realImage; // RealSubject |
| 4 | |
| 5 | public ImageProxy(String fileName) { |
| 6 | this.fileName = fileName; |
| 7 | System.out.println("ImageProxy: Created for " + fileName + ". Real image not loaded yet."); |
| 8 | } |
| 9 | |
| 10 | @Override |
| 11 | public String getFileName() { |
| 12 | // Can safely return without loading the image |
| 13 | return fileName; |
| 14 | } |
| 15 | |
| 16 | @Override |
| 17 | public void display() { |
| 18 | // Lazy initialization: Load only when display() is called |
| 19 | if (realImage == null) { |
| 20 | System.out.println("ImageProxy: display() requested for " + fileName + ". Loading high-resolution image..."); |
| 21 | realImage = new HighResolutionImage(fileName); |
| 22 | } else { |
| 23 | System.out.println("ImageProxy: Using cached high-resolution image for " + fileName); |
| 24 | } |
| 25 | |
| 26 | // Delegate the display call to the real image |
| 27 | realImage.display(); |
| 28 | } |
| 29 | } |