Design Patterns

A Design Pattern is a reusable solution to common software design problems.

Elaboration:

  • Three categories:
    1. Creational Patterns (e.g., Singleton, Factory).
    2. Structural Patterns (e.g., Adapter, Composite).
    3. Behavioral Patterns (e.g., Observer, Strategy).
  • Helps in writing scalable and maintainable code.

Example (Singleton Pattern in Java):

public class Singleton {
private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
  • Ensures only one instance of Singleton exists.