Proxy Pattern Lab 1

Lab: Enhancing the Image Proxy Application

Objective

In this lab, you will extend the provided Image Proxy application. The existing application includes a RealImage class that represents an image loaded from disk and a ProxyImage class that lazily loads the RealImage. You will add a new feature to the ProxyImage: a method to delete the image.

Instructions

  1. Begin by examining the existing Image, RealImage, and ProxyImage classes, and understand how the Proxy design pattern is implemented.

  2. Add a delete() method to the Image interface and its implementing classes. In the RealImage, the delete() method should simulate deleting the image from the disk by printing a message such as "Deleting image file: [filename]".

  3. In the ProxyImage class, enhance the delete() method to first check if the RealImage object has been created or not. If it has, call the delete() method. If not, print a message indicating that the image file cannot be deleted because it hasn't been loaded yet.

  4. Update the ProxyDemo class to demonstrate the use of the new delete() method for both loaded and not yet loaded images.

  5. Test your application to ensure that it behaves as expected.

Bonus

If you have extra time, consider these additional tasks:

  • Add a method to rename the image file. Make sure to appropriately handle the cases where the image has been loaded or not.

Tips

  • Remember that the Proxy pattern is used to control and manage access to an object. In this case, we are managing access to RealImage through ProxyImage.