Class Design for Delete Photo


#1

I had no real problems with the challenge like veganjay, but do have a question about the “right” way to structure which classes perform the actual delete and how once it is selected by the user.

Crime offers setPhoto and getPhoto methods. veganjay and I in my first solution that works put all the file deletion logic in CrimeFragment - this includes the file deletion activity, invoking setPhoto(null) to remove the Photo from the Crime.

However I think from an OOP perspective numerous things are “wrong” here and need advise on the “best practice” approach re:OOP.

  1. The Photo object which was created when the picture was created which contains the filename is not deleted at all. There is no “delete” or destructor for the Photo class and so the memory consumed by the Photo object is never freed.

  2. Generally I also think the code that performs the deletion of the file in storage and the code that should remove the Photo object should be in the Photo and/or Crime classes. One could implement Crime.deletePhoto() and do all the legwork there including setting Crime.mPhoto=null, invoking a Photo destructor, and freeing up the file storage.

OR one could actually do the removal of the file on disk in a destructor/delete method of the Photo class which would be invoked by Crime.deletePhoto or Crime.setPhoto§ when Crime.mCrime != null on a setPhoto invokation.

So my questions are:

  1. Which flavor of the above is “right” according to the lore of OOP? Shouldn’t the file handling be abstracted to the Crime at least?
  2. How would one go about deleting the Photo object? Or should I just not worry about this since the last reference to it is set to null in Crime and thus it becomes eligible for garbage collection? The only references to the Photo object are the local object used when it was created when the photo was taken which should disappear when the local frame exits, and the one internal to Crime which persists.

Thanks in advance.


#2

This discussion is unfortunately a little beyond the scope of what we can effectively address here in the fora, especially because design questions are always subject. Having said that…

1- The code in question should definitely go in the model layer somewhere. I’d recommend having it in the Photo class, using a delete() method which would need to be called by convention. That’s not the only solution that could work, though.

2- Just set it to null and let it get GC’d.

If you’re poking around this kind of thing, you’ll probably run into finalizers at some point. If you’re interested in them, I strongly recommend researching their drawbacks. There are enough drawbacks that people don’t generally like to use them.