Design Patterns for Interviews Detailed Notes
---
1. Singleton Pattern
**Intent:** Ensure a class has only one instance and provide a global point of access to it.
**Use Case:** Database connection, configuration settings.
public class DatabaseConnection {
private static DatabaseConnection instance;
private Connection connection;
private DatabaseConnection() {
try {
connection = [Link]("jdbc:mysql://localhost:3306/mydb", "user",
"password");
} catch (SQLException e) {
throw new RuntimeException("Error connecting to DB", e);
public static synchronized DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
public Connection getConnection() {
return connection;
**Usage:**
Connection conn = [Link]().getConnection();
---
2. Factory Pattern
**Intent:** Define an interface for creating an object, but let subclasses alter the type of objects that
will be created.
**Use Case:** Object creation logic based on conditions.
**Example: Car Factory**
interface Car {
void drive();
}
class Sedan implements Car {
public void drive() {
[Link]("Driving a Sedan");
class Suv implements Car {
public void drive() {
[Link]("Driving an SUV");
class CarFactory {
public static Car getCar(String type) {
if ("sedan".equalsIgnoreCase(type)) return new Sedan();
else if ("suv".equalsIgnoreCase(type)) return new Suv();
throw new IllegalArgumentException("Unknown car type");
**Usage:**
Car car = [Link]("suv");
[Link]();
---
3. Adapter Pattern
**Intent:** Convert the interface of a class into another interface clients expect.
**Use Case:** Integrating incompatible or legacy systems.
**Example: Media Player**
interface MediaPlayer {
void play(String audioType, String fileName);
interface AdvancedMediaPlayer {
void playVlc(String fileName);
void playMp4(String fileName);
class VlcPlayer implements AdvancedMediaPlayer {
public void playVlc(String fileName) {
[Link]("Playing vlc file: " + fileName);
public void playMp4(String fileName) {}
class Mp4Player implements AdvancedMediaPlayer {
public void playVlc(String fileName) {}
public void playMp4(String fileName) {
[Link]("Playing mp4 file: " + fileName);
class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMediaPlayer;
public MediaAdapter(String audioType) {
if ("vlc".equalsIgnoreCase(audioType)) advancedMediaPlayer = new VlcPlayer();
else if ("mp4".equalsIgnoreCase(audioType)) advancedMediaPlayer = new Mp4Player();
public void play(String audioType, String fileName) {
if ("vlc".equalsIgnoreCase(audioType)) [Link](fileName);
else if ("mp4".equalsIgnoreCase(audioType)) advancedMediaPlayer.playMp4(fileName);
class AudioPlayer implements MediaPlayer {
public void play(String audioType, String fileName) {
if ("mp3".equalsIgnoreCase(audioType)) {
[Link]("Playing mp3 file: " + fileName);
} else {
MediaAdapter adapter = new MediaAdapter(audioType);
[Link](audioType, fileName);
}
}
**Usage:**
AudioPlayer player = new AudioPlayer();
[Link]("mp3", "song.mp3");
[Link]("vlc", "[Link]");
[Link]("mp4", "movie.mp4");
---