lundi 26 janvier 2015

Best Practice for naming objects in java

I have a need to have a unique name for a subset of objects in java. Basically I have a display object and multiple displays that extend it. Each display needs to have a unique name for auditing purposes. This is so when you look at the audit you will know what display was audited. What I am looking for is how to do this as a best practice possibly using a design pattern. Here are some of my current ideas


IDEA 1 Use toString() every object already has a toString so overriding this should provide an easy way to name the objects. The down side to this is all objects have a toString so it is hard to enforce that the Display objects have theirs filled out while everything else does not need it.


IDEA 2 Use an interface. I could make an interface called IDisplayName with a function called getName(). My Display object could implement this interface so my concrete classed will need to set it. This is what I currently have implemented.


IDEA 3 Same as IDEA 2 but have getName() return an ENUM instead of a String. This way I could maybe enforce uniqueness in a single file and the ENUMs would have a displayString attribute. The problem with this is there are a lot of said Display objects >100 so this can get really big really fast.


IDEA 4 Just log the class name. This does not work well since the audit is not read by programmers so I would prefer to make the audit more friendly.


Thanks in advance for any help.


Aucun commentaire:

Enregistrer un commentaire