Skip to main content

Java

GoF Design Patterns in Java: The Complete Guide (All 23 Patterns)

Complete reference for all 23 Gang of Four design patterns implemented in Java 17. Each pattern has a dedicated post with beginner-to-advanced coverage, full runnable code, UML diagram, and console output. Creational, Structural, and Behavioral patterns with real-world examples and JDK cross-references.

Visitor Design Pattern in Java: Complete Guide with Examples

The Visitor pattern lets you add new operations to a class hierarchy without modifying it. Each operation is a separate Visitor class; elements accept visitors via double dispatch. Complete Java guide: shape hierarchy with AreaCalculator and PerimeterCalculator visitors, double dispatch explained, when Visitor beats a switch statement, and when it's overkill.

Template Method Design Pattern in Java: Complete Guide with Examples

The Template Method pattern defines the skeleton of an algorithm in a base class and lets subclasses fill in the steps — without changing the overall structure. Complete Java guide: data migration pipeline with invariant skeleton, overridable hooks, and the Hollywood Principle. Includes comparison with Strategy and when to use each.

Strategy Design Pattern in Java: Complete Guide with Examples

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime. No more switch statements for algorithm selection. Complete Java guide: sorting strategy with BubbleSort/MergeSort/QuickSort, runtime selection, Java 8 functional shortcut with Comparator, and when Strategy is overkill.

State Design Pattern in Java: Complete Guide with Examples

The State pattern lets an object change its behaviour when its internal state changes — as if the object changed its class. Eliminate sprawling if-else state machines. Complete Java guide: traffic light example, state-driven transitions, how State differs from Strategy, vending machine walkthrough, and when to use enums vs State objects.

Observer Design Pattern in Java: Complete Guide with Examples

The Observer pattern defines a one-to-many dependency: when one object changes state, all dependents are notified automatically. Complete Java guide: stock price alert system, dynamic subscribe/unsubscribe, push vs pull notification models, java.util.Observable pitfalls, PropertyChangeListener, reactive extensions, and thread-safety considerations.

Memento Design Pattern in Java: Complete Guide with Examples

The Memento pattern captures an object's internal state in a snapshot and restores it later — without violating encapsulation. Complete Java guide: text editor undo with multi-field state, the Originator/Memento/Caretaker triad, how to keep mementos opaque, serialization as Memento, and bounded undo history management.

Mediator Design Pattern in Java: Complete Guide with Examples

The Mediator pattern replaces a web of N² direct object references with N connections through a central coordinator. Complete Java guide: chat room example, O(N²) vs O(N) dependency analysis, MVC as a Mediator, Spring's ApplicationEventPublisher, and how Mediator differs from Observer and Facade.

Iterator Design Pattern in Java: Complete Guide with Examples

The Iterator pattern provides a uniform way to traverse a collection without exposing its internal structure. Complete Java guide: custom BookShelf iterator, filtered decade iterator, how Java's for-each loop works under the hood, Iterable vs Iterator, and when to write a custom iterator vs using Streams.