Question: ## Decorator Pattern ### Ex: 1 . Choose a programming language of your choice ( e . g . Python, Java, C + + ,

## Decorator Pattern
### Ex:
1. Choose a programming language of your choice (e.g. Python, Java, C++, etc.).
2. Write a simple program that creates an object (e.g. a car, a book, a game character, etc.) and adds some functionality to it using the Decorator design pattern.
3. Your program should have at least one base object and two decorators that add new functionality to the base object.
4. Use comments to explain your code and how the Decorator design pattern is being implemented.
5. Test your program to make sure it works as expected.
Here's an example scenario for a possible program:
Suppose you have a base class called **`Pizza`** that represents a basic pizza with a certain price. You want to add toppings to the pizza, which will increase the price. You decide to use the Decorator design pattern to add toppings to the pizza dynamically.
You could create a program that follows these steps:
1. Define the **`Pizza`** class with a price attribute and a **`get_price()`** method that returns the price.
2. Create a **`PizzaDecorator`** class that inherits from **`Pizza`** and adds a **`get_price()`** method that calls the same method in the base class and adds the cost of the decorator.
3. Create two decorator classes, **`PepperoniTopping`** and **`MushroomTopping`**, that add toppings to the pizza and increase the price accordingly.
4. Instantiate a **`Pizza`** object and then decorate it with the **`PepperoniTopping`** and **`MushroomTopping`** decorators.
5. Test the program by printing the final price of the pizza after adding the toppings.
Good luck! Let me know if you have any questions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!