[GitHub Repository for Code Samples]
https://github.com/sumithpuri/skp-code-marathon-phuket
Was
going through the book ‘Head First Design Patterns’, came up with my
own examples to understand them further. Try downloading the code and
see if it helps you in comprehending these in a better way.
Decorator Pattern [Sample Code]
Decorator Pattern provides an elegant way to use composition for enhancing functionality, where the result expected has direct dependency on the composed and composing class. A chain relation (via composition) or decoration can be finally used to achieve the desired output at runtime. In real-time, when the functionality of one particular product is expected to be built form a base product and various other related sub-products or fixtures, we can rely on the Decorator.
Decorator Pattern provides an elegant way to use composition for enhancing functionality, where the result expected has direct dependency on the composed and composing class. A chain relation (via composition) or decoration can be finally used to achieve the desired output at runtime. In real-time, when the functionality of one particular product is expected to be built form a base product and various other related sub-products or fixtures, we can rely on the Decorator.
Fig. 2 : Decorator (Structural) Design Pattern - Class and Sequence Diagram
[Source : Wikipedia]
PizzaWorld is the main class. Try adding more decorators and pizza base classes to see if you can get a real taste of the Decorator!
No comments:
Post a Comment