Part 1: Singleton Design Pattern
Introduction:
The Singleton Design Pattern ensures that a class has only one instance and provides a global
point of access to that instance. It is a creational design pattern.
Key Characteristics:
- Only one instance of the class exists
- Global access point to the instance
- Controlled instantiation
Use Cases:
- Database connection handling
- Configuration management
- Logging systems
- Caching mechanisms
Real-Life Example:
A real-world example of a Singleton is a Printer Spooler. In a system, multiple users may send
print jobs, but only one spooler manages the queue to avoid conflicts.
Basic Implementation (Java)
Part 2: Factory Design Pattern
Introduction:
The Factory Design Pattern provides an interface for creating objects without specifying their
exact class.
Key Characteristics:
- Delegates object creation
- Promotes loose coupling
- Enhances scalability
Use Cases:
- Complex object creation
- Systems requiring flexibility
- Multiple similar object types
Real-Life Example:
A Restaurant Ordering System is a good example. The customer orders food (e.g., Burger,
Pizza), and the kitchen (factory) decides how to prepare it without exposing the
preparation details.
Basic Implementation (Java)
Part 3: Abstract Factory Pattern
Introduction
Abstract Factory is a creational pattern used to create families of related objects
without specifying their concrete classes.
Key Characteristics
Creates families of related objects
Promotes consistency
Hides object creation logic
Uses interfaces
Use Cases
Cross-platform UI systems
Theming systems
Applications with multiple product families
Real-Life Example: Operating System UI Kit
Different OS provide their own UI components:
Windows → WindowsButton, WindowsCheckbox
Mac → MacButton, MacCheckbox
Basic Implementation (Java)
// Abstract Products
interface Button {
void render();
}
interface Checkbox {
void render();
}
// Concrete Products
class WindowsButton implements Button {
public void render() {
[Link]("Windows Button");
}
}
class MacButton implements Button {
public void render() {
[Link]("Mac Button");
}
}
class WindowsCheckbox implements Checkbox {
public void render() {
[Link]("Windows Checkbox");
}
}
class MacCheckbox implements Checkbox {
public void render() {
[Link]("Mac Checkbox");
}
}
// Abstract Factory
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
// Concrete Factories
class WindowsFactory implements GUIFactory {
public Button createButton() { return new WindowsButton(); }
public Checkbox createCheckbox() { return new WindowsCheckbox(); }
}
class MacFactory implements GUIFactory {
public Button createButton() { return new MacButton(); }
public Checkbox createCheckbox() { return new MacCheckbox(); }
}
Part 4: Decorator Design Pattern
Introduction
Decorator is a structural pattern that allows adding new behavior dynamically to objects.
Key Characteristics
Runtime behavior modification
Uses composition
Flexible alternative to inheritance
Use Cases
UI enhancements
Feature extensions
Optional functionalities
Real-Life Example: Mobile App Notifications
Basic notification → plain message
Enhancements:
Sound
Vibration
Flash Alert
Basic Implementation (Java)
// Component
interface Notification {
String send();
}
// Concrete Component
class BasicNotification implements Notification {
public String send() {
return "Notification";
}
}
// Decorator
abstract class NotificationDecorator implements Notification {
protected Notification notification;
public NotificationDecorator(Notification n) {
[Link] = n;
}
}
// Concrete Decorators
class SoundDecorator extends NotificationDecorator {
public SoundDecorator(Notification n) { super(n); }
public String send() {
return [Link]() + " + Sound";
}
}
class VibrationDecorator extends NotificationDecorator {
public VibrationDecorator(Notification n) { super(n); }
public String send() {
return [Link]() + " + Vibration";
}
}
Part 5: Observer Design Pattern
Introduction
Observer is a behavioral pattern where: one object (Subject) notifies multiple observers when its
state changes.
Key Characteristics
One-to-many relationship
Loose coupling
Automatic updates
Use Cases
Event handling (GUI)
Real-time systems
Notifications
Basic Implementation (Java)
import [Link].*;
// Observer
interface Observer {
void update(int value);
}
// Subject
interface Subject {
void addObserver(Observer o);
void notifyObservers();
}
// Concrete Subject
class DataSource implements Subject {
private List<Observer> observers = new ArrayList<>();
private int data;
public void setData(int value) {
[Link] = value;
notifyObservers();
}
public void addObserver(Observer o) {
[Link](o);
}
public void notifyObservers() {
for (Observer o : observers) {
[Link](data);
}
}
}
// Concrete Observers
class Display implements Observer {
public void update(int value) {
[Link]("Display updated: " + value);
}
}
class Logger implements Observer {
public void update(int value) {
[Link]("Log: " + value);
}}
Lab Tasks (Singleton Design Pattern)
Task 1: Logger System
Problem Statement: Design a Logger class using Singleton pattern where multiple
classes can log messages but only one logger instance is used.
Requirements:
Create a Singleton Logger class
Add method: log(String message)
Simulate logging from 2 different classes
Expected Outcome: All logs should come from the same instance
Task 2: Configuration Manager
Problem Statement: Create a Configuration Manager that loads system settings (e.g.,
app name, version).
Requirements:
Implement Singleton pattern
Store at least 3 configuration values
Access these values from multiple classes
Expected Outcome: Same configuration instance shared across the system
Lab Tasks(Factory Design Pattern)
Task 1: Notification System
Requirements:
- Interface: Notification
- Classes: Email, SMS, Push
- Factory class to generate objects
Task 2: Payment System
Requirements:
- Interface: PaymentMethod
- Classes: CreditCard, PayPal, BankTransfer
- Factory class for object creation
Lab Task (Abstract Factory Design Pattern)
Task 1: GUI Theme System
Problem Statement:
Design a system where the registration form supports multiple UI themes.
Requirements:
Interface: Button, TextField
Concrete classes:
o DarkThemeButton, LightThemeButton
o DarkThemeTextField, LightThemeTextField
Abstract Factory: GUIFactory
Concrete Factories:
o DarkThemeFactory
o LightThemeFactory
Expected Outcome:
User selects theme → corresponding UI components are created.
Lab Tasks (Decorator)
Task 1: Form Field Enhancer
Problem Statement:
Enhance input fields with extra features.
Requirements:
Base: TextField
Decorators:
o ValidationDecorator (check input)
o BorderDecorator (highlight field)
o TooltipDecorator (show hints)
Expected Outcome:
Fields dynamically enhanced without changing base class.
Lab Tasks (Observer)
Task 1: Form Validation Listener
Problem Statement:
When user enters data, validation messages update automatically.
Requirements:
Subject: FormField
Observers:
o ErrorLabel
o SubmitButton (enable/disable)
Expected Outcome:
Input change → UI auto update