Using Decorator pattern in Java project

Published: 17 September 2020
on channel: Improve Your Programming skills
72
3

Assume that we manage an Ice-cream shop. There are many types of ice-cream that combine from ingredients such as orange, strawberry, coconut, milk, chocolate, cocoa and so on. This combination causes us to create hundreds of Java classes.

Decorator pattern help us solve this issue. We don't have to create too many Java classes but it is possible to create many types of ice-cream objects at runtime

Steps to implementation
1. Create an abstract class named AbstractIceCream
2. Create a concrete IceCream class. We will create many types
of ice-cream objects from this class
3. Create an IceCreamDecorator. This is an abstract class that
provide a flexible alternative to sub-classing for
extending functionality.
4. Create a MilkIceCream class that extends IceCreamDecorator
5. Create a ChocolateIceCream class that extends IceCreamDecorator
6. Create a CoconutIceCream class that extends IceCreamDecorator
7. Combination of 4 classes:
IceCream, MilkIceCream, ChocolateIceCream, CoconutIceCream
We can create many type of ice-creams objects such as IceCreamWithMilk, IceCreamWithChocolate,
IceCreamWithChocolateAndCoconut, IceCreamWithCoconut, IceCreamWithMilkAndCoconut, IceCreamWithMilkAndChocolate, .....

So we only need 4 classes to create so many types of ice-cream objects. This is the strong point of Decorator pattern


On this page of the site you can watch the video online Using Decorator pattern in Java project with a duration of hours minute second in good quality, which was uploaded by the user Improve Your Programming skills 17 September 2020, share the link with friends and acquaintances, this video has already been watched 72 times on youtube and it was liked by 3 viewers. Enjoy your viewing!