← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 1 of 14

Java Design Patterns Introduction

Creational Patterns Concept

Creational patterns focus on the mechanics of object creation itself, aiming to instantiate objects in whatever manner best fits the situation rather than always using a plain constructor. A classic example is the Prototype pattern, which creates new objects by cloning an existing, pre-configured instance instead of building one from scratch.

Example: Creational Patterns Concept

java
public class Main {
	static class Config implements Cloneable {
		String name;
		Config(String name) { this.name = name; }
		public Config clone() {
			try { return (Config) super.clone(); } catch (CloneNotSupportedException e) { return null; }
		}
	}
	public static void main(String[] args) {
		Config original = new Config("base");
		Config copy = original.clone(); // creates a new object by cloning, not a fresh constructor call
		System.out.println(copy.name);
	}
}

Structural Patterns Concept

Structural patterns describe how to assemble objects and classes into larger structures while keeping those structures flexible and loosely coupled. The Adapter pattern is a common example, translating one class's interface into another interface that client code already expects, without modifying either side.

Example: Structural Patterns Concept

java
public class Main {
	interface UsSocket { void plugUs(); }
	static class EuPlug {
		void plugEu() { System.out.println("EU plug connected"); }
	}
	static class Adapter implements UsSocket {
		EuPlug euPlug = new EuPlug();
		public void plugUs() { euPlug.plugEu(); } // translates one interface to another
	}
	public static void main(String[] args) {
		UsSocket socket = new Adapter();
		socket.plugUs();
	}
}

Behavioral Patterns Concept

Behavioral patterns are concerned with algorithms and how responsibilities are distributed and communicated between objects. The Strategy pattern, for instance, defines a family of interchangeable algorithms and lets client code swap between them at runtime without changing the code that uses them.

Example: Behavioral Patterns Concept

java
public class Main {
	interface PaymentStrategy { void pay(int amount); }
	static class CardPayment implements PaymentStrategy {
		public void pay(int amount) { System.out.println("Paid " + amount + " by card"); }
	}
	static class CashPayment implements PaymentStrategy {
		public void pay(int amount) { System.out.println("Paid " + amount + " in cash"); }
	}
	public static void main(String[] args) {
		PaymentStrategy strategy = new CardPayment();
		strategy.pay(100); // swappable at runtime
	}
}

Why Use Design Patterns?

Design patterns exist because they're well-tested, proven solutions to problems that come up again and again in software design. They give developers a shared vocabulary — saying 'this is a Factory' communicates a whole design instantly — and they save you from re-inventing (and possibly getting wrong) a solution someone has already refined.

Example: Why Use Design Patterns?

java
public class Main {
	interface Factory { Object create(); } // naming this "Factory" instantly communicates the design
	public static void main(String[] args) {
		Factory factory = () -> "Shared vocabulary saves re-inventing solutions";
		System.out.println(factory.create());
	}
}

Categorizing Patterns

The three broad categories — Creational, Structural, and Behavioral — are a useful first filter when you're facing a design problem: identifying which category your problem actually falls into narrows down which specific pattern is likely to fit before you even look at the individual options.

Example: Categorizing Patterns

java
public class Main {
	enum PatternCategory { CREATIONAL, STRUCTURAL, BEHAVIORAL }
	public static void main(String[] args) {
		PatternCategory category = PatternCategory.CREATIONAL; // identify the category first
		System.out.println("Problem falls into: " + category);
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.