Design patterns Flashcards

1
Q

What are Singleton / Factory design patterns?

A

Singleton pattern is a design pattern in which only one instance of a class is present in the Java virtual machine. A singleton class (implementing singleton pattern) has to provide a global access point to get the instance of the class. In other words, a singleton pattern restricts the instantiation of a class.

The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.

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

How would you create a Singleton?

A

The most popular approach is to implement a Singleton by creating a regular class and making sure it has:

A private constructor.

A static field containing its only instance.

A static factory method for obtaining the instance.

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

Explain the DAO design pattern

A

DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.

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