AlgoMaster Logo
AlgoMasterImplement a Lazy Image Proxyeasy

Implement a Lazy Image Proxy

easy

Large images are expensive to load, but a gallery often needs only a lightweight preview first. A virtual proxy can stand in for the real image, remember its file name, and delay creating the expensive object until the image is actually displayed.

The starter code already provides:

  • ImageResource, the contract shared by the proxy and the real image.
  • RealImage, whose construction records one expensive load.
  • LoadTracker, which exposes how many real images were created.
  • ProxyDriver, the public test harness that creates and forwards calls to your proxy.

Your task is to implement only LazyImageProxy.

LazyImageProxy behaves as follows:

  • LazyImageProxy(String fileName) stores the file name without creating a RealImage.
  • String preview() returns "Preview: <fileName>" without loading or displaying the real image.
  • String display() creates the real image on the first call, stores it, and delegates to its display() method. Every later call reuses the same object.
  • boolean isLoaded() reports whether the real image has been created.
  • int loadCount() reports how many real images the tracker saw.
  • int displayCount() reports how many times the proxy's display() method was called.

The examples use ProxyDriver operations because it is the public test harness. Do not modify or recreate the supplied types; your code changes belong only inside LazyImageProxy, and it must not construct RealImage until display() needs it.

Example 0:

Input:

Output:

Explanation: A preview uses only the file name already held by the proxy. The real image is still absent, so neither the load count nor the display count moves.

Example 1:

Input:

Output:

Explanation: The first display constructs the real image and delegates to it. The second display reuses that same object, so displays reach 2 while loads remain 1.

Constraints

  • 1 <= fileName.length <= 40
  • File names contain letters, digits, periods, underscores, and hyphens only.
  • At most 100 calls are made across all methods.

Starter Code

Only LazyImageProxy is unfinished. The supporting types and ProxyDriver test harness are pre-implemented; do not modify them.

How the design is graded

needs 7/10 to pass
  • Proxy and real image share one contract

    Full marks when LazyImageProxy and RealImage implement ImageResource and display is produced by delegating through that contract. Lose points heavily when the proxy builds the display result itself or does not implement the shared interface.

  • Loading is genuinely lazy

    Full marks when construction and preview leave the real image absent, the first display creates exactly one RealImage, and later displays reuse it so loadCount remains 1. Lose points when loading happens in the proxy constructor or on every display.

  • Proxy-only behavior stays separate

    Full marks when preview never loads or displays the real image, displayCount moves once per display call, isLoaded reflects whether the real image exists, and methods return values rather than printing. Lose points when preview changes either counter.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new ProxyDriver("forest.png")null
preview()"Preview: forest.png"
isLoaded()false
loadCount()0
displayCount()0

A preview uses only the file name already held by the proxy. The real image is still absent, so neither the load count nor the display count moves.

Run checks these cases. Submit also runs a larger hidden set.