From the course: PHP: Design Patterns

Unlock the full course today

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

Exploring a use case for the decorator pattern

Exploring a use case for the decorator pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Exploring a use case for the decorator pattern

- In our code, we can print any of the properties we want, but we must call them individually. For example, here we're displaying the title of the book, the author's first and last name in two different formats. First, it's first, last name, with a space in-between and then it's last name, first name, with a comma and space in-between. Here's what the code displays. Unfortunately, if we need to display the author's name in numerous places, we have to do this over and over again. This becomes incredibly painful once we have to change something. We'll have to find and update every instance. That's trouble. Using the decorator pattern, you'll see we've made one major addition. We've added a PrettyPrint class. It takes our $book_object as an input and gives us two methods. The first method is the same thing that we had before, it displays a first name, space, and then the last name. The second method does the second thing that we had before. It displays last name, comma, space, and then…

Contents