Paul Clapham wrote:
Also, you're using a static Map to store the data, which means you've sort of written a Singleton object but don't have the proper controls, and you've created that map in the Attribute class and not in the AttributeList class where I think it belongs. So creating two of those objects (whichever class has the static variable) is going to lead you into trouble.
I tried to simulate enum. I want to use this class in different layers of the app which will communicate via simple messaging interface.
This is not singleton since it is expendable. However, I enforce "singleton property" for each attribute. I use combination of readResolve(), protected constructor and builder to enforce single instance of the attribute of a particular id.
Since I want users to extend it before use, it should be a class and not part of the AttributeList.
Also I think it's a bad practice to name something "SomethingList" when it uses a Map to store things. Calling it a "List" leads the user to believe he/she can add as many Dates as he/she likes, when it doesn't actually work that way.
I totally agree to that. Will rename ASAP.
My sample may have been misleading. I will have a message that will contain attributes. The messages will be exchanged through a message broker between different components of the app. I want complete decoupling and have similar system to NotificationCenter of Objective C.
Your container will work but has some limitations. I have to store this container in some sort of the collection.
I may have two containers with the same type. For example start Date and end Date. How will consumer distinguish them without extra information (some tag may be)?
On the other hand, we can create a class per type (similar to what I do with attribute). This may lead to a maintenance issue. Also, I will need some construct for this containers to make all of them immutable/mutable. I would like to keep it simple and use a dictionary where anyone can put/get data. This concept is easy to understand and use but will lead to type safety issue. For example, if I use a map and decide to use String for data while consumer was expecting another object, the issue will arise at run time only. I want to know about it at compile time and not hear about it from production env.