Java Pattern Example: Master Design Patterns with Code Snippets

Robert Jul 13, 2026

Java programming introduces various design patterns that aid in writing clean, efficient, and maintainable code. One way to illustrate these design patterns is through real-world examples. Let's delve into two essential Java patterns: Singleton and Observer, and explore how they work.

Factory Pattern in Java | Static Factory method Example
Factory Pattern in Java | Static Factory method Example

Design patterns are reusable solutions to common software challenges, enabling us to write better, more structured, and easier-to-maintain code. They provide a proven framework for solving commonly occurring problems in software design.

Java Design Patterns
Java Design Patterns

The Singleton Pattern

The Singleton pattern restricts a class to a single instance and provides a global access point to this instance. It includes lazy initialization, which delays the creation of an object until its use.

Java Patterns Notes 1
Java Patterns Notes 1

Here's a basic example of the Singleton pattern in Java:

```java public class Singleton { private static Singleton singleInstance; private Singleton() {} public static Singleton getInstance() { if (singleInstance == null) { singleInstance = new Singleton(); } return singleInstance; } } ```

Lazy Initialization

Client Challenge
Client Challenge

In the above code, the instance of the Singleton class is not created at the time of class loading. Instead, it's created when it's needed (i.e., when `getInstance()` is called). This is known as lazy initialization.

However, this simple technique doesn't handle concurrent scenarios well. A multithreaded environment could lead to multiple instances being created, violating the Singleton pattern's constraint. To address this, consider using the lazy initialization with double-checked locking:

```java public class Singleton { private static volatile Singleton singleInstance; private Singleton() {} public static Singleton getInstance() { if (singleInstance == null) { synchronized (Singleton.class) { if (singleInstance == null) { singleInstance = new Singleton(); } } } return singleInstance; } } ```

Singleton with Enums

Creational Design Patterns in Java
Creational Design Patterns in Java

To freeze the instance creation process and guarantee the singleton property, you can use an enum to hold the single instance, making it thread-safe:

```java public enum Singleton { INSTANCE; public static Singleton getInstance() { return INSTANCE; } } ```

The Observer Pattern

The Observer pattern defines a one-to-many dependency between objects, enabling one object (subject) to notify others (observers) when its state changes. Imagine a newspaper (subject) with multiple subscribers (observers) who want to be informed daily.

Iterator Design Pattern in Java
Iterator Design Pattern in Java

Let's implement this pattern in Java:

```java import java.util.ArrayList; import java.util.List; interface Observer { void update(String message); } class Subject { List observers = new ArrayList<>(); void attach(Observer observer) { observers.add(observer); } void detach(Observer observer) { observers.remove(observer); } void notifyObservers(String message) { observers.forEach(observer -> observer.update(message)); } } ```

Observer Class Example

80+ Pattern Programs In Java
80+ Pattern Programs In Java
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
7 Best Java Design Pattern Books for Beginners and Experienced Programmers
7 Best Java Design Pattern Books for Beginners and Experienced Programmers
Observer Design Pattern in Java
Observer Design Pattern in Java
LeetCode 205 Isomorphic Strings Explained Simply | Java DSA Cheat Sheet
LeetCode 205 Isomorphic Strings Explained Simply | Java DSA Cheat Sheet
Most Common Design Patterns in Java (with Examples) | DigitalOcean
Most Common Design Patterns in Java (with Examples) | DigitalOcean
Java Type Conversion & Type Casting Notes
Java Type Conversion & Type Casting Notes
Design Patterns - Builder Pattern
Design Patterns - Builder Pattern
Java Streams Cheat Sheet. Creation, Intermediate & Terminal Operations (Java 17+)
Java Streams Cheat Sheet. Creation, Intermediate & Terminal Operations (Java 17+)
Facade Design Pattern in Java
Facade Design Pattern in Java
QuickSort Algorithm Example in Java using Recursion  - Tutorial
QuickSort Algorithm Example in Java using Recursion - Tutorial
Top 3 Design Patterns and Best Practices Java Developers Can Learn From Spring Framework
Top 3 Design Patterns and Best Practices Java Developers Can Learn From Spring Framework
Composite Design Pattern in Java
Composite Design Pattern in Java
Chain of Responsibility Design Pattern in Java
Chain of Responsibility Design Pattern in Java
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Top 10 Java Programs, Assignments, and Coding Interview Questions Answers
Top 10 Java Programs, Assignments, and Coding Interview Questions Answers
Prototype Design Pattern in Java
Prototype Design Pattern in Java
a black and white photo with lines drawn on the bottom, in front of a dark background
a black and white photo with lines drawn on the bottom, in front of a dark background
Master Java Patterns
Master Java Patterns

The Observer interface contains the `update()` method, allowing observers to receive updates. The `Subject` class maintains a list of observers, providing methods to attach, detach, and notify observers. Here's an example:

```java class ConcreteObserver implements Observer { private String observerName; ConcreteObserver(String name) { this.observerName = name; } @Override public void update(String message) { System.out.println(observerName + " received message: " + message); } } ```

In this case, the `ConcreteObserver` class implements the `Observer` interface, printing a message when updated. You can now use the `Subject` and `ConcreteObserver` classes to create a real-world Observable and Observer scenario, demonstrating the Observer pattern in action.

Understanding and applying these Java design patterns can significantly improve your coding efficiency and maintainability. By mastering the Singleton and Observer patterns, you'll be equipped to tackle common programming challenges more effectively. Happy coding!