Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

NetTuts.com:
Design Patterns: The Decorator Pattern
Jan 23, 2015 @ 18:08:21

The NetTuts.com site has continued their series looking at design patterns and how they can be used in PHP. In this new post they focus in on the Decorator pattern, most commonly used to add functionality to a existing class (to "decorate" it).

Earlier in this series we explored both the facade and adapter design patterns in this series. Using facade, we can simplify large systems, and by implementing adapter we can stay safe while working with external API and classes. Now we are going to cover the decorator design pattern, which also falls under the category of structural patterns. We can use the decorator pattern when we just want to give some added responsibility to our base class. This design pattern is a great alternative to a sub‑classing feature for extending functionality with some added advantages.

They start with a problem that needs solving - sending an email with additional content not defined in the parent class. They show how to do something similar with child classes, but quickly find a limitation. Instead, they show how to use decorator classes and a simple interface to provide interchangeable classes that augment the contents of the email body as passed in via constructor injection.

tagged: designpattern decorator tutorial series email body content

Link: http://code.tutsplus.com/tutorials/design-patterns-the-decorator-pattern--cms-22641


Trending Topics: