4

Seeing this UML class diagram :

enter image description here

FileHandler, Uploader and Deleter are abstract classes. The four bottom classes are implementations. They extend on FileHandlerInterface which requires a handle method to validate the contract. But this method isn't in the implementations, it is in their parents (Uploader & Deleter).

I did this because I didn't like the idea of implementing an interface in an abstract class.

Although, I feel like there's a huge flaw in this design. The fact that it is needed to go through parents to know how the child works makes me feel like it's bad design.

To be more precise, it is unclear that UploaderComplete's direct parent has a method "handle" even thought it needs it. It could be in Uploader's parent (and recursively).

Where is this design wrong and how could I improve it ?

Thanks.

9
  • 4
    “I didn't like the idea of implementing an interface in an abstract class”: why? May 30, 2016 at 12:27
  • i would have the abstract classes implement the interface. but i don't think your design is 'bad'
    – Ewan
    May 30, 2016 at 12:32
  • 1
    What does handle actually do? May 30, 2016 at 13:33
  • @MainMa Because to me it's not explicit that children will be the one defining the interface implementation which is kind of sad since it should help understanding what this class is and does, isn't it ? May 30, 2016 at 14:45
  • @BenAaronson this is where it gets fuzzy. Handle will call $this->upload() after using a Validator and a directory generator. $this->upload that is to be defined in children. May 30, 2016 at 14:45

1 Answer 1

6

FileHandler should implement FileHandlerInterface. Abstract methods common to all children such as handle() should be hoisted to FileHandlerInterface if public, FileHandler if not.

The purpose of abstract classes and interfaces is to define the contract for concrete classes and allow polymorphism. If I am handed FileHandlerInterface I should be able to do whatever I need regardless of the actual implementation.

Based on the diagram in the question, it looks like polymorphism will work as expected if you are passing around FileHandlerInterface references.

However, your hierarchy is brittle. If you make a change in one part of the hierarchy, it may cause inconsistencies with other parts of the hierarchy. Furthermore, it is possible to define another subclass of FileHandler but you might forget to add FileHandlerInterface. The fact that it is possible to create a new file handler that is not effectively a file handler tells me that hierarchy is incorrect and FileHandler must implement FileHandlerInterface.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.