Home  Java   Design patt ...

Design patterns in Java - Gang of Four

The most famous classification comes from the "Gang of Four" (GoF), which groups patterns into three categories:


1. Creational Patterns 🛠️

These patterns deal with object creation mechanisms, trying to create objects in a manner suitable for the situation while increasing flexibility and reuse.

PatternGoalExample
SingletonEnsures a class has only one instance and provides a global point of access to it.A configuration manager or a database connection pool.
Factory MethodProvides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.A factory that creates different types of vehicles (Car, Truck, Bike) based on input.
Abstract FactoryProvides an interface for creating families of related objects without specifying their concrete classes.A factory that creates related components for different operating systems (Windows GUI elements, Mac GUI elements).
BuilderSeparates the construction of a complex object from its representation, allowing the same construction process to create different representations.Building a complex Pizza object with many optional fields (crust, toppings, sauce).
PrototypeCreates new objects by cloning an existing object (the prototype).Creating many identical enemy characters in a game by copying one master object.

2. Structural Patterns 🏗️

These patterns deal with class and object composition, helping to organize large applications by defining simple ways for objects to relate to each other.

PatternGoalExample
AdapterAllows the interface of an existing class to be used as another interface. Wraps one object around another.Converting a round peg to fit into a square hole. In code, converting an old API to match a new interface.
CompositeComposes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.A file system where a folder (composite) contains files (leafs) and other folders.
DecoratorAttaches additional responsibilities to an object dynamically.Adding features like sprinkles, whipped cream, or extra shots to a basic Coffee object at runtime.
FacadeProvides a unified interface to a set of interfaces in a subsystem, making the subsystem easier to use.A single HomeTheater object that handles all the complexities of turning on the TV, receiver, and Blu-ray player.
BridgeDecouples an abstraction from its implementation so that the two can vary independently.Separating a Shape abstraction from a DrawingAPI implementation (e.g., drawing with OpenGL vs. DirectX).
ProxyProvides a surrogate or placeholder for another object to control access to it.A SecurityProxy that checks a user's permissions before allowing them to access a sensitive file.

3. Behavioral Patterns 🔄

These patterns deal with communication and interaction between objects, focusing on the responsibility distribution between them.

PatternGoalExample
Observer (Event Listener)Defines a one-to-many dependency so dependents are notified of changes. (The pattern we discussed previously!)The Stock Exchange example where multiple traders are notified of a price change.
StrategyDefines a family of algorithms, encapsulates each one, and makes them interchangeable.Implementing different sorting algorithms (QuickSort, MergeSort) and allowing the client to select one at runtime.
Template MethodDefines the skeleton of an algorithm in a superclass but lets subclasses override specific steps without changing the structure.Defining the steps for building a house (foundation, walls, roof), but letting subclasses define the materials (wood house, brick house).
CommandEncapsulates a request as an object, allowing you to parameterize clients with different requests, queue or log requests, and support undoable operations.A UI menu where each button press is wrapped as a Command object (e.g., SaveCommand, OpenCommand).
IteratorProvides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.Using a List's built-in Iterator to traverse elements without knowing if it's an ArrayList or LinkedList.
StateAllows an object to alter its behavior when its internal state changes. The object appears to change its class.A traffic light object whose behavior changes depending on its internal state (Red, Yellow, or Green).
Published on: Oct 05, 2025, 10:59 AM  
 

Comments

Add your comment