2

I have a class that creates StringWriters and passes them around. A calling class will write to that Writer. Is there any way for me to put some sort of Listener on the Writers as they are being created to let me know when their contents change?

I tried looking online, but all I could find about Listeners were for GUIs, and this is not GUI-related.

chama
  • 5,973
  • 14
  • 61
  • 77
  • 3
    You'd have to write something like that yourself, e.g. a class that wraps a `StringWriter` and implements the observer pattern – Ismail Badawi Mar 03 '14 at 20:37
  • There's really nothing out of the box? – chama Mar 03 '14 at 20:46
  • Writing the wrapper would be quite straight forward and take minimal code, and be usable with minimal changes to existing code and be compatible with almost everything expecting a plain StringWriter. I'm guessing the code would be ~20 lines. – Charlie Mar 03 '14 at 21:03
  • Probably the closest to something "out of the box" would be [`java.util.Observable`](http://docs.oracle.com/javase/7/docs/api/java/util/Observable.html) / [`java.util.Observer`](http://docs.oracle.com/javase/7/docs/api/java/util/Observer.html) – Ismail Badawi Mar 03 '14 at 21:27

0 Answers0