Software Design Patterns: Principles and Implementation
Software Design Patterns and Architecture (CSC3324)
Defining Software Design Patterns
A reusable solution to a recurring design problem.
It reuses abstract knowledge about problems and solutions.
Independent of specific implementations, it acts as a template.
It utilizes Object-Oriented Programming (OOP) features such as inheritance and polymorphism.
The Christopher Alexander Definition
“Each pattern describes a problem which occurs over and over again... and then describes the core of the solution... you can use this solution a million times over without doing it the same way twice.”
Architecture vs. Design Patterns
| Aspect | Architecture | Design Patterns |
|---|---|---|
| Level | High-level | Lower-level (Micro-architecture) |
| Purpose | Structure of the whole application | Solve subproblems within the application |
| Focus | System components and interactions | Object collaborations |
| Examples | Client-server, SOA | Observer, Singleton, Factory, etc. |
Four Essential Elements of a Design Pattern
| Element | Meaning |
|---|---|
| Name | Unique identifier |
| Problem | Describes when to apply the pattern |
| Solution | Template for solving the problem |
| Consequences | Results and trade-offs of applying it |
Classification of Design Patterns
| Classification | Categories | Scope |
|---|---|---|
| By Purpose | Creational, Structural, Behavioral | |
| By Scope | Applies to classes or objects |
Core Design Pattern Categories
| Type | Purpose | Examples |
|---|---|---|
| Creational | Object creation mechanisms | Singleton, Factory, Builder, Prototype |
| Structural | Composing objects into larger structures | Adapter, Bridge, Composite, Façade |
| Behavioral | Communication between objects | Observer, Strategy, Iterator, Command |
The Observer Pattern Explained
| Element | Description |
|---|---|
| Name | Observer |
| Problem | The need to reflect state changes in multiple displays |
| Solution | Use Subject and Observer interfaces; Observers subscribe to changes |
| Consequences | Loose coupling between Subject and Observer, but limited optimization |
UML Structure of the Observer Pattern
Subject: Abstract class. It maintains a list of observers and notifies them of changes.ConcreteSubject: Stores the actual state and sends updates to observers.Observer: An interface for theupdate()method.ConcreteObserver: Receives updates and refreshes the display accordingly.
Observer Pattern in Practical Applications
This pattern is used when:
You need multiple views (e.g., a chart and a table) for the same data.
Changes in one place must propagate to all views automatically.
The subject should not know the specific details of its observers.
Common Design Patterns and Use Cases
| Pattern | Use Case Example |
|---|---|
| Façade | Simplifies or tidies up a set of complex APIs |
| Iterator | Provides a consistent way to loop through a collection |
| Decorator | Dynamically extends object functionality at runtime |
| Command | Encapsulates requests as objects for flexible undo/redo |
| State | Changes object behavior based on internal state |
| Strategy | Chooses different algorithms at runtime |
English with a size of 4.92 KB