I’m writing a bunch of data out from a java application that gets consumed by the end user it could be to a file, console, or an arbitrary listener. It would be nice to allow the user to specify how they want to consume this data. What approach have people taken to this sort of problem, is there a good open source solution? I could see something as simple as just using log4j with different appenders etc…
Share
Think about the form that your output data takes, and potentially what response object(s) your application needs back, and then select or create an interface that fulfils these requirements.
At the most abstract level you’ll be spewing out a bunch of bytes, so an
OutputStreamis certainly the correct interface for a “byte receiver”. One level of abstraction higher and you might be outputting text characters, in case aWriteris appropriate. If you’re passing domain objects around then you’ll possibly want something a bit more specific.However I suspect that your interface doesn’t need to be much more complicated than
possibly with a return type, if your provider cares about that sort of thing (e.g. an int to record the number of entities processed). There are undoubtedly lots of interfaces in open source projects that fit a similar pattern – after all, there’s only so much variation you can have in an interface with a single unary method – but nothing springs to mind as being the “one true version” of this pattern. Besides, it’s really easy to understand and since you don’t have to worry about the implementation there’s almost no downside to just rolling this yourself in about 60 seconds.