Clean Architecture
Clean Architecture
• Characteristics:
Modules • Characteristics:
• They focus on “how” task are executed.
• They are specific implementation of abstract
contract/interfaces.
Coupled Design Decoupled
Design
Coupling
• Coupling: Coupling refers to the degree of dependency between two
modules or components in a system.
and
• Tight Coupling: Modules are heavily dependent on each other.
In other word changing in one module may require changes in
others.
• Loose Coupling: Modules have minimal dependency. Changes
• Example:
• TransactionManager class should handle creating,
updating transactions
• ReportGenerator class should be responsible for
generating reports.
2. Open/Close Principle (OCP)
Software entities (class, module, functions) should be
open for extension but closed for modification.
• Example:
• A PaymentProcessor class uses a
PaymentStrategy interface. Adding a new
payment method (e.g. PayPal) involves
creating a new implementation of
PaymentStrategy without changing
PaymentProcessor
3. Liskov Substitution Principle (LSP)
Subtype must be substitutable for their base types.
• Example:
• A BankAccount base has methods like deposit() and
withdraw(). A SavingAccount and CurrentAccount
class derived from bank account must implement
these method without altering expected behavior
(e.g., withdraw(), should not through unexpected
exceptions unless account-specific constraints are
met)
4. Interface Segregation Principle (ISP)
No client should be forced to depend on method it does
not use.
• Example:
• Instead of a single BankService interface with
methods like tranferFunds(),
calculateInterest() and applyForLoan()
create separate interfaces such as
FundTranferService,
InterestCalculationService and LoanService
5. Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level
modules; both should depend on abstractions.
Example:
• A NotificationService depends on a
NotificationSenderInterface not on specific
implementations like
EmailNotificationSender or
SMSNotificationSender.
What is Clean Architecture?
• History
• In 1996 Robert C. Martin (Commonly known as Uncle Bob) introduced
Dependency Inversion Principle in SOLID principle.
• In 2012 Uncle Bob formalized these ideas in his book Clean Architecture: A
Craftsman's Guide to Software Structure and Design
• Definition
• An architectural pattern that emphasizes on independent layers.
• Designed for maintainability, scalability and testability.
• Key Characteristics
• Clear Separation of Concerns.
• Dependency Direction: Inner layers must not depend on outer layers.
Think of it like an onion. With outermost
layers that can easily be peeled off, without
damaging any of its insides.
• Examples:
• Business models (e.g., User, Product, Order)
• Domain services (e.g., validations, calculation rules)
2. Application
• Examples:
• Use cases
3. Interface Adapters
• Examples:
• Controllers/Presenters/ViewModels
• Data Mappers
4. Frameworks and Drivers (Outermost Layer)
• Example:
• UI frameworks (e.g., Jetpack Compose, Swift UI)
• Libraires (e.g., Room, Core Data)
• Device Hardware and Services (e.g., GPS, Camera)
• APIs (e.g., Push Notification, Firebase)
Dependency Rules
The fundamental rule of Clean Architecture is the Dependency Rule:
• Inner layers must not depend on outer layers
• Dependencies should always point inward, towards the higher-
level policies.
This means:
• Frameworks and Drivers (Outermost layer) depends on
Interface Adapters.
• Interface Adapters depends on the Application layer.
• The Application layer depends on the Entities layer.
• The Entities layer depends on nothing.
Implementation
Q&A
Clean up your doubts ☺
Credits
• Book
Clean Architecture: A Craftsman's Guide to Software Structure and Design
• Link: https://amzn.eu/d/i2wRMjh
• Blogs
• Solid Principle in Pictures
• Link: https://medium.com/backticks-tildes/the-s-o-l-i-d-principles-in-pictures-b34ce2f1e898
• Clean Architecture
• Link: https://medium.com/@rudrakshnanavaty/clean-architecture-7c1b3b4cb181
• Clean Architecture for the rest of us
• Link: https://pusher.com/tutorials/clean-architecture-introduction
• Tools
draw.io: For flow chart designs
Plant UML(PUML): for uml designs
Thanks
Code Clean, Code Smart