Question: ## Decorator Pattern ### Ex: 1 . Choose a programming language of your choice ( e . g . Python, Java, C + + ,
## Decorator Pattern
### Ex:
Choose a programming language of your choice eg Python, Java, C etc.
Write a simple program that creates an object eg a car, a book, a game character, etc. and adds some functionality to it using the Decorator design pattern.
Your program should have at least one base object and two decorators that add new functionality to the base object.
Use comments to explain your code and how the Decorator design pattern is being implemented.
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:
Define the Pizza class with a price attribute and a getprice method that returns the price.
Create a PizzaDecorator class that inherits from Pizza and adds a getprice method that calls the same method in the base class and adds the cost of the decorator.
Create two decorator classes, PepperoniTopping and MushroomTopping that add toppings to the pizza and increase the price accordingly.
Instantiate a Pizza object and then decorate it with the PepperoniTopping and MushroomTopping decorators.
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
