From the course: Java Design Patterns: Structural

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: The Decorator pattern

Solution: The Decorator pattern - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Solution: The Decorator pattern

(upbeat techno music) - Let's take a look at my solution to the exercise. As usual, yours might be a bit different to mine and that's fine. So I've created a class called PizzaWithExtraCheese, which implements the pizza interface. This class has two fields an array list called toppings and a pizza called pizza in the constructor and passing in a pizza objects. And I'm assigning that to the pizza field. Then I'm setting the toppings field using the get toppings method on the pizza objects. Finally, I'm adding another topping, extra cheese, to the list of toppings. Because this class implements pizza, I also need to have methods called get toppings and get name. In the get toppings method I'm returning the toppings field and in the get name method, I'm returning pizza don't get name to get the name of the pizza objects I passed in inside the constructor. I'll show what I've done in the main cross tube to show how…

Contents