1

00:00:00.06 --> 00:00:01.06

- [Instructor] So, we've talked at a high



2

00:00:01.06 --> 00:00:04.02

level about the decorator and how it's used.



3

00:00:04.02 --> 00:00:05.04

Let's jump into code and take a



4

00:00:05.04 --> 00:00:07.09

look at its actual implementation.



5

00:00:07.09 --> 00:00:11.06

We're going to start with our base package in scr/main/java.



6

00:00:11.06 --> 00:00:14.02

And we're going to create a new package.



7

00:00:14.02 --> 00:00:16.07

This time, called decorator.



8

00:00:16.07 --> 00:00:18.09

Now, one of the easiest examples to use



9

00:00:18.09 --> 00:00:22.01

of a decorator is the concept of a pizza.



10

00:00:22.01 --> 00:00:23.07

And that's what we're going to do.



11

00:00:23.07 --> 00:00:28.03

So, we will create a class of Name: Pizza.



12

00:00:28.03 --> 00:00:31.01

And we will make that class abstract.



13

00:00:31.01 --> 00:00:33.01

Now, we're going to add a protected



14

00:00:33.01 --> 00:00:36.07

string on here, called description.



15

00:00:36.07 --> 00:00:40.07

And then we will create a public string,



16

00:00:40.07 --> 00:00:44.09

getDescription method, that will return that description.



17

00:00:44.09 --> 00:00:48.03

Then we're going to put a public abstract method,



18

00:00:48.03 --> 00:00:52.06

that returns a BigDecimal, called getCost.



19

00:00:52.06 --> 00:00:53.05

Okay.



20

00:00:53.05 --> 00:00:55.03

So, now we've got our pizza.



21

00:00:55.03 --> 00:00:59.05

So, let's create a concrete instance of a pizza.



22

00:00:59.05 --> 00:01:01.09

And we will create a ThickCrustPizza.



23

00:01:01.09 --> 00:01:04.02

Now, we're going to extend Pizza here.



24

00:01:04.02 --> 00:01:12.02

And we will create a public ThickCrustPizza constructor.



25

00:01:12.02 --> 00:01:15.08

And now, we will implement our method of getCost.



26

00:01:15.08 --> 00:01:19.09

And we will simply return a new BigDecimal.



27

00:01:19.09 --> 00:01:24.07

In this case, we will return 15.00.



28

00:01:24.07 --> 00:01:27.06

So, now we're going to make our decorator itself.



29

00:01:27.06 --> 00:01:29.01

And in this case, the only way to



30

00:01:29.01 --> 00:01:33.05

decorate a pizza is with ingredients.



31

00:01:33.05 --> 00:01:37.01

So, we will create a class, called PizzaIngredient.



32

00:01:37.01 --> 00:01:40.05

And this will extend Pizza.



33

00:01:40.05 --> 00:01:45.02

We will create a public abstract method,



34

00:01:45.02 --> 00:01:49.00

that returns a string, called getDescription.



35

00:01:49.00 --> 00:01:51.09

'Cause we're not ready to implement that yet.



36

00:01:51.09 --> 00:01:55.03

We also need to make our class abstract.



37

00:01:55.03 --> 00:01:59.08

So, now our decorator itself is actually ready to be used.



38

00:01:59.08 --> 00:02:04.00

So, let's create my favorite topping on a pizza...



39

00:02:04.00 --> 00:02:06.04

Which is pepperoni.



40

00:02:06.04 --> 00:02:11.05

Now, Pepperoni will extend PizzaIngredient.



41

00:02:11.05 --> 00:02:16.02

We'll implement some methods here.



42

00:02:16.02 --> 00:02:20.05

And we will start out with a constructor.



43

00:02:20.05 --> 00:02:24.01

And the constructor is going to take a pizza.



44

00:02:24.01 --> 00:02:32.01

And that pizza is going to be stored on this object.



45

00:02:32.01 --> 00:02:35.09

So, in our constructor, we will call super.



46

00:02:35.09 --> 00:02:38.01

And this.pizza,



47

00:02:38.01 --> 00:02:41.02

= pizza.



48

00:02:41.02 --> 00:02:43.03

So, now we've stored it.



49

00:02:43.03 --> 00:02:44.06

So, to our description,



50

00:02:44.06 --> 00:02:48.09

we will return this.pizza.getDescription,



51

00:02:48.09 --> 00:02:50.05

Plus.



52

00:02:50.05 --> 00:02:53.08

And then we're going to say, plus pepperoni.



53

00:02:53.08 --> 00:02:55.06

So, if you remember, on our ThickCrustPizza,



54

00:02:55.06 --> 00:02:57.09

we said that was a ThickCrustPizza.



55

00:02:57.09 --> 00:03:00.04

This way, the text will make sense.



56

00:03:00.04 --> 00:03:02.02

And now, for the cost.



57

00:03:02.02 --> 00:03:06.05

So, we will return a new BigDecimal.



58

00:03:06.05 --> 00:03:10.02

And we're going to charge 1.50 per topping.



59

00:03:10.02 --> 00:03:15.09

And we will add that to this.pizza.getCost.



60

00:03:15.09 --> 00:03:19.03

And now we're good to go. So, let's create a test for this.



61

00:03:19.03 --> 00:03:21.09

We'll go down to src/test/java.



62

00:03:21.09 --> 00:03:23.09

Base package.



63

00:03:23.09 --> 00:03:28.01

Create a new package down here, called decorator.



64

00:03:28.01 --> 00:03:29.09

And in the decorator, we will create a



65

00:03:29.09 --> 00:03:34.04

new Java class, called DecoratorTest.



66

00:03:34.04 --> 00:03:36.08

So, @Test.



67

00:03:36.08 --> 00:03:39.02

Public void.



68

00:03:39.02 --> 00:03:41.06

TestDecorator.



69

00:03:41.06 --> 00:03:43.08

So, what we've done here, is we've created a pizza.



70

00:03:43.08 --> 00:03:46.05

We're putting on its description and its cost.



71

00:03:46.05 --> 00:03:49.05

Now, we're going to create a pepperoni pizza.



72

00:03:49.05 --> 00:03:52.04

And we will pass our original pizza.



73

00:03:52.04 --> 00:03:54.09

And we're going to do the same thing.



74

00:03:54.09 --> 00:03:58.03

Copy those two values.



75

00:03:58.03 --> 00:04:00.09

And now we're going to do one more.



76

00:04:00.09 --> 00:04:05.09

In this case, we're going to do a doublePepperoni.



77

00:04:05.09 --> 00:04:10.05

And this time, we're going to pass our pepperoni pizza to it.



78

00:04:10.05 --> 00:04:14.04

Copy these two statements one more time.



79

00:04:14.04 --> 00:04:16.05

And bring in the double.



80

00:04:16.05 --> 00:04:19.08

And now, we'll run our test.



81

00:04:19.08 --> 00:04:20.07

So, as you might expect,



82

00:04:20.07 --> 00:04:23.00

we created the first ThickCrustPizza.



83

00:04:23.00 --> 00:04:25.04

We then decorated it with pepperoni.



84

00:04:25.04 --> 00:04:27.03

We then took that pepperoni pizza and



85

00:04:27.03 --> 00:04:30.02

decorated it with another round of pepperoni.



86

00:04:30.02 --> 00:04:31.05

The cost has been adjusted.



87

00:04:31.05 --> 00:04:33.03

The text has been adjusted.



88

00:04:33.03 --> 00:04:34.08

Each and every time.



89

00:04:34.08 --> 00:04:36.06

And now, if we added other ingredients we could



90

00:04:36.06 --> 00:04:38.09

do the same thing until we got to the supreme.



