L7 + L8 Design Patterns and Singleton Flashcards

1
Q

What are design patterns in software engineering?

A

Design patterns to common software design problems, popularized by the Gang of Four (GoF)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the benefits of using design patterns?

A

-Shared language for developers
-Improves code readability and maintainability
-Encourages best practices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the main reasons to use design patterns?

A

-Reusability: Avoid reinventing solutions
-Scalability: Make systems adaptable
-Maintainability: Keep code clean and organized
-Collaboration: Provides a common understanding among developers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three main categories of design patterns?

A
  1. Creational Patterns - Handle object creation.
    -Singleton, Factory Method, Builder
  2. Structural Patterns - Handle object composition.
    -Adapter, Composite, Decorator
  3. Behavioral Patterns - Handle object interaction.
    -Observer, Strategy, Command
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should you consider using design patterns?

A
  • When facing recurring design problems.
  • When designing complex systems
  • When improving code flexibility and scalability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why shouldn’t you overuse design patterns?

A

Overusing them can lead to unnecessary complexity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Singleton Pattern?

A

A creational pattern that ensures a class has only one instance and provides a global access point to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In what situations is the Singleton pattern useful?

A
  • Managing configurations
  • Logging
  • Database connections
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two key problems the Singleton pattern solves?

A
  1. Ensures only one instance of a class exists.
  2. Provides a global access point to that instance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you implement the Singleton Pattern in Java?

A

public class Singleton {
private static Singleton instance;

private Singleton() {} // Private constructor

public static Singleton getInstance() {
    if (instance == null) {
        instance = new Singleton();
    }
    return instance;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is the Singleton constructor declared ‘private’

A

It prevents other classes from directly creating new instances.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Singleton ensure only one instance exists?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages of the Singleton pattern?

A

-Ensures a single instance of a class
-Provides a global access point
-Lazy initialization - Instance is created only when needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the disadvantages of Singleton Pattern?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is the Singleton pattern problematic in a multithreaded environment?

A

If two threads access ‘getInstance()’ simultaneously, they might both create separate instances, violating Singleton’s purpose

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you make Singleton thread-safe?

A

By synchronizing ‘getInstance()’
Example:
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why is Singleton difficult to unit test?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is Singleton discouraged in Dependency Injection?

A
  • Hard to replace with mock objects
  • Leads to hidden dependencies
  • Encourages global state, making debugging harder.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an alternative to Singleton for managing shared resources?

A
  • Dependency Injection - Pass thee instance as a parameter instead of making it globally accessible.
20
Q

When is the Singleton pattern appropriate?

A
  • Logging
  • Configuration management
  • Shared resources like database connections
21
Q

What is Inheritance?

A

Inheritance is a mechanism that allows a class (derived class) to inherit fields and methods from another class (base class)

22
Q

What are the benefits of inhertance?

A

Code reuse - Avoid duplicating code

Hierarchy organization - Natural grouping
of classes
Extensibility - Easily add new features to n
derived classes.

23
Q

What are base and derived classes?

A

Base class (Super class) - The existing class being inherited from.

Derived class (Subclass) - The new class that extends the base class.

24
Q

How do you define a derived class in Java?

A

public class HourlyEmployee extends Employee {
//code
}

25
What is class hierarchy?
A class hierarchy represents a structured inheritance relationship, where classes are progressively more specialized.
26
What do derived classes inherit from base classes?
Instance Variables (non-private) Methods (non-private) Static variables and methods (Private members are NOT inherited directly)
27
What are parent and child classes?
Parent class = Base class Child class = Derived class
28
How do you override a method in Java?
Use the @Override annotation: @Override public void display() { System.out.println("This is a derived class method".); }
29
What are the rules for overriding methods?
- Method must have the same name and parameters. -Return type must be the same or a subclass(covariant return type) -Cannot override private or static methods
30
What is a covariant return type?
A method in a derived class can return a subtype of the return type in the base class. Example: public class Employee { public Employee getEmployee() { return this; } } public class HourlyEmployee extends Employee { @Override public HourlyEmployee getEmployee() { return this; } }
31
Can an overridden method have a different access modifier?
- You can make a method more accessible (e.g., private into public) - You CANNOT make it more restrictive (public into private)
32
What is the difference between overriding and overloading?
Overriding - Redefining a method in a subclass. Overloading - Defining multiple methods with the same name but different parameters.
33
What does 'final' do in Java?
'final' methods cannot be overridden. 'final' classes cannot be extended.
34
What is the purpose of 'super' in Java?
-Call a superclass method: super.display(); -Call a superclass constructor: super(param1, param2); -Access a superclass variable: super.variableName;
35
How do you use 'this' in a constructor?
public Employee() { this("Default Name", new Date()); } -Calls another constructor in the same class. -Must be the first statement in the constructor.
36
How do you invoke a superclass constructor?
public HourlyEmployee(String name, Date hireDate) { super(name, hireDate); } -Calls the constructor of the base class -Must be the first statement in the constructor
37
Can an object of a derived class be assigned to a base class variable?
Yes, because of polymorphism. Employee emp = new HourlyEmployee() -But you cannot assign a base class object to a derived class reference.
38
What is 'instanceof' used for?
Checks if an object is an instance of a particular class or subclass: if( emp instanceof HourlyEmployee() { }
39
How does 'getClass()' differ from 'instanceof'
getClass() checks for an exact match. instanceof allows subclass matches.
40
Why can't a derived class access a 'private' variable of a base class?
Private members are not inherited. They can only be accessed via getter an setter methods.
41
What is special about the 'Object' class?
-Every Java class implicitly extends 'Object' -It provides useful methods like 'toString()', equals(), and 'hashCode()'
42
What's the best way to override 'equals()'
@Override public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) return false; Employee other = (Employee) obj; return this.name.equals(other.name); } -Checks for null -Uses getClass() instead of 'instanceof'
43
What is the difference between 'is-a' and 'has-a' relationships?
-'is-a' = Inheritance (e.g., HourlyEmployee is an Employee) -'Has-a' = Composition (e.g., Employee has a Date)
44
Can you call 'super' twice in a constructor?
No, you can only call 'super' once, and it must be the first statement.
45