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
Auf dieser Seite können Sie das Online-Video Using Decorator pattern in Java project mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Improve Your Programming skills 17 September 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 72 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!