Java Design Patterns Introduction
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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?
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern