Package Diagrams
A Package Diagram shows how a system is organized into packages (namespaces/modules) and the dependencies between them. It's a high-level architectural view that helps you understand the big picture before diving into class-level details.
Package Diagram Notation:
Package:
ββββββββββββββββββββββββββ
β Β«packageΒ» β
β PackageName β
ββββββββββββββββββββββββββ€
β β
β Classes inside β
β β
ββββββββββββββββββββββββββ
Dependency (dashed arrow):
PackageA - - - - -βΆ PackageB
"PackageA depends on PackageB"
Import (solid arrow with Β«importΒ»):
PackageA βββββΆ PackageB Β«importΒ»
"PackageA imports PackageB's public elements"
Example: E-Commerce Architecture
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Β«presentationΒ» β
β Presentation Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β WebUI β β MobileUI β β AdminUI β β
β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β
βββββββββββΌββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββ
β - - - - - - - - β - - - - - - - - β
βΌ βΌ βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Β«applicationΒ» β
β Application Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β OrderService β β UserService β β PaymentServiceβ β
β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β
βββββββββββΌββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββ
β - - - - - - - - β - - - - - - - - β
βΌ βΌ βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Β«domainΒ» β
β Domain Layer β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β Order β β Customer β β Product β β Payment β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β - - - - - - - - β - - - - - - - - β
βΌ βΌ βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Β«infrastructureΒ» β
β Infrastructure Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β DatabaseRepo β β EmailService β β PaymentGatewayβ β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Dependency Rule:
Dependencies point INWARD (toward the domain).
Outer layers depend on inner layers, never the reverse.
Domain layer has ZERO dependencies on infrastructure.
Layered Architecture
The most common architectural pattern β organizing code into horizontal layers with clear dependency rules:
βββββββββββββββββββββββββββββββββββββββββββ
β Presentation Layer β
β β’ UI components, controllers β
β β’ Handles user input and display β
βββββββββββββββββββββββββββββββββββββββββββ€
β Application Layer β
β β’ Use cases, application services β
β β’ Coordinates domain objects β
βββββββββββββββββββββββββββββββββββββββββββ€
β Domain Layer β
β β’ Business entities and rules β
β β’ Core logic, no framework dependenciesβ
βββββββββββββββββββββββββββββββββββββββββββ€
β Infrastructure Layer β
β β’ Database access, external APIs β
β β’ File system, network communication β
βββββββββββββββββββββββββββββββββββββββββββ
Benefits:
β Clear separation of concerns
β Each layer can be tested independently
β Infrastructure can be swapped without changing domain
Drawbacks:
β Can become rigid (change in one layer affects others)
β Performance overhead from layering
Package Design Principles
1. Stable Dependencies Principle
ββββββββββββββββββββββββββββββ
Depend in the direction of stability.
Stable packages should not depend on unstable ones.
2. Stable Abstractions Principle
ββββββββββββββββββββββββββββββ
The more stable a package, the more abstract it should be.
3. Common Reuse Principle
βββββββββββββββββββββββ
Classes that are reused together should be packaged together.
4. Common Closure Principle
ββββββββββββββββββββββββ
Classes that change for the same reason should be in the same package.
Package Organization Strategies:
By Feature (recommended for most projects):
ββββββββββββ ββββββββββββ ββββββββββββ
β Orders β β Products β β Users β
β β β β β β
β β’ Model β β β’ Model β β β’ Model β
β β’ Serviceβ β β’ Serviceβ β β’ Serviceβ
β β’ Repo β β β’ Repo β β β’ Repo β
β β’ API β β β’ API β β β’ API β
ββββββββββββ ββββββββββββ ββββββββββββ
By Layer (traditional):
ββββββββββββββββββββββββββββββββ
β Controllers β
ββββββββββββββββββββββββββββββββ€
β Services β
ββββββββββββββββββββββββββββββββ€
β Models β
ββββββββββββββββββββββββββββββββ€
β Repositories β
ββββββββββββββββββββββββββββββββ