Complete guide to Modeling Design Patterns in UML: learn how to represent Gang of Four patterns, architectural patterns, and common design patterns using UML diagrams. Master pattern modeling techniques.
Design patterns are reusable solutions to common problems in software design. They represent best practices evolved over time and provide a common vocabulary for discussing design solutions.
UML is an excellent tool for modeling design patterns because:
Patterns involve relationships between classes/components
UML diagrams clearly show pattern structure
Patterns can be documented and communicated effectively
UML helps teams understand and apply patterns consistently
The Gang of Four (GoF) patterns are categorized into:
Creational: Object creation patterns
Structural: Object composition patterns
Behavioral: Object interaction patterns
2. Creational Patterns
2.1 Singleton Pattern
Ensures a class has only one instance and provides global access.
@startuml
class Singleton {
- instance : Singleton {static}
+ getInstance() : Singleton {static}
- Singleton()
}
note right of Singleton
Private static instance variable
Private constructor
Public static getInstance() method
end note
@enduml
Key elements:
Private static instance variable
Private constructor
Public static getInstance() method
2.2 Factory Pattern
Creates objects without specifying the exact class.
@startuml
abstract class Product
class ProductA
class ProductB
class Factory {
+ create() : Product
}
Product <|-- ProductA
Product <|-- ProductB
Factory ..> Product : creates
@enduml
2.3 Builder Pattern
Constructs complex objects step by step.
@startuml
class Director {
+ construct()
}
abstract class Builder {
+ buildPart()
}
class ConcreteBuilder1
class ConcreteBuilder2
class Product
Director --> Builder
Builder <|-- ConcreteBuilder1
Builder <|-- ConcreteBuilder2
ConcreteBuilder1 ..> Product
ConcreteBuilder2 ..> Product
@enduml
Separates application into three interconnected components.
@startuml
class View {
+ display()
}
class Controller {
+ handleInput()
}
class Model {
+ getData()
+ setData()
}
View --> Controller : uses
Controller --> Model : manipulates
View ..> Model : observes
@enduml
5.2 Layered Architecture
Organizes system into layers with specific responsibilities.
@startuml
package "Presentation Layer" {
class Controller
}
package "Business Layer" {
class Service
}
package "Data Layer" {
class Repository
}
Controller ..> Service : depends on
Service ..> Repository : depends on
@enduml
0 Comments