Suppose you want to share some data that one object produces with another object as the consumer. How would you go about doing that?
If you took a straightforward approach, you might have Producer call
consumer.method("data")
and pass it the data that way. On the other hand, you could have Consumer get the data it needs by requesting it from Producer with something like this:
myData = producer.getData()
.
However, perhaps Producer and Consumer shouldn't know anything about each other. Then you might introduce an Intermediary that gets the data from Producer and passes it to Consumer with something like
consumer.myData = producer.getData()
Now if you want to get really creative, you could make Producer write its data to an XML file, and then have Consumer read the data from there.
But why?
Disagreements and horror stories are encouraged below.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
Or you could use a space based architecture - something like Rinda or JavaSpaces. I believe that would be much more effective and cleaner than writing out to XML and reading back in the file with the other object.
Posted by Josh
on Jan 23, 2008 at 06:39 PM UTC - 6 hrs
I think that might be a little complex for this scenario, but if you feel differently, please tell! =)
Posted by
Sammy Larbi
on Jan 23, 2008 at 07:37 PM UTC - 6 hrs
never needed to do it, but i am sure you can do this with memcache (depending on the size).
Posted by shag
on Jan 24, 2008 at 12:36 PM UTC - 6 hrs
I'd expect writing to a file, and then reading from it in the same program to be due to the consumer predating the producer in the software lifecycle, so it just read from a file before the producer was brought in to generate the data dynamically, or that the XML is used for diagnostics, or it was a known (easy) way to package the data so may as well save it to disk for diagnostics.... Otherwise having the parser and generator are overkill. Only other thing is: did they want to keep all that stuff out of memory during the middle bit?
Or possibly the full data marshalled to the XML file is too big to read or write all at once. And is another process, possible instance of this program, reading and writing the file, and (assuming some locking strategy), the change might be important to be picked up?
An example would be something that invoked with some options writes queue files to a directory, and with other options reads and executes the jobs in the queue. uucp and lp have been like that on systems I've used, and its the model sendmail uses.
Posted by hgs
on Jan 25, 2008 at 03:35 AM UTC - 6 hrs
Go direct, especially if the data is already coupled (e.g. a name has an address).
Most attempts at decoupling simply move the problem to a different level, since in order for object A to use object B's data object A must know the structure of the data it's receiving anyway (address, city, st, zip, etc.).
Posted by
Michael Long
on Jan 27, 2008 at 12:30 AM UTC - 6 hrs
Leave a comment