Hey everyone. This seems like a great forum. I'm looking forward to getting involved.
Now, this is the problem that brought me here via Google: My app is recording updates from web services. Updates can be of various types.
I want to find a nice way of storing all these updates so that, at any time, I can iterate over the entire set (treating different types polymorphically), or just over one specific subtype of event. For example, I might want to look through everything that's come in and throw away old updates. Or I might want to look through just the updates of type MessageUpdate and read the messages (or some other behaviour provided by the subclass).
So lots of questions then come up. I'm thinking of an abstract class 'Update' with a basic implementation, extended by various concrete <specificTypeOf>Update -type classes. Is this the right way to handle the polymorphism?
Then I'm thinking I should create a class to contain a bunch of separate collections. The two obvious problems here are that adding a new subtype of Update requires me to add code to that class: a new individual collection type, AND new code to iterate over that collection along with all the others.
The second problem might be mitigated if I created a "collection of collections" and just iterated over all of them. I have provided some basic example code:
Well, this strikes me as a bit wrong, somehow, and I have been looking for ways to apply various design patterns to smarten the design up a bit.
But Composite seems to be most suited for an arbitrarily deep tree of similar items, and so it doesn't map neatly onto a problem. I think the nature of my problem is a shallow tree, with several different types of things in the tree.
Anyway, is all this making sense?
Does anyone have any suggestions for ways to refactor this code?
Without knowing more I'd probably either just use a map of updateType => updates, or do a select-by-updateType on a collection containing all updateTypes.
posted 9 years ago
I'm afraid I don't follow completely.
Could you provide a little more detail on how the map might solve the problem? Would you store the Updates or various collections of updates in the map?
In your second solution, would you iterate over all Updates to do the select? How would you pick out the specific subtypes of Update that actually could perform the behaviour we want? I don't like using 'instanceof()', and I'm not sure if that's what you're suggesting.