This repository explains all 23 Gang of Four design patterns in Java.
The teaching style is intentionally beginner-first:
- Understand the real problem first.
- Learn the pattern idea in simple language.
- Read small step-by-step Java examples.
- Run a mini project that shows where the pattern fits in real code.
- Compare the pattern with similar patterns so you do not memorize blindly.
A design pattern is a proven way to solve a common software design problem.
It is not a library, framework, or ready-made code copy.
It is a reusable idea.
For example, if many classes need to be notified when one object changes, Observer gives a clean structure for that. If an object has too many constructor parameters, Builder gives a cleaner construction style.
The main goal is not to use patterns everywhere. The goal is to recognize recurring problems and choose a pattern only when it makes the code easier to understand, extend, and maintain.
Every pattern folder contains:
- A detailed
README.md - Small learning examples like
Pattern1.java,Pattern2.java,Pattern3.java - A
miniProjectfolder - A mini project
README.md - A runnable
App.java
Creational patterns focus on object creation.
Structural patterns focus on how classes and objects are composed.
Behavioural patterns focus on communication and responsibility between objects.
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- Start with Singleton, Factory Method, Builder, and Strategy.
- Then learn Adapter, Decorator, Facade, and Observer.
- Then move to Abstract Factory, Prototype, Composite, State, and Command.
- Finally study Bridge, Flyweight, Chain of Responsibility, Interpreter, Iterator, Mediator, Memento, Template Method, and Visitor.
This order is easier for beginners because it starts with patterns you can immediately connect to daily Java code.
Use -d out so compiled .class files go into the out folder instead of being created beside source files.
Example learning file:
javac -d out src/DesignPatterns/creational/builder/Builder2.java
java -cp out DesignPatterns.creational.builder.Builder2Example mini project:
javac -d out src/DesignPatterns/structural/decorator/miniProject/*.java
java -cp out DesignPatterns.structural.decorator.miniProject.AppFor each pattern:
- Read the pattern README first.
- Run
Pattern1.javato see the problem. - Run the next files to see the improvement.
- Read comments inside the Java files line by line.
- Run the mini project.
- Read the comparison section to avoid confusing similar patterns.
- Ask yourself: would this pattern simplify my current code, or would it only add ceremony?
Do not force design patterns into every program.
A pattern is useful only when it solves a real design problem.
Simple code is better than pattern-heavy code when the requirement is simple.