Java Design Patterns - Interview Summary
1. Creational Patterns
- Singleton: Ensures a class has only one instance.
- Example: [Link]()
- Use: Logging, Configuration
- Factory Method: Creates object without exposing creation logic.
- Example: [Link]()
- Use: Shape factory
- Abstract Factory: Factory of factories. Creates related objects.
- Use: UI components per OS
- Builder: Step-by-step construction of complex object.
- Example: StringBuilder
- Prototype: Clones an object instead of creating.
- Example: [Link]()
2. Structural Patterns
- Adapter: Bridge between incompatible interfaces.
- Example: [Link]()
- Decorator: Add behavior without changing structure.
- Example: BufferedReader wrapping FileReader
- Facade: Unified interface over subsystems.
- Example: FacesContext
- Proxy: Placeholder for another object.
- Example: [Link]
- Composite: Treat group of objects like one.
- Example: File system
- Bridge: Decouple abstraction from implementation.
- Flyweight: Reduce memory using shared objects.
- Example: [Link]()
3. Behavioral Patterns
- Observer: One-to-many dependency.
- Example: Event listeners
- Strategy: Interchangeable algorithms.
- Example: Comparator
- Command: Wrap request as an object.
- Example: Runnable
- Chain of Responsibility: Pass request along a chain.
- Example: Servlet filters
- Template Method: Algorithm structure in superclass.
- Example: HttpServlet
- State: Change behavior based on state.
- Example: Vending Machine
- Mediator: Centralizes communication.
- Use: Chatroom
- Iterator: Sequential access.
- Example: Iterator, ListIterator
- Visitor: Add operations without changing classes.
- Use: Tax calculation
Interview Focus Patterns
- Most Asked:
- Singleton
- Factory / Abstract Factory
- Builder
- Strategy
- Observer
- Decorator
- Proxy
- Template Method