I was writing some code and came across a scenario that I was thinking about doing a circular dependent class, which I have not done before. I then looked circular dependencies up and whether they are impermissible, and found that it is achievable but not desirable. But I have some dilemma regarding how to implement it without circular dependencies so I thought I should see if there are other suggestions available.
Imagine that you are building an index for a number of files, and those files have a number of attributes, including an attribute that records what files the particular file references.
Attempting to set up some classes mimicking such structure, I have written a number of classes.
subClassAincludes defining attributes of a file, let's call this attribute set A
subClassBincludes classification attributes of a file, let's call this attribute set B
fileObjectis an object representing a file, and has one
subClassAobject and one
fileSetis an object representing a particular set of files, and is essentially a collection of
As I was creating
subClassB, I realized that the information related to reference files within
subClassB is really just a simplified
fileSet with limited
subClassA information. Is it wise then to simply circular reference within
fileSet object? Or if that is a terrible idea, how should one go about storing the information? Technically, we can create another collection class object under
fileObjectthat will store a bunch of
subClassA objects, but I don't really favor that as I would need to duplicate certain functions of
fileSet within that new class definition (for example, functions that checks those objects, combines those objects, and etc).
And if I do it where I have an
object_M that includes a collection of
subClassA and another
object_N that includes a collection of
subClassB (that has an
object_M included), and a top level
fileSet that includes one
object_N, that will solve the problem, but we suddenly got a new problem of needing some way to link the objects within
object_N together somehow, another complexity in itself.
With the given scenario, should I just go with circular dependency? Or is there a better way to do it altogether?