What is an elegant way to keep track of the size of a set of objects without referencing one authoritative collection?

Update: Read this question in the context of design principles, elegance, expressing intent, and especially "signals" sent to other programmers of design choices.

I have two "views" of a set of objects. One is a dictionary / map that indexes objects by string value. The other is a dictionary / map indexing objects in order (ordinal integer). There is no "master" collection of objects that can serve as an authoritative source for the number of objects, but two dictionaries should always contain references to all objects.

When a new item is added to the set, the link is added to both dictionaries, and then some processing needs to be done that is affected by the new total number of objects.

What should I use as an authoritative source for referencing the current size of a set of objects? It seems that all my options are wrong in one dimension. I can just consistently refer to one of the dictionaries, but that will codify the consequences of this word's superiority over the other. I could add a third collection, a simple list of objects, that will serve as an authority list, but this increases the redundancy. Keeping a counting account seems simpler, but it also increases redundancy and is more fragile than accessing an introspective collection on the fly.

Is there another option that will allow me to avoid choosing the lesser evil, or will I have to accept the elegance tradeoff?

+1


source to share


4 answers


I would create a class that has (at least) two sets.

  • The version of the collection that is sorted by string
  • Version collection, which is sorted in order
  • (Optional) Master Collection

The class will handle handling the nitty gritty:



  • Synchronizing content for collections
  • Standard collection actions (for example, allow users to get size, add or retrieve items)
  • Let users get a string or sequence number

This way, you can use the same collection wherever you need either behavior, but abstract away from the "indexing" behavior that you intend to use.

A separate class gives you one interface with which you can explain your intentions regarding the use of that class.

+3


source


I would suggest encapsulation: create a class that hides the details of the control (such as the current count) and use it to display immutable "views" of the two collections.

Clients will ask for a "manglement" object for an appropriate link to one of the collections.

Clients adding "term" (for lack of a better word) to collections will do so through the "manglement" object.



This way, your assumptions and implementation options are "hidden" from service clients, and you can record that the choice of collection for size / count was arbitrary. Future maintainers can change the way the account is managed without disrupting customers.

By the way, I meant "manglement" - my favorite malapropism for management (in any context!)

+3


source


If both dictionaries contain references to each object, the score should be the same for both of them, right? If so, just pick one and be consistent.

0


source


I don't think this is very important. Just refer to the kits in the same order each time you need to access them.

If you are really concerned about this, you can encapsulate collections with a wrapper that provides public interfaces - for example,

  • Add (item)
  • Count ()

This way it will always be consistent and atomic - or at least you can implement it that way.

But I don't think this is very important.

0


source







All Articles