13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and OutputSo What's a Delegate???output classes we're already seen, but each takes another input/output class as anargument to its constructor. We'll refer to these other classes as delegates, classesthat we pass the input or output request to. In particular,• outputDelegate - this is a class whose write() method a filter calls when thefilter is asked to write()• inputDelegate - this is a class whose read() method a filter calls when thefilter is asked to read()Delegation is the formal term for "passing the buck". Rather than completelyperforming a task yourself, you ask someone (or something) else to perform partof all of that task for you. Let's take a simple, and very common, example.Nancy asks her husband Scott to clean the house while she goes to the market.Scott, being a brilliant (read "lazy") programmer, decides it would be a good todelegate the task, so he asks each of his kids to clean part of the house. (In return,he promises to take them to Toys R Us to get a set of Legos.) The kids do adecent job cleaning up, but not perfect. Before Nancy returns home, Scott "fixes"things a bit, such as placing the sheets on the bed instead of the sofa.Let's analyze what happened in this scenario:• Nancy asks Scott to clean the house. Think of this as "the Nancy objectsends a cleanHouse message to the Scott object."• Scott asks the kids to clean the house. Here's the delegation in action. TheScott object has references to his kid objects (he'd better or the Nancy objectwill get upset), and when sent a cleanHouse message, he sends cleanHousemessages to those kid objects. These kid objects are known as delegates,because Scott is asking them to do a job he is responsible for.• The kids each clean part of the house. A common use of delegation is todivide a task into smaller, more manageable pieces. Each piece performs itstask in its own way, and the end result is that the overall task is performed.• Scott "fixes" things. Often, delegates don't do the exact job that you need,and you need to "tweak" the results, or perhaps combine the results somehow(perhaps sweeping all the individual dust piles the kid objects created underthe throw rug in the bathroom).Java Input and Output -18© 1996-2003 jGuru.com. All Rights Reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!