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.
This one looked promising:
http://en.wikipedia.org/wiki/Composite_pattern
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?
Thanks in advance!!