What type of process would I need in a new class to ‘read’ all of the colors that are in this class below? I use the class to set colors on components and love it. But the time has come to allow my application to right-click on a panel and change the background color for example.
What would get me started in the correct direction code-wise in continuing to use these colors: ColorFactory.java
I was just starting to think about adding a new method to the class something like:
public Map<String, Color> getColorMapValues(){
return colorMap;
//
}
Since you have control over the source code in this case, it would be better to refactor the class to meet your needs than using reflection (which is not only expensive, but is prohibited depending on your security policy).
The technique commonly used is the
Holder pattern. See Joshua Bloch’s Effective Java (2nd Edition) Item 71 for more information. We can also avoid synchronization on reads by using a thread-safe, non blocking structure, namelyjava.util.concurrent.ConcurrentHashMap.In fact, because the parse operation is likely to be very, very fast (in comparison to the synchronization), we can do away with the synchronization altogether. So we might end up parsing the value multiple times – not really a big problem, since the result will be the same every time. See Bloch et al. Item 69 for more information.