Using of which design pattern is demonstrated by the following code:

FileInputStream in = new FileInputStream("test.dat");
BufferedInputStream bin = new BufferedInputStream(in);
Explanation
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.
See more on stackoverflow.com

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback