StudyDSA logoStudyDSA

Command Palette

Search for a command to run...

Sign InSign Up
Sign Up

Factory MethodAbstract FactoryBuilderPrototypeSingletonAdapterBridgeCompositeDecoratorFacadeFlyweightProxyChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyTemplate MethodVisitor

Definition
StudyDSA

Where complexity meets clarity.
By Armas Zarra.

Topics

  • Data Structures
  • Algorithms
  • Big-O Notation
  • Robotics
  • AI Research
  • Machine Learning

Practice

  • Blind 75
  • LeetCode 75
  • NeetCode 150

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Armas Films LLC

IntroductionDesign Patterns

Design Patterns

Named, tested ways out of the problems object-oriented code keeps running into.

Definition

Code rarely dies from being wrong, it dies from being rigid: one new requirement forces edits in twenty files, and every change makes the next one harder. The same rigidity shows up in the same situations so often that the ways out have names. A design pattern is a reusable solution to a problem that keeps coming up in software design. It isn't code you copy and paste, it's a blueprint for arranging your classes and objects so the design stays flexible as the system grows. The patterns here come from the Gang of Four, who cataloged them in 1994 and sorted them into three families: creational, structural, and behavioral.

Creational Patterns

Creational patterns deal with how objects get made. Instead of scattering constructor calls and setup logic all over your code, they keep object creation in one flexible place. Five patterns live here, from the everyday Factory Method to the one-and-only Singleton.

Structural Patterns

Structural patterns are about how objects fit together into something bigger. The goal is to compose flexible systems out of smaller parts without everything turning brittle. There are seven of them. Decorator wraps one object inside another to add behavior, and Facade hides a tangled subsystem behind a single clean interface.

Behavioral Patterns

Behavioral patterns are the largest family, ten in all. They handle how objects talk to each other and split up the work, so the pieces stay loosely coupled instead of wired directly together. Observer, Strategy, and State all live here.

Learning Path

Here is the whole catalog, grouped by family. Each pattern will get its own page with diagrams and a code walkthrough soon. For now, use this as a map of how all 22 relate.

Creational

Every object starts with a constructor call, and scattering those calls around couples your code to concrete classes. These five decide who runs the constructor and when, from a single overridable factory all the way to a guaranteed lone instance.

Factory Method

Adding a new product type shouldn't mean editing every place that builds one. Subclasses decide what gets created.

CreatorProduct

Abstract Factory

Keeps a whole family consistent: ask for the dark theme and every button, menu, and dialog comes back matching.

Factory

Builder

A constructor with nine arguments is a trap. Build the object step by step, using only the steps you need.

obj

Prototype

When setup is expensive or hidden, don't rebuild. Clone a working example and adjust the copy.

objcopy

Singleton

One instance, guaranteed, reachable from anywhere. The most famous pattern, and the most abused one.

instance
Structural

Once objects exist, they have to fit together, and inheritance alone makes the combinations explode. These seven are composition moves instead: wrap to translate or add behavior, share to save memory, and hide whole subsystems behind one front.

Adapter

Your code expects one interface and the library speaks another. A thin wrapper translates so neither has to change.

ClientAdapterAPI

Bridge

Three shapes times four renderers shouldn't cost twelve classes. Split the two dimensions and they vary independently.

AbsImpl

Composite

A file and a folder of a thousand files answer the same call. One thing and a group of things, treated identically.

Decorator

Stack behaviors at runtime instead of multiplying subclasses. Wrap the object, add the feature, repeat.

core

Facade

One simple call in front of thirty tangled ones. Callers get a clean entry point and the subsystem keeps its mess private.

Facade

Flyweight

A million characters on screen, but only a handful of fonts. Share what repeats and memory stops exploding.

shared

Proxy

A stand-in with the same interface as the real thing, free to lazy-load, cache, or guard before passing the call through.

ifaceClientProxyReal
Behavioral

Built and wired together, objects still have to talk without hard-coding who calls whom. These ten separate the asker from the answerer, whether the thing in motion is a request, an algorithm, a state change, or an undo.

Chain of Responsibility

Nobody has to know who will handle the request. Each handler either takes it or passes it down the line.

Command

Once a request is an object, you can queue it, log it, and reverse it. Every undo stack is built on this.

Command

Iterator

Loop over a tree, a list, or a graph with the same code. The collection keeps its insides to itself.

Mediator

Ten objects talking directly means 45 connections. Route everything through one hub and they stop knowing each other.

Memento

Snapshots an object's private state without exposing it. Undo works because someone saved one of these.

state

Observer

One change triggers many reactions without any hard wiring. Subscribers sign up and get notified the moment something happens.

Subj

State

An object that acts like a different object in each state, without a giant switch statement in sight.

ctx

Strategy

Swap the algorithm without touching the code that calls it. Sorting, pricing, and routing rules become plug-ins.

ctx

Template Method

When five algorithms share one outline, write the outline once. Subclasses fill in only the steps that differ.

Visitor

New operations on a stable family of classes, added without touching a single one of them.

Visitor