← Back to list

Design Patterns — Gang of Four

In software engineering, many problems repeat themselves across applications, domains, and technologies. Design Patterns provide proven…

Kaizen Chandra · 2026-02-22 12:53 · 0 claps · 4.8 min read
#desing-pattern #gang-of-four #software-design-patterns #design-pattern-in-java
Open on Medium ↗
Wiki topics: 💻 · Programming

Design Patterns — Gang of Four

In software engineering, many problems repeat themselves across applications, domains, and technologies. Design Patterns provide proven, reusable solutions to these commonly occurring problems in software design.

A design pattern is not code, but a general solution template that can be adapted to different situations.

Design patterns are to software design what blueprints are to architecture.

What Is a Design Pattern?

A Design Pattern is:

  • A reusable solution to a commonly occurring design problem
  • A best practice refined through experience
  • A language for developers to communicate design ideas clearly

Why Do We Need Design Patterns?

Problems Without Design Patterns

  • Tight coupling
  • Difficult maintenance
  • Code duplication
  • Hard-to-test code
  • Poor scalability

When Should You Use Design Patterns?

Use design patterns when:

  • You identify repeating design problems
  • You want flexibility for future changes
  • You are designing frameworks, libraries, or APIs
  • You want to follow SOLID principles

Core Characteristics of Design Patterns

A design pattern:

  • Solves a specific design problem
  • Is language independent
  • Is time-tested
  • Encourages object-oriented principles
  • Improves maintainability and extensibility

Structure (Specification) of a Design Pattern

Each design pattern typically consists of the following components:

1. Pattern Name

A meaningful name to describe the problem and solution

Example: Singleton, Factory, Observer

2. Problem

Describes when and why to apply the pattern

3. Intent

Explains what the pattern does

4. Solution

Describes the structure and relationships between classes/objects

5. Participants

Lists the classes or interfaces involved

6. Consequences

Trade-offs and results of using the pattern

  • Pros
  • Cons

Design patterns are broadly categorized based on what problem they solve in software design.

The Gang of Four (GoF) classified design patterns into three main types:

  1. Creational Design Patterns — Object creation

They abstract the object instantiation process, making the system independent of how objects are created, composed, and represented.

2. Structural Design Patterns — Class & object composition

Structural patterns deal with how classes and objects are composed to form larger structures.

They focus on:

  • Relationships between objects
  • Simplifying complex systems
  • Ensuring flexibility in structure

3. Behavioral Design Patterns — Object interaction & responsibility

Behavioral patterns focus on communication between objects and how responsibilities are distributed.

They help define:

  • How objects interact
  • How behavior changes at runtime
  • How responsibilities are assigned

Design Patterns in Spring Framework

Spring internally uses multiple design patterns:

  • Singleton → Default bean scope
  • Factory → BeanFactory
  • Proxy → AOP
  • Strategy → HandlerAdapter
  • Observer → Application events
  • Template → JdbcTemplate, RestTemplate, KafkaTemplate

Creational Patterns (5)

1️Singleton

Ensures a class has only one instance and provides a global access point to it. Commonly used for configuration managers, logging systems, or caching services. It controls instantiation, prevents multiple object creation, and maintains shared state across the application lifecycle in a consistent and centralized manner.

2️Factory Method

Defines an interface for creating objects but allows subclasses to decide which class to instantiate. It promotes loose coupling by eliminating the need to bind application-specific classes into code. Instead of direct instantiation, object creation is delegated to child classes through overridden factory methods.

3️Abstract Factory

Provides an interface for creating families of related or dependent objects without specifying their concrete classes. It ensures consistency among related products. Common in UI frameworks or cross-platform applications where multiple product variants must be used together without tightly coupling to implementations.

4️Builder

Separates the construction of a complex object from its representation so that the same construction process can create different representations. It allows step-by-step object creation and improves readability. Frequently used when objects require many parameters or optional configurations.

5️Prototype

Creates new objects by copying an existing object, known as the prototype. It avoids expensive object creation and allows cloning with modifications. Particularly useful when object initialization is costly or when creating many similar instances efficiently.

Structural Patterns (7)

6️Adapter

Converts the interface of a class into another interface clients expect. It enables incompatible classes to work together without modifying their source code. Common in integrating third-party libraries or legacy systems into modern architectures.

7️Bridge

Decouples abstraction from its implementation so both can vary independently. It prevents a combinatorial explosion of subclasses. Often used when both abstraction and implementation may change independently, improving scalability and flexibility.

8️Composite

Composes objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions uniformly. Widely used in UI hierarchies, file systems, or organizational structures.

9️Decorator

Adds additional behavior to objects dynamically without altering their structure. It wraps the original object and extends functionality at runtime. Preferred over subclassing when behavior must be added flexibly and transparently.

1️0️Facade

Provides a simplified interface to a complex subsystem. It hides internal complexity and exposes a unified interface for easier usage. Common in APIs, libraries, or layered architectures to reduce coupling and simplify client interactions.

1️1️ Flyweight

Reduces memory usage by sharing common parts of state between multiple objects. It separates intrinsic and extrinsic states to minimize duplication. Frequently used in rendering systems, caching mechanisms, or scenarios involving many similar objects.

1️2️ Proxy

Provides a placeholder or surrogate for another object to control access to it. It can add security, lazy loading, logging, or caching. Often used in remote services, virtual proxies, or access control mechanisms.

Behavioral Patterns (11)

1️3️ Chain of Responsibility

Passes a request along a chain of handlers. Each handler decides either to process the request or forward it. It reduces coupling between sender and receiver and is common in middleware systems, logging frameworks, or event processing pipelines.

1️4️ Command

Encapsulates a request as an object, allowing parameterization of clients with queues, logs, and undo operations. It decouples sender from receiver and is widely used in GUI actions, job queues, and transactional systems.

1️5️ Interpreter

Defines a representation for grammar and provides an interpreter to process sentences in that language. Useful for domain-specific languages, query parsing, or rule engines where expressions must be evaluated systematically.

1️6️ Iterator

Provides a way to access elements of a collection sequentially without exposing its internal representation. It promotes encapsulation and simplifies traversal logic across different collection types.

1️7️ Mediator

Defines an object that centralizes communication between multiple objects. It reduces direct dependencies between interacting components. Often used in UI components or complex interaction systems.

1️8️ Memento

Captures and externalizes an object’s internal state so it can be restored later without violating encapsulation. Commonly used in undo/redo functionality and state restoration systems.

1️9️ Observer

Defines a one-to-many dependency so that when one object changes state, all dependents are notified automatically. Widely used in event-driven systems, messaging frameworks, and reactive programming models.

2️0️ State

Allows an object to alter its behavior when its internal state changes. The object appears to change its class. Useful in workflows, game development, or finite state machines.

2️1️ Strategy

Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows selecting behavior at runtime. Frequently used in payment processing, sorting logic, or validation mechanisms.

2️2️Template Method

Defines the skeleton of an algorithm in a method while allowing subclasses to redefine specific steps. Promotes code reuse and ensures consistent algorithm structure across implementations.

2️3️ Visitor

Represents an operation to be performed on elements of an object structure without changing the classes. It separates algorithms from objects, making it easier to add new operations.


메타데이터
post_id
4a2a48756463
slug
design-patterns-gang-of-four-4a2a48756463
url
https://medium.com/@code.chandrashekhar/design-patterns-gang-of-four-4a2a48756463
canonical_url
https://medium.com/@code.chandrashekhar/design-patterns-gang-of-four-4a2a48756463
author_url
https://medium.com/@code.chandrashekhar
status
ok
fetched_at
2026-07-15 15:45:51