Chapter 3. Creating and Extending Objects with the Decorator and Factory Patterns

<feature> <title>In This Chapter</title> <objective>

Keeping code closed for modification, open for extension

</objective>
<objective>

Introducing the Decorator pattern

</objective>
<objective>

Creating your own decorators

</objective>
<objective>

Wrapping objects in decorators

</objective>
<objective>

Building objects with factories

</objective>
<objective>

Encapsulating object creation in factories

</objective>
<objective>

Using the Factory Method pattern

</objective>
</feature>

You’re on the job as the new Design Pattern consultant at GigantoComputer company, getting paid an exorbitant amount of money, and you’re in the company cafeteria.

“What’ll you have?” asks the surly cook behind the grill counter.

“A burger,” you say, wrestling with your tray.

The cook rings it up on the cash register, and then remembers to ask, “Fries with that?”

“Sure,” you say.

The cook clears the order on the cash register and starts over, saying, “Burger and fries,” and enters that.

“Let’s make that a cheeseburger,” you say on impulse.

The cook gives you a dirty look and starts the order over from scratch, pecking at the keyboard and saying, “Burger with cheese and fries. Okay. That it?”

“Hmm,” you say, scanning the posted menu. “Maybe make that a bacon cheeseburger.”

The cook stares at you and seems on the verge of saying something unpleasant, but enters the order into the cash register, starting the whole process over.

“Hey,” you say, “you ...

Get Design Patterns For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.