0% found this document useful (0 votes)
6 views6 pages

Design Patterns Notes

The document provides detailed notes on three design patterns: Singleton, Factory, and Adapter. Each pattern includes its intent, use case, and example code demonstrating its implementation. The Singleton pattern ensures a single instance of a class, the Factory pattern defines an interface for object creation, and the Adapter pattern converts interfaces for compatibility.

Uploaded by

Lycan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Design Patterns Notes

The document provides detailed notes on three design patterns: Singleton, Factory, and Adapter. Each pattern includes its intent, use case, and example code demonstrating its implementation. The Singleton pattern ensures a single instance of a class, the Factory pattern defines an interface for object creation, and the Adapter pattern converts interfaces for compatibility.

Uploaded by

Lycan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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");

---

You might also like