Design patterns Flashcards
What are Singleton / Factory design patterns?
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 would you create a Singleton?
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.
Explain the DAO design pattern
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.