• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How would you store subtypes in a general collection that can be sliced by subtype?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gareth Lloyd
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I appreciate the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!