dimanche 30 novembre 2014

Specific empty children classes


Is having empty children classes, just to specify, a bad practice?


Lets suppose I've a generic Product class. There are electrical, electronic and mechanical products. I need to represent all them and must be able to differ each one in some operations. I've two options:




  • add a member named "type" in Product class to specify whether it's a electrical, electronic or mechanical product.




  • create children classes of Product, one for each kind of product: ElectricalProduct, ElectronicProduct and MechanicalProduct. As these classes don't differ from each other in methods or members - already defined in parent class - they would be all empty.




I consider second option a better approach, but is it the right way? I've never seen such thing on any application or library.


What do you thing? Any suggestion?





Aucun commentaire:

Enregistrer un commentaire