2

A class that performs both computations and data logging* seems to have at least two responsibilities. Given a system for which the specifications require heavy data logging, what kind of design patterns or architectural patterns can be used to avoid bloating all the classes with logging calls every time they compute something?

The decorator pattern be used (e.g. Interpolator decorated to LoggingInterpolator), but it seems that would result in a situation hardly more desirable in which almost every major class would need to be decorated with logging.

*Note: When I wrote this, I was working in a context in which it was called logging, but it was actually writing to a database. I'm not particularly referring issuing debug or error messages in a log4net log or the like.

Kazark
  • 1,810
  • 1
  • 17
  • 37
  • 2
    possible duplicate of [Is logging next to an implementation a SRP violation?](http://programmers.stackexchange.com/questions/275957/is-logging-next-to-an-implementation-a-srp-violation) – Dan Pichelman Mar 11 '15 at 12:58
  • Why the heck is my question, which is more than two years old, marked as a duplicate of a brand new question? And why am I given no other options but "That solved my problem!" (when I'm not even experiencing the problem anymore--I don't even work at the same place) or "No, my question is different. I will edit to to explain how." Why the heck do I have to explain how my question is different than a brand new question!!?! – Kazark Mar 11 '15 at 13:40
  • 1
    @Kazark question age doesn't matter, see [Should I vote to close a duplicate question, even though it's much newer, and has more up to date answers?](http://meta.stackexchange.com/a/147651/165773) As for explanation, it is not "required" but only suggested - this is a [recently introduced feature](http://meta.stackexchange.com/q/250981/165773) – gnat Mar 11 '15 at 14:08
  • 1
    @Kazark The question would require 5 votes from reviewers before actually being marked duplicate. Just because somebody believes it might be a duplicate doesn't mean that it is automatically marked as such and it doesn't mean that others will agree. – maple_shaft Mar 11 '15 at 14:22

1 Answers1

2

My first thought is to treat the computational parts as a model, and the logging parts as a view. You could then manage the work flow activities of each using a controller or presenter pattern. Substitute other fancy names for each part if you wish, but I see three parts.

So, the controller would initiate the computation, retrieve the result, and relay that to the logger. The computation and logging parts stay ignorant of each other, and the high level logic of your main job is expressed in one place.

  • I'm going to go ahead and accept this answer, though I would also appreciate further answers. – Kazark Dec 24 '12 at 16:16