Design_Patterns_Interview_Problems
Design_Patterns_Interview_Problems
How would you apply the Strategy pattern in a real-world system to support
pluggable business rules?
Use the Strategy pattern when you want to define a family of algorithms or behaviors
and make them interchangeable at runtime.
Example use case: A payment service that supports multiple discount strategies (e.g.,
seasonal discount, loyalty discount, referral discount).
Implementation approach:
Define a common interface, e.g., DiscountStrategy with method
calculateDiscount(order).
Implement various strategies as separate classes: SeasonalDiscount, LoyaltyDiscount,
etc.
Inject the correct strategy based on context using dependency injection or a factory.
The service delegates to the selected strategy without knowing its implementation.
Benefits:
Open/Closed Principle — new strategies can be added without modifying existing code.
Decouples algorithm from the context object using it.
What problems can arise from using the Singleton pattern excessively in an
enterprise application?
Excessive use of Singleton can lead to tight coupling and global state, which harms
testability and maintainability.
Typical issues include:
Hidden dependencies that make code hard to understand and refactor.
Concurrency problems if Singleton holds mutable state without synchronization.
Difficulty mocking or substituting the Singleton during unit testing.
Overuse turns Singleton into a glorified global variable, violating SOLID principles.
Alternatives:
Use dependency injection frameworks (e.g., Spring) to manage lifecycle and scope.
Convert stateful singletons to stateless service beans or scoped components.
How does the Decorator pattern differ from inheritance and when would you
prefer it?
The Decorator pattern allows behavior to be added to an object dynamically, without
modifying its structure.
Unlike inheritance, which is static and compile-time, decorators are composed at
runtime.
Use cases where Decorator is preferred:
When you want to add responsibilities to objects without altering their class hierarchy.
Example: Adding logging, encryption, or compression to a stream without changing the
stream interface.
Advantages:
Follows the Open/Closed Principle.
Avoids an explosion of subclasses for every combination of behaviors.
Best practice:
Favor composition (Decorator) over inheritance when flexibility and behavior layering
are needed.
Can you describe how the Factory Method pattern improves object creation in
complex systems?
The Factory Method pattern delegates the instantiation of objects to subclasses or
strategy methods.
Useful when object creation logic is complex or depends on runtime context.
Typical use cases:
Creating different parser instances based on file type (e.g., JSON, XML, CSV).
Frameworks (e.g., Spring) that manage object lifecycle through configuration or
annotations.
Benefits:
Encapsulates object creation logic, reducing duplication and tight coupling.
Allows extending object families without modifying the client code.
Difference from Abstract Factory:
Factory Method creates one product at a time; Abstract Factory creates related families
of products.