Given the following hierarchy of objects: a keyed collection of ClassA
objects, where each ClassA
object contains a keyed collection of ClassB
objects and each ClassB
object contains a keyed collection of ClassC
objects.
No suppose we start out with the root collection of ClassA
objects (called a_objs
) and we want to look for a ClassC
object with a specific key. The (pseudo) code would look something like this:
a = a_objs.get(a_key);
if (a) {
b = a.get(b_key);
if (b) {
c = b.get(c_key);
}
}
if (c) {
// do the actual work with object c
}
The code that tries to find the requested ClassC
object looks relatively simple, but can be bigger or more complex depending on the depth of the hierarchy or the kind of search that is necessary (e.g. find first ClassC
object that has a certain property). Also with regards to unit testing it would be useful to be able to write separate tests for the code that searches the ClassC
object and for the code that does the actions on the ClassC
object.
As such I think it would be best to move the search for a ClassC
object to a dedicated function:
c = findC(a_objs, <params depending on specific search operation>)
if (c) {
// do the actual work with object c
}
My question is now: where should I put this findC()
function?
I don't want to put it in the file that contains the calling code, because it will be called from multiple different files. I also don't feel like it fits in the files for any of the objects in the hierarchy (ClassA
, ClassB
, ClassC
) because they should only need to know about the piece of the hierarchy they directly maintain (ClassA
maintains a collection of ClassB
objects, but doesn't need to know that ClassB
contains a collection of ClassC
objects).
Should I create a dedicated Manager
-like object for the hierarchy and its search operations? Should it reside in some generic Utility
file somewhere?
Edit: The languague I am working in is C.
Edit: Another example of a search action would be to look for the next ClassC
object in the hierarchy, given a specific ClassC
object (specific <a_key
, b_key
, c_key
>).
Manager
orUtility
. Call them whatever you want, but yes, the idea is to put the function in some central location where it can be shared.C
, or maybe in a subnamespace likeQueries