How to handle optional variables of an object in Java?
Posted
by Arvanem
on Stack Overflow
See other posts from Stack Overflow
or by Arvanem
Published on 2010-04-17T03:36:45Z
Indexed on
2010/04/17
3:43 UTC
Read the original article
Hit count: 343
java
|optional-variables
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?
© Stack Overflow or respective owner