0

I have a class, Thing, which has a number of variables. I also have a class, FileHandler, which records data to log files.

The FileHandler is specific to the instantiation of a Thing and needs to print several of the instance variables of Thing. So, Thing is imported into FileHandler.

There is another class, OtherThing, which also needs a Filehadler. Unfortunately, Thing imports OtherThing for reasons specific to the nature of Thing and OtherThing. This means that FileHandler imports Thing and Thing imports OtherThing which in turn imports FileHandler.

I can't seem to pull these apart. I will add more to this post depending on the comments. Is there an obvious reason that I cannot separate my classes here? It seems to be due to the fact that there is common data, that is needed by three classes.

3
  • Is any of this a problem? Commented Oct 30, 2013 at 15:30
  • 1
    Please show the relevant code for Thing, OtherThing, and FileHandler. The problem may be language-agnostic from a design point of view, but it will be easier to provide suggestions with a concrete example. Commented Oct 30, 2013 at 15:35
  • 1
    Being able to see (the pertinent parts of) the code might be somewhat useful... Commented Oct 30, 2013 at 15:36

1 Answer 1

1

Following this wording...

"All problems in computer science can be solved by another level of indirection... Except for the problem of too many layers of indirection." [David Wheeler]

...I would suggest to add an interface. Without knowing the exact code it's hard to decide. Maybe you try to introduce IThing or IFileHandler.

Sign up to request clarification or add additional context in comments.

1 Comment

I fixed this problem by creating a new class that contained the common fields. Now Thing and OtherThing and Filehandler call this new class instead of calling each other. I just needed a more appropriate home for the fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.