The Decorator Pattern is a structural design pattern that allows you to dynamically add behavior or responsibilities to an object without modifying its original code. It is often used to adhere to the 2. Open and Closed Principle (one of the SOLID principles), which states that classes should be open for extension but closed for modification.
Imagine you have a plain cake. The Decorator Design Pattern is like adding layers of frosting, sprinkles, and decorations to make it more delicious and fancy. It lets you enhance an object by “decorating” it with extra features while keeping everything organized and flexible.
Pizza Interface
BasicPizza
Knowledge
An abstract class is like a blueprint for other classes. It cannot be instantiated itself but is designed to be extended by other classes. Abstract classes can have both regular methods with implementations and abstract methods that don’t have implementations and must be overridden by the subclasses.
ToppingsDecorator
Knowledge
In the
ToppingDecorator
abstract class, thePizza
field is declared asprotected
to control the visibility and accessibility of this field for subclasses. Here’s why:Visibility Control: By making
Pizza
fieldprotected
, it can be accessed by subclasses (likeCheeseTopping
,PepperoniTopping
, etc.) and any other classes in the same package. This allows subclasses to directly interact with thePizza
instance they’re enhancing without exposing it to classes outside the package.Inheritance and Encapsulation: Making the
Pizza
fieldprotected
ensures that subclasses can access it directly, which is important for the decorators to work. SincePizza
is the core component being decorated, decorators need access to its methods and properties to add their functionality.Consistency: By using
protected
, the design enforces a consistent and controlled way for subclasses to interact with the core component.Remember, encapsulation is a key principle in object-oriented programming, and using
protected
in this context helps balance visibility and control while ensuring that the decorator pattern works effectively.
CheeseToppings
PepperToppings