Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6020115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:31:16+00:00 2026-05-23T03:31:16+00:00

I have read in wikipedia that Decorator pattern is used for .Net and Java

  • 0

I have read in wikipedia that Decorator pattern is used for .Net and Java IO classes.

Can anybody explain how this is being used? And what is the benefit of it with a possible example?

There is an example of Windows forms on wikipedia but I want to know how it happens with Java IO classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:31:17+00:00Added an answer on May 23, 2026 at 3:31 am

    InputStream is an abstract class. Most concrete implementations like BufferedInputStream, GzipInputStream, ObjectInputStream, etc. have a constructor that takes an instance of the same abstract class. That’s the recognition key of the decorator pattern (this also applies to constructors taking an instance of the same interface).

    When such a constructor is used, all methods will delegate to the wrapped instance, with changes in the way the methods behave. For example, buffering the stream in memory beforehand, decompressing the stream beforehand or interpreting the stream differently. Some even have additional methods that finally also delegate further to the wrapped instance. Those methods decorate the wrapped instance with extra behaviour.

    Let’s say that we have a bunch of serialized Java objects in a Gzipped file and that we want to read them quickly.

    First open an inputstream of it:

    FileInputStream fis = new FileInputStream("/objects.gz");
    

    We want speed, so let’s buffer it in memory:

    BufferedInputStream bis = new BufferedInputStream(fis);
    

    The file is gzipped, so we need to ungzip it:

    GzipInputStream gis = new GzipInputStream(bis);
    

    We need to unserialize those Java objects:

    ObjectInputStream ois = new ObjectInputStream(gis);
    

    Now we can finally use it:

    SomeObject someObject = (SomeObject) ois.readObject();
    // ...
    

    The benefit is that you have a lot of freedom to decorate the stream using one or more various decorators to suit your needs. That’s much better than having a single class for every possible combination like ObjectGzipBufferedFileInputStream, ObjectBufferedFileInputStream, GzipBufferedFileInputStream, ObjectGzipFileInputStream, ObjectFileInputStream, GzipFileInputStream, BufferedFileInputStream, etc.

    Note that when you’re about to close the stream, just closing the outermost decorator is sufficient. It will delegate the close call all the way to the bottom.

    ois.close();
    

    See also:

    • Examples of GoF Design Patterns in Java's core libraries
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can
I have read from the Wikipedia that: References cannot be null, whereas pointers can;
I have read on wikipedia However, symmetric ciphers also can be used for non-repudiation
I have read a lot that LISP can redefine syntax on the fly, presumably
Could anybody explain in plain words how Cloud computing works? I have read the
Actually I have read about that I can write Objective-C app on Linux (using
I have read that HttpURLConnection supports persistent connections, so that a connection can be
I have read the GOLD Homepage ( http://www.devincook.com/goldparser/ ) docs, FAQ and Wikipedia to
I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.