Skip to main content

Posts

Showing posts from October 5, 2019

Decorator Design Pattern In Java

Greetings!

Decorator desgin pattern is a structural design pattern.
This is a pretty standards pattern in Java, especially in code related to input/output classes such as FileReader, BufferedReader.

Full source code for this blog post is at my github account. [source-code]

GoF definition.
"Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality."


This is very powerful pattern to add extra funtionality to an object through composition. Interestingly, we are doing it at runtime.
This gives us an alternative to subclassing.

"Subclassing adds behaviour at compile time, decorator adds behaviour at run time."
When we use inheritance to add additional functionality, we may end with long heirarchy with too many classes. With decorator design pattern, we only need to create base and additional functionality can be added by decorating that base.
This is sometimes called as wrapper. Term …