githubEdit

Design Patterns

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code.


Pattern Categories

Design patterns are categorised into three main types based on their purpose:

Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

Patterns: Abstract Factory, Builder, Factory Method, Prototype, Singleton

Explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

Patterns: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

Concerned with algorithms and the assignment of responsibilities between objects.

Patterns: Chain of Responsibility, Command, Iterator, Mediator, Observer, State, Strategy, Template Method, Visitor


Quick Reference Table

Pattern
Purpose
Use When

Singleton

Ensure a class has only one instance and provide a global access point to it

Need global access point

Factory Method

Create objects without specifying the exact class of object that will be created

Subclasses decide which class to instantiate

Abstract Factory

provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Need consistent product families

Builder

separates the construction of a complex object from its representation

Object has many optional parameters

Prototype

used to clone existing objects instead of constructing them from scratch.

Object creation is expensive

Adapter

Allows objects with incompatible interfaces to collaborate. Acts as a wrapper/translator.

Integrate legacy code

Bridge

used to decouple an abstraction from its implementation so that the two can vary independently.

Both can vary independently

Composite

allows you to compose objects into tree structures to represent part-whole hierarchies.

Tree structures needed

Decorator

Dynamically adds behavior to an object without altering its structure. Avoids "Class Explosion".

Extend functionality without inheritance

Flyweight

minimize memory usage by sharing as much data as possible with similar objects.

Many similar objects

Facade

Provides a simplified interface to a complex subsystem

Reduce coupling to subsystem

Proxy

Provides a placeholder for another object to control access to it (Lazy loading, Access control, Logging).

Lazy initialization, access control

Command

encapsulates a request as an object, allowing for more flexible and dynamic command handling. In the upcoming sections,

Queue, log, or undo operations

State

Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Object behavior depends on state

Observer

Define a one-to-many dependency so that when one object changes state, all its dependents are notified and updated automatically.

One-to-many dependency

Strategy

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Algorithm should be interchangeable

Visitor

lets you add new operations to existing class hierarchies without modifying the classes themselves.

Operations on object structure

Mediator

centralizes complex communication between objects into a single mediation object.

Reduce object dependencies

Iterator

provides a way to access the elements of a collection sequentially without exposing the underlying representation.

Abstract collection traversal

Chain of Responsibility

sets up a chain where each team can either process the request or pass it to the next team in the chain.

Multiple objects may handle request

Template Method

provides a blueprint for executing an algorithm. It allows subclasses to override specific steps of the algorithm, but the overall structure remains the same.

Subclasses override specific steps


Learning Path

Beginner

Start with these fundamental patterns:

  1. Singleton - Simplest creational pattern

  2. Factory Method - Foundation for other patterns

  3. Strategy - Easy to understand behavioral pattern

  4. Observer - Common in event-driven systems

Intermediate

  1. Decorator - Flexible alternative to inheritance

  2. Adapter - Common in integration scenarios

  3. Template Method - Good for frameworks

  4. Command - Important for undo/redo

Advanced

  1. Abstract Factory - Complex but powerful

  2. Builder - Essential for fluent APIs

  3. Composite - Tree structures

  4. Visitor - Most complex, but very useful


Interview Tips

Common Questions:

  • Explain the difference between Strategy and State patterns

  • When would you use Abstract Factory vs Factory Method?

  • How does Decorator differ from inheritance?

  • What problems does Singleton solve and what are its drawbacks?

Be Ready to:

  • Draw UML diagrams

  • Write code examples

  • Discuss real-world use cases

  • Explain trade-offs


Real-World Examples

  • Singleton: Database connection pool, Logger

  • Factory: UI component libraries (ButtonFactory)

  • Observer: Event handling systems, MVC

  • Decorator: Java I/O streams, UI component wrappers

  • Strategy: Payment processing, sorting algorithms

  • Adapter: Legacy system integration

  • Facade: Simplified API for complex libraries


Anti-Patterns to Avoid

God Object: One class does everything ❌ Spaghetti Code: No clear structure ❌ Golden Hammer: Using same pattern everywhere ❌ Premature Optimization: Overengineering simple problems


Resources

Last updated