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.
The decorator pattern is used in java.io classes when you manipulated input/output streams (and the same applies for readers and writers).
inputstream, bytearrayinputstream, stringbuilderinputstreams and so on are based elements. Filterinputstream is the base class for the decorator classes. Filter input streams (such as bufferedinput stream) can do additional things when they read streams or write to them.
They are built by encapsulating a stream, and are streams themselves.
I can't think of any class implementing this pattern in java.net, but I think your were told about this package as it is strongly tied to java.io (socket.getInputStream for instance).
Actually, here is a course from O'Relly that explains how decorator is implemented in java.io.
Regards, Stéphane
The decorator pattern is used to add functionality to existing objects such as a class defined in a library. You can then "decorate" it to fit your needs. If you are interested in learning more about patterns I recommend "Design Patterns" by the Gang of Four.
A - Decorator Pattern
A.1 - Use Case of Decorator Pattern
Decorator pattern is used for extending a legacy functionality without changing the legacy class. Let's say, we have a concrete class that implements an interface. And we need to extend the functionality of the existing method however because that the existing class, and its methods are already used by other classes, thus we don't want to make a change in the existing classes. But we also need extended functionality on newer class, then how do we solve this problem?
So we use decorator pattern, wrap the existing class inside the decorators.
B - Basic GoF Decorator Pattern Example
Here we have a simple interface and an implementation/concrete class. The interface has one simple method, which is
getMessageOfTheDay
and it returns aString
. Assume that there are lots of other classes using this method. So if we want to make a change in the implementation/concrete class, it will affect the old legacy code. We want to change it for only the new classes so we use the decorator pattern.Here is a trivial example of Gang Of Four Decorator Design pattern;
B.1 - Greeter.java
B.2 - BasicGreeter.java
B.3 - Abstract Decorator Class: GreeterDecorator.java
B.4 - Concrete Decorator Class: StrangerDecorator.java
B.5 - Demo Code: DecoratorDemo .java
Take a look at those examples. The abstract decorator class is needed to wrap the original contract and implementation. Using the abstract decorator, you can create newer multiple decorators but in this example, BasicGreeter is wrapped inside the abstract decorator and we have only created on new decorator class which is StrangeGreeter. Please notify that decorator classes can be used like a train, we can wrap a decorator inside another decorator or the same. The functionality is extendable but the original class is preserved without any modification.
C - OutputStream Demo
Let's take a look at this example. We want to write a string to file with OutputStream. Here is the demo code;
C.1 - Sample OutputStream Demo To Write A File
C.2 - JSON Decorator Output: normal.txt
There will be a new file with name "normal.txt" created under the project folder and the content will be;
D - JSON OutputStream Decorator Demo
Now, I want to create a JSON wrapper format, which is as follows;
What I want is to write the content inside a simple one field JSON format. How can we achieve this goal? There are many trivial ways. However, I will use the GoF Decorator Pattern by writing a JSONDecorator which extends the OutputStream class of Java;
D.1 - JSON Decorator for OutputStream: JSONStream.java
D.2 - JSON Decorator Demo: JSONDecoratorDemo.java
D.3 - JSON Decorator Output: json.txt
Actually, OutputStream itself a Decorator Pattern, it is the abstract decorator and concrete decorator in here is the JSONStream class.
One way you can decorate an input/output stream is to apply compression/decompression to it. See the classes in
java.util.zip
, for example. Such a decorated stream can be used exactly the same way as a "regular" input/output stream, with compression/decompression performed totally transparently.Well, I may be late to the party but this question never gets old. The key point to understand Decorator is that it gives you the ability to plug an object to an existing object to another existing object and so on. It is popular to implement this pattern in a constructor. For example,
If you look at the diagram in the wikipedia, you would see ConcreteComponent and Decorator inherit from the same superclass/interface, Component. That is, these two classes have the same implement method(s).
However, in the Decorator class, you'd see an arrow back to the Component, which means you use Component somewhere in the Decorator class. In this case, you use the Component as a datatype of a constructor in the Decorator. That is the big trick. Without this trick, you won't be able to plug a new object to an existing object.
After that, you can create subclasses inheriting from the Decorator class. Because all classes have the same root, every single classes can freely plugin without any order.
In .NET, there are a bunch of stream decorators, like BufferedStream, CryptoStream, GzipStream, etc. All those decorate
Stream
class.