Design Patterns
Design Patterns
Design Patterns
Fall 2024
What are Design Patterns?
• Descriptions of communicating objects and classes that are
customized to solve a general design problem in a particular context
• Gamma et al. described 23 design patterns divided into three
categories:
1. Creational Patterns
2. Structural Patterns
3. Behavioral Patterns
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 2
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Creational Patterns
• Concern the process of object creation
• Six creational patterns
1. Factory Method
2. Abstract Factory
3. Singleton
4. Prototype
5. Builder
6. Object Pool
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 3
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Structural Patterns
• Deal with the composition of classes or objects
• Seven structural patterns
1. Adapter
2. Bridge
3. Composite
4. Decorator
5. Facade
6. Flyweight
7. Proxy
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 4
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Behavioral Patterns
• Characterize the ways in which classes or objects interact and distribute
responsibility
• Ten Behavioral patterns
1. Chain of Responsibility
2. Command
3. Interpreter
4. Iterator
5. Mediator
6. Memento
7. Observer
8. State
9. Strategy
10. Template
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 5
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Singleton (Creational)
• Intent: Ensure a class has only one instance, and provide a global
point of access to it
Singleton
-instance: Singleton
-Singleton()
+getInstance(): Singleton
…
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 6
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Factory Method (Creational)
• Intent: Define an interface for creating an object, but let subclasses
decide which class to instantiate.
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 7
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Facade (Structural)
• Intent: Hide complexities and provide a unified interface to a set of
interfaces in a subsystem
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 8
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Adapter (Structural)
• Intent: Let classes work together that couldn't otherwise because of
incompatible interfaces
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 9
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Observer (Behavioral)
• Intent: Define a one-to-many dependency between objects so that
when one object changes state, all its dependents are notified and
updated automatically
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 10
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995
Strategy (Behavioral)
• Intent: Define a family of algorithms, encapsulate each one, and make
them interchangeable
Freeman et al., Head First Design Patterns, © 2004 O’Reilly Media, Inc. 11
Gamma et al., Design Patterns: Elements of Reusable Object-oriented Software, 1995