From the course: Spring: Design Patterns

Unlock the full course today

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

Decorator pattern in action

Decorator pattern in action

From the course: Spring: Design Patterns

Start my 1-month free trial

Decorator pattern in action

- [Instructor] So, we've talked at a high level about the decorator and how it's used. Let's jump into code and take a look at its actual implementation. We're going to start with our base package in scr/main/java. And we're going to create a new package. This time, called decorator. Now, one of the easiest examples to use of a decorator is the concept of a pizza. And that's what we're going to do. So, we will create a class of Name: Pizza. And we will make that class abstract. Now, we're going to add a protected string on here, called description. And then we will create a public string, getDescription method, that will return that description. Then we're going to put a public abstract method, that returns a BigDecimal, called getCost. Okay. So, now we've got our pizza. So, let's create a concrete instance of a pizza. And we will create a ThickCrustPizza. Now, we're going to extend Pizza here. And we will create a public ThickCrustPizza constructor. And now, we will implement our…

Contents