From the course: Java Design Patterns: Structural
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Understand the Decorator pattern - Java Tutorial
From the course: Java Design Patterns: Structural
Understand the Decorator pattern
- [Narrator] The decorator pattern is a similar principle to decorating something in real life, where you have an object that's already created, like a Christmas tree or a cake, and you want to add some decorations to it. Imagine a bakery where cakes are baked and sold and the baker has different recipes for chocolate cakes, lemon cakes, cupcakes and so on. Suppose one day someone decides they want to order some cupcakes with a really specific pink icing on it. The baker wouldn't write a whole new recipe for cupcakes with pink icing. They would use the same cupcake recipe as normal but they would just add the icing on top after. A similar situation also applies in software. If you are representing this in Java code, you might have an interface called cake and beneath that the different types of cake. How would you handle the situation where you need a cupcake but with pink icing on it? One way would be to use…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.