Design Patterns (Gang of Four) Cheat Sheet
Summarizes the classic Gang of Four creational, structural, and behavioral design patterns with a runnable Strategy and Singleton example.
2 PagesIntermediateApr 5, 2026
Creational Patterns
Patterns concerned with flexible object creation.
- Singleton- Ensures a class has only one instance and provides a global point of access to it
- Factory Method- Defines an interface for creating an object, letting subclasses decide which concrete class to instantiate
- Abstract Factory- Provides an interface for creating families of related objects without specifying their concrete classes
- Builder- Separates construction of a complex object from its representation, allowing step-by-step assembly
- Prototype- Creates new objects by cloning an existing instance instead of instantiating from scratch
Structural Patterns
Patterns for composing classes and objects into larger structures.
- Adapter- Converts the interface of a class into another interface that clients expect
- Decorator- Attaches additional responsibilities to an object dynamically without altering its class
- Facade- Provides a simplified, unified interface to a complex subsystem
- Composite- Composes objects into tree structures so clients treat individual objects and compositions uniformly
- Proxy- Provides a surrogate or placeholder for another object to control access to it
- Bridge- Decouples an abstraction from its implementation so the two can vary independently
Behavioral Patterns
Patterns focused on communication and responsibility between objects.
- Strategy- Defines a family of interchangeable algorithms and encapsulates each one behind a common interface
- Observer- Defines a one-to-many dependency so that when one object changes state, all its dependents are notified
- Command- Encapsulates a request as an object, allowing parameterization, queuing, and undoable operations
- Template Method- Defines the skeleton of an algorithm in a base class, deferring specific steps to subclasses
- Iterator- Provides a way to access elements of a collection sequentially without exposing its underlying representation
- State- Allows an object to alter its behavior when its internal state changes, appearing to change its class
Strategy & Singleton in Practice
Two of the most commonly used GoF patterns implemented in Python.
python
# Strategy patternclass DiscountStrategy: def apply(self, price: float) -> float: raise NotImplementedErrorclass NoDiscount(DiscountStrategy): def apply(self, price): return priceclass PercentageDiscount(DiscountStrategy): def __init__(self, percent): self.percent = percent def apply(self, price): return price * (1 - self.percent / 100)class Order: def __init__(self, strategy: DiscountStrategy): self.strategy = strategy def total(self, price): return self.strategy.apply(price)order = Order(PercentageDiscount(10))order.total(100) # => 90.0# Singleton patternclass Config: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance
Pro Tip
Favor composition-based patterns (Strategy, Decorator) over inheritance-based ones when behavior needs to vary at runtime — they keep classes open for extension without deep, fragile class hierarchies.
Was this cheat sheet helpful?
Explore Topics
#DesignPatternsGangOfFour#DesignPatternsGangOfFourCheatSheet#Programming#Intermediate#CreationalPatterns#StructuralPatterns#BehavioralPatterns#StrategySingletonInPractice#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance