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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *