L7 + L8 Design Patterns and Singleton Flashcards
What are design patterns in software engineering?
Design patterns to common software design problems, popularized by the Gang of Four (GoF)
What are the benefits of using design patterns?
-Shared language for developers
-Improves code readability and maintainability
-Encourages best practices
What are the main reasons to use design patterns?
-Reusability: Avoid reinventing solutions
-Scalability: Make systems adaptable
-Maintainability: Keep code clean and organized
-Collaboration: Provides a common understanding among developers
What are the three main categories of design patterns?
- Creational Patterns - Handle object creation.
-Singleton, Factory Method, Builder - Structural Patterns - Handle object composition.
-Adapter, Composite, Decorator - Behavioral Patterns - Handle object interaction.
-Observer, Strategy, Command
When should you consider using design patterns?
- When facing recurring design problems.
- When designing complex systems
- When improving code flexibility and scalability
Why shouldn’t you overuse design patterns?
Overusing them can lead to unnecessary complexity.
What is the Singleton Pattern?
A creational pattern that ensures a class has only one instance and provides a global access point to it.
In what situations is the Singleton pattern useful?
- Managing configurations
- Logging
- Database connections
What are the two key problems the Singleton pattern solves?
- Ensures only one instance of a class exists.
- Provides a global access point to that instance.
How do you implement the Singleton Pattern in Java?
public class Singleton {
private static Singleton instance;
private Singleton() {} // Private constructor public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
Why is the Singleton constructor declared ‘private’
It prevents other classes from directly creating new instances.
How does Singleton ensure only one instance exists?
The ‘getInstance()’ method checks if an instance exists:
-If no instance exists, it creates one.
-If an instance already exists, it returns the existing instance.
What are the advantages of the Singleton pattern?
-Ensures a single instance of a class
-Provides a global access point
-Lazy initialization - Instance is created only when needed.
What are the disadvantages of Singleton Pattern?
- Violates the Single Responsibility Principle: Handles both instantiation and global access.
- Can hide bad design: Leads to tightly coupled code.
- Not thread-safe : Multiple threads may create multiple instances.
Why is the Singleton pattern problematic in a multithreaded environment?
If two threads access ‘getInstance()’ simultaneously, they might both create separate instances, violating Singleton’s purpose
How can you make Singleton thread-safe?
By synchronizing ‘getInstance()’
Example:
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
Why is Singleton difficult to unit test?
-Mocking is hard - Since the constructor is private and static methods can’t be overridden.
- Tests might interfere - Singleton maintains state, which persists across tests.
Why is Singleton discouraged in Dependency Injection?
- Hard to replace with mock objects
- Leads to hidden dependencies
- Encourages global state, making debugging harder.
What is an alternative to Singleton for managing shared resources?
- Dependency Injection - Pass thee instance as a parameter instead of making it globally accessible.
When is the Singleton pattern appropriate?
- Logging
- Configuration management
- Shared resources like database connections
What is Inheritance?
Inheritance is a mechanism that allows a class (derived class) to inherit fields and methods from another class (base class)
What are the benefits of inhertance?
Code reuse - Avoid duplicating code
Hierarchy organization - Natural grouping
of classes
Extensibility - Easily add new features to n
derived classes.
What are base and derived classes?
Base class (Super class) - The existing class being inherited from.
Derived class (Subclass) - The new class that extends the base class.
How do you define a derived class in Java?
public class HourlyEmployee extends Employee {
//code
}