How to handle optional variables of an object in Java?
- by Arvanem
Hi folks,
For my trading program, I have a Merchant class. A given Merchant object may or may not have a particular special quality or bundle of special qualities. For example, one Merchant object may have the Stockbroker quality, another Merchant may have the Financial Services and Stockbroker qualities, and another Merchant may have no special qualities at all.
My initial thought was to create a HashMap and a Qualities class as follows:
Map<Qualities, Boolean> merchantQualities = new HashMap<Qualities, Boolean>();
The only problem is, there are at least 50 possible special qualities for Merchants, such that it would be extremely tiresome to subclass all the qualities from the Quality class.
Is there a better way of coding for these optional special qualities and representing them in the Merchants class than a HashMap and subclassing a Qualities class?