• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Decorator Pattern in Java  RSS feed

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problem to understand Decorator pattern and how I can use this pattern in Java.
I have went through many different sites but unable to understand it.

A good and concise example will be very handy.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahtab,

Have you ever worked with Java's I/O streams like BufferedInputStream and DataInputStream?

They are perfect examples of the decorator pattern. Any subclass of FilterInputStream (like BufferedInputStream) "wraps around" another InputStream, and "decorates" it with more functionality. BufferedInputStream gives them the ability of buffering input. DataInputStream gives a stream the ability to read primitives.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To implement the decorator pattern you need to make use of inheritance && composition...

Lets say you own an ice-cream shop and you want to create an application to assist with sales... You sell different flavours of ice-cream which can also be topped off with extra treats... If each flavour has a different cost and there is an additional cost for each extra treat, then you have to come up with a design that will calculate the cost for the sale with all possible combination of choices...

Lets create a chart to represent prices:

Flavours
Coconut: $2.99
Vanilla: $1.99

Extras
Syrup: $0.70
Sprinkles: $0.50

Someone can make an order such as 1 scope coconut + 1 scope vanilla + syrup + sprinkles... plus if your shop has over a dozen flavours and extras the combinations can be overwhelming... The easiest way to determine the cost of any combination you must use the decorator pattern where an object wraps another object of its type and adds another layer of functionality....

Lets illustrate using some code snippets::

The ice cream flavour super-class:-


The extras super-class:-


Coconut flavour:-


Vanilla flavour:-


Sprinkle extra:-


Syrup extra:-


Ice cream shop:-


Output
Flavour: Coconut + syrup + sprinkles
Cost: $4.19

Reading through the code snippets carefully you realize that all you do it wrap one object inside the other and add the extra bit of functionality that is required to produce the desired result...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:A good and concise example will be very handy.

Simplest one I can think of:The "implements" bit is simply to ensure that the class exhibits similar behaviour to the one it decorates (String), and the last 2 methods are just to illustrate that it's important to think about how they should be implemented for a "decorated" object - the above is just one possibility.

you should generally also implement toString() specifically as well.

HIH

Winston
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it .
Shukran
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!