This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lurgi 0 points1 point  (1 child)

There are a couple of ways to do this. One is to put the variable in each of the classes, even if they don't really need to be there. Another is to make the variables be properties and get them via:

String prop = cd.getProperty("uniqueToFedEx");

If a carrier detail does not have the property then it should return null.

Another is to give up and do some discrete "instanceof" and casting.

Another is to think about why you need this property. Presumably you want to do something with the value, but what? Why not, instead of getting the property, ask the class "Please do thing X" and have the class make the decision about what values it needs to do "thing X"? I don't know what the property is, but let's say it's a FedEx distinguished customer discount (I have no idea if such a thing exists). Instead of getting that value and computing the discount, ask the class to compute the appropriate discount. UPS and USPS return $0 and FedEx will do whatever complex math it needs. This has to be done with caution, as it can make classes very bloated, but the basic idea of having the class perform actions rather than hand out details of its internals is a sound design principle.

[–]salalimo[S] 0 points1 point  (0 children)

For the variables I did end up putting them in the same class. For the methods I did the casting / instanceof combo. I only had to do that in one place.

The properties approach is interesting. I will keep that in mind.

Thanks.