A small collection of Java examples demonstrating classic Software Design Patterns, implemented for learning and practice.
This repository contains simple and clean implementations of the most common Gang of Four (GoF) design patterns.
Each pattern is organized into its own package and includes:
- Source code
- Comments explaining the logic
- A runnable
Mainclass for quick testing
| Pattern | Type | Description |
|---|---|---|
| Builder | Creational | Step-by-step object construction (e.g. Pizza, Car) |
| Factory Method | Creational | Creates objects via subclasses rather than direct instantiation |
| Abstract Factory | Creational | Creates families of related objects without specifying their concrete classes |
| Adapter | Structural | Converts one interface into another that clients expect |
| (coming soon) | — | More patterns will be added during study sessions |
// Interface expected by the client
public interface PaymentProcessor {
void pay(double amount);
}
// Existing incompatible class
public class PayPal {
void makePayment(double amount) {
System.out.println("Paid " + amount + " via PayPal");
}
}
// Adapter class
public class PayPalAdapter implements PaymentProcessor {
private PayPal payPal = new PayPal();
@Override
public void pay(double amount) {
payPal.makePayment(amount);
}
}
// Usage
public class Main {
public static void main(String[] args) {
PaymentProcessor processor = new PayPalAdapter();
processor.pay(1500);
}
}