What is the Single Responsibility Principle (SRP)?
→ A class should have only one reason to change.
What does the Open/Closed Principle (OCP) state?
→ Classes should be open for extension but closed for modification.
What is the Liskov Substitution Principle (LSP)?
→ Subtypes must be substitutable for their base types.
What does the Interface Segregation Principle (ISP) mean?
→ A class should not be forced to implement interfaces it doesn’t use.
What is the Dependency Inversion Principle (DIP)?
→ High-level modules should depend on abstractions, not concrete implementations.
What is encapsulation?
→ Restricting direct access to object data while exposing necessary parts.
How does inheritance differ from composition?
→ Inheritance uses "is-a" relationships; composition uses "has-a" relationships.
What is abstraction?
→ Hiding implementation details while exposing only essential functionality.
What is the purpose of the Singleton pattern?
→ Ensures only one instance of a class exists.
What problem does the Factory Method pattern solve?
→ Encapsulates object creation, letting subclasses decide which class to instantiate.
What is the Abstract Factory pattern used for?
→ Creates families of related objects without specifying concrete classes.
How does the Builder pattern work?
→ Separates object construction from representation by building it step by step.
What is the Adapter pattern?
→ Converts one interface into another to make incompatible systems work together.
How does the Bridge pattern differ from the Adapter pattern?
→ Bridge separates abstraction from implementation, while Adapter makes two existing interfaces compatible.
What is the Flyweight pattern?
→ Shares objects to reduce memory usage.
What is the purpose of the Strategy pattern?
→ Encapsulates multiple interchangeable behaviors.
How does the Observer pattern work?
→ Defines a dependency where multiple objects listen and react to changes in a subject.
What is the Command pattern?
→ Encapsulates a request as an object, allowing parameterization, queuing, and logging.
What does the State pattern allow an object to do?
→ Change behavior when its internal state changes.
What is the Chain of Responsibility pattern?
→ Passes a request through a chain of handlers until one processes it.
What is the key difference between Strategy and State patterns?
→ Strategy lets the client choose the behavior; State changes behavior based on internal state.
When should you use the Observer pattern?
→ When multiple objects need to react to state changes in a subject.
How does the Chain of Responsibility pattern improve flexibility?
→ Requests are passed through multiple handlers, allowing different handlers to process them dynamically.
What are the benefits of the Builder pattern?
→ It helps construct complex objects while keeping the creation process flexible.
When should you use the Adapter pattern?
→ When you need to make an existing class compatible with another interface.
How do you fix a Singleton implementation that lacks thread safety?
→ Use synchronized getInstance()
or double-checked locking.
What’s the primary benefit of using the Bridge pattern?
→ It decouples abstraction from implementation.
How does the Command pattern help in undo/redo functionality?
→ Commands can be stored in a history log, allowing undo and redo operations.
What is the main drawback of the Flyweight pattern?
→ Managing shared state can be complex.
How does the Factory Method align with the Open/Closed Principle?
→ New types can be added without modifying existing code.
How does the Observer pattern reduce coupling?
→ Subjects and observers are loosely coupled.
What’s the difference between tight coupling and loose coupling?
→ Tight coupling makes changes harder, while loose coupling improves flexibility.
What is high cohesion?
→ A class focuses on a single responsibility, making it easier to maintain.
What is low cohesion?
→ A class has multiple unrelated behaviors, making it difficult to maintain.
What is the difference between aggregation and composition?
→ Aggregation is a weak relationship; composition is a strong relationship where the contained object cannot exist independently.
Why should composition be preferred over inheritance?
→ It avoids deep inheritance hierarchies and helps achieve loose coupling.