01. Introduction
(From Design Patterns: Elements of Reusable Object-Oriented Software)
1.1 What Is a Design Pattern?¶
Definition¶
A design pattern is a general, reusable solution to a commonly occurring problem in software design within a given context.
Key Characteristics¶
A design pattern:
-
Is not code, but a template/blueprint
-
Solves a design problem, not an implementation detail
-
Is language-independent
-
Is proven through repeated use
-
Improves communication among developers
What a Design Pattern Is NOT¶
-
Not an algorithm
-
Not a finished design
-
Not a framework
-
Not a library
Why Design Patterns Matter¶
-
Promote reusability
-
Improve maintainability
-
Reduce coupling
-
Increase flexibility
-
Provide a shared vocabulary (e.g., “use Observer here”)
1.2 Design Patterns in Smalltalk MVC¶
MVC (Model–View–Controller)¶
MVC is an early example of a design pattern system used in Smalltalk.
Components¶
-
Model
- Represents core data and business logic
-
View
- Displays data to the user
-
Controller
- Handles user input and updates the model
Design Patterns Inside MVC¶
MVC itself is not a single pattern but a combination of patterns:
-
Observer
- Views observe the Model for changes
-
Strategy
- Controllers act as interchangeable strategies
-
Composite
- Views can be nested
Why MVC Is Important¶
-
Demonstrates how multiple patterns collaborate
-
Encourages separation of concerns
-
Influenced modern frameworks (Spring MVC, Django, Rails)
1.3 Describing Design Patterns¶
GoF uses a standard template to describe every pattern.
Pattern Description Elements¶
-
Pattern Name
- A handle to communicate design ideas
-
Intent
- What the pattern does and why
-
Motivation
- Problem scenario explaining need
-
Applicability
- When to use the pattern
-
Structure
- UML-style class relationships
-
Participants
- Classes/objects involved
-
Collaborations
- How participants interact
-
Consequences
- Trade-offs (flexibility vs complexity)
-
Implementation
- Pitfalls and tips
-
Sample Code
-
Known Uses
-
Related Patterns
Interview Tip¶
If asked to explain a pattern, Intent + Applicability + Consequences is usually sufficient.
1.4 The Catalog of Design Patterns¶
The GoF catalog contains 23 design patterns.
Purpose of the Catalog¶
-
Provide standard solutions
-
Avoid reinventing the wheel
-
Encourage best practices
Pattern Scope¶
Patterns differ by:
-
Purpose (what they do)
-
Scope (class vs object)
1.5 Organizing the Catalog¶
Classification by Purpose¶
-
Creational Patterns
-
Object creation mechanisms
-
Example: Factory, Singleton
-
-
Structural Patterns
-
Class/object composition
-
Example: Adapter, Decorator
-
-
Behavioral Patterns
-
Object interaction and responsibility
-
Example: Observer, Strategy
-
Classification by Scope¶
| Scope | Meaning |
|---|---|
| Class | Uses inheritance |
| Object | Uses composition |
Why This Organization Helps¶
-
Makes pattern selection easier
-
Highlights design intent
-
Clarifies trade-offs
1.6 How Design Patterns Solve Design Problems¶
Core Design Problems Addressed¶
-
Finding Appropriate Objects
- Identify responsibilities, not classes
-
Determining Object Granularity
- Avoid God objects
-
Specifying Object Interfaces
- Program to interfaces, not implementations
-
Managing Dependencies
- Reduce tight coupling
-
Making Code Reusable
- Favor composition over inheritance
Fundamental OO Principles Used¶
-
Encapsulation
-
Abstraction
-
Polymorphism
-
Composition over inheritance
-
Loose coupling
1.7 How to Select a Design Pattern¶
Steps to Choose a Pattern¶
-
Understand the problem
-
Study pattern intents
-
Consider causes of redesign
-
Examine patterns with similar intent
-
Evaluate consequences
-
Consider scalability and flexibility
Interview Rule of Thumb¶
Do not force patterns.
Use them only when the problem context fits.
1.8 How to Use a Design Pattern¶
Steps to Apply a Pattern¶
-
Read the pattern completely
-
Understand its intent and structure
-
Identify participants in your design
-
Adapt names and structure
-
Implement incrementally
-
Refactor if needed
Common Mistakes¶
-
Overengineering
-
Blindly applying patterns
-
Using patterns without understanding trade-offs
Quick Interview Summary (1-Minute Revision)¶
-
Design patterns are reusable design solutions
-
GoF catalog contains 23 patterns
-
MVC demonstrates pattern collaboration
-
Patterns are described using a standard template
-
Classified into Creational, Structural, Behavioral
-
Patterns improve flexibility, maintainability, communication
-
Use patterns judiciously, not everywhere