Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Why are constructors often nested when creating streams?

  1. #1
    Join Date
    Nov 2011
    Rep Power

    Default Why are constructors often nested when creating streams?

    Such as in this example:

    PrintWriter output = null;
    output = new PrintWriter(new BufferedWriter(new FileWriter("WriteText4.out")));

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Why are constructors often nested when creating streams?

    This is an implementation of the Decorator Design Pattern which allows you to increase the functionality of a class by wrapping it in another class.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Why are constructors often nested when creating streams?

    output is intended to do various things: it writes content to a file, it is able to format things (object instances and primitive values), and it uses buffering so that the disk is not being hammered for each byte being written.

    The question is how to organise these different aspects of functionality - and other functionality that you or others may dream up to do file writing tasks. You could have a BufferedFormattingFileWriter class, but that would not be very flexible. The point is that you would like to be able to later "fold in" new functionality as it is implemented.

    The mechanism chosen (sometimes called the decorator pattern) is to recognise one aspect of the functionality - the FileWriter - as rather basic. BufferedWriter adds to this by (1) delegating to FileWriter to do the actual file writing and (2) keeping a buffer so that file writing can be done efficiently.

    PrintWriter does the same thing. It (1) delegates to a FileWriter to do the actual writing and (2) is able to format things to provide the actual content to be written. In the case you give, PrintWriter is actually consructed "on top of" a BufferedWriter, but PrintWriter doesn't know or care that the underlying file writer is buffered. It is happy so long as it has any sort of file writer to which it can delegate the task of actually writing content.

    In fact neither BufferedWriter nor PrintWriter need a FileWriter, any old Writer instance will do to write the output.


    The specific syntax of nesting the constructor calls is a matter of taste. The same thing could be written as:

    Java Code:
    FileWriter fw = new FileWriter("WriteText4");
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter output = new PrintWriter(bw);
    Last edited by pbrockway2; 12-11-2011 at 05:44 AM. Reason: slow :( But my holidays started on Friday, so I can be as loquacious as I like...
    Fubarable likes this.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Why are constructors often nested when creating streams?

    I wrote a few blogs once in this forum blog section and one is about decorators (a.k.a wrappers) : here. Maybe you find it interesting.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Nested constructors: why do they work?
    By Daimoth in forum New To Java
    Replies: 3
    Last Post: 11-18-2011, 09:39 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. Cmd process streams
    By helloworld922 in forum Advanced Java
    Replies: 0
    Last Post: 10-20-2010, 08:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts