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
Begin by examining the existing
Image
,RealImage
, andProxyImage
classes, and understand how the Proxy design pattern is implemented.Add a
delete()
method to theImage
interface and its implementing classes. In theRealImage
, thedelete()
method should simulate deleting the image from the disk by printing a message such as "Deleting image file: [filename]".In the
ProxyImage
class, enhance thedelete()
method to first check if theRealImage
object has been created or not. If it has, call thedelete()
method. If not, print a message indicating that the image file cannot be deleted because it hasn't been loaded yet.Update the
ProxyDemo
class to demonstrate the use of the newdelete()
method for both loaded and not yet loaded images.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
throughProxyImage
.