While developing a web application, you create a class "Product" to hold the basic attributes of products in an e-commerce site. Later, you need to extend the functionality of some products which are on sale without altering the "Product" class. Which design pattern might be most appropriate to implement this additional functionality without modifying the existing "Product" class?
- Decorator Pattern
- Factory Method Pattern
- Singleton Pattern
- Observer Pattern
The Decorator Pattern is a suitable choice in this scenario. It allows you to add new behaviors or functionality (e.g., for products on sale) to existing classes (e.g., "Product") without modifying their structure. This ensures the open-closed principle and maintainability.
Loading...
Related Quiz
- What is the impact on performance when using a switch statement versus multiple if-else statements for numerous conditions?
- How does JavaScript’s prototype inheritance differ from classical inheritance models?
- How does the pop() method behave when applied on an empty array?
- Considering JavaScript's type coercion, what will be the result of [] == ![]?
- The ________ method is used to parse a JSON string into a JavaScript object.