ππππ πππππππ ππ π ππππππ ππππ πππππππππππ, πππππ π ππππ πππ ππππππ πππππ, πΌπππππ’ πππ ππ‘ππππππ πππππ πππ π³πππππ ππ‘ππππππ πππππ ππππππππππ πππ πππ π²πππ³ ππππππππππ. πππ πππππ πππ ππππππ ππ πππ ππππππ, π±ππππ ππ ππ πΌπ π πΌ-πππππ πππππ ππππππππππππ, πππ πππππππ ππ πππ ππππππππππππ πππ’ππ
- Having one concern per component, Meaning each piece of our architecture does only one thing and nothing else. If one component is doing more than one thing, we need to split them
- It is structured layered dependency. It is not specific to mobile development instead its software development architecture.
- Clean architecture term comes from Uncle bob, He created this methodology for building software products.
- It actually uses existing architectures like
mvvm
and other and additionally usesuse-cases
etc - It makes your applicaiton scalable majorly involves, how easy it is to replace a layer of application without breaking the other layers.
- A large code base comes with challenge of structuring it which involves how easy it is to extend it, test the functions in it, understand it
π°πππππππππ ππ πππππ ππππππππππππ
Advantages |
How its useful |
---|---|
Strict architecture |
Because of how the layers of code are organized it is not easy to make mistakes |
Encapsulation |
Each layer is organized into its own part having its own responsibility |
Parallel development |
If we have each feature in module wise each of the modules are built parallely |
Highly scalable |
Once organized properly, we can grow a small project into large one without bumping to problems |
Simple and easy |
Due to single responsibility its way easier to understand each snipped of code on what it does |
Testing |
Since all the layers are decoupled, we can write unit tests on them easily |
ππππππ ππππππππππππππ’ πππππππππ
- A class should have only one job.
- A class should have only one reason to change.
- If the class has two reasons to change, Then the class has to be split up into two components.
πΎπππ π²πππππ πππππππππ
- It should be open for extension, but closed for modification.
- If new functionality needs to be added, it should be added as an extension of the class.
- This allows us to abstract the stable functionality.
- If functionality is working and stable and we add a new feature on top of the existing feature for it. Now we will have stable functionality always working.
- Thus the volatile functionality will be in the extended classes and the stable functionality in the extended classes.
π»πππππ ππππππππππππ πππππππππ
- Lower level classes can be substituted without affecting higher levels.
- This is achieved using abstract classes and interfaces.
πΈππππππππ πππππππππππ πππππππππ
- We use an interface to advertise the functionality.
- Many specific interfaces are better than a generic interface.
- Interface just exposes the methods the dependent classes needed and nothing else.
π³πππππππππ’ πππππππππ πππππππππ
- Concrete classes depend on abstract classes and not the other way around.
- Volatile and stable functionalities are encapsulated, having the dependency relationship between the two.
- Framework-specific functionality depends on business logic with a dependency between them.
π»ππ’πππ ππ πππππππ ππππππππππππ
ππππππππππ’:
- Here in the project we have database, We use the database and call the functions of DAO in our repository.
- The repository directly accesses our datasources, wither API or database.
- The repository takes these datasources and determine which data has to be forwarded to the corrosponding use-cases.
- Say if you have two data sources(API,Cache), The repository needs to determine, do we load the data fro API or the cache.
- The decesion logic of determing the choosing of data source and determining if there is any errors during this.
ππππ²ππππ:
- Use-cases shouldn't know where the repository gets the data from.
- They just get the data and thats it.
- Contains the business logic.
- Makes code very redable, Because essentially the use-case is is something that does one thing.
- We can just know what a class does by just reading the name of the class. Ex:
DeleteNoteUseCase
determines this is used to delete a note. - They make code very re-usable, because in the end the
view-model
call the use-cases. - If you implement the all the business logic in view-model and say if you need that logic in a different view model its not possible, Using a use-case overcomes this by re-using hte use-case class in a different view-model.
ππππππππππ’ ππππππππππ ππ πππ ππππππ πππ’ππ:
- This is useful because we can create fake versions of the repository for testing
- We can pass fake repository to use cases for testing so that use cases won't know where the data is coming from
- Say its from a real repository or a local json file, They just get data and do something from it
π πππ -πΌππππ:
- When we just use a
mvvm
architecture, we have all the business logic in theview-model
- But when we use
clean-architecture
in combination withmvvm
, the business logic is lifted touse-case
layer. - So the view model puts some inputs to a use-case and performs some computation based on the input and gets the result and delegates the resut as a state to the presentation(UI) layer.
πΏππππππ πππππππππ ππ πππ πππππππ
π¦
Project Folder
.
βββ app
| |
β βββ> DI
| |
β |ββ> Feature
| | |
| | |ββ-------------> Data
| | | |
| | | |ββ-------------> DataSource
| | | |
| | | βββ-------------> Repository
| | |
| | |ββ-------------> Domain
| | | |
| | | |ββ-------------> Model
| | | |
| | | |ββ-------------> RepositoryDefinition
| | | |
| | | |ββ-------------> UseCase
| | | |
| | | βββ-------------> Util
| | |
| | βββ-------------> Presentation
| | |
| | |ββ-------------> Screen1
| | | |
| | | |
| | | βββ-------------> Components
| | |
| | βββ-------------------------------> MainActivity
| |
| |ββ> UI
| | |
| | βββ--------------------------------------------------> Theme
| |
β βββ-------------------------------------------------------> ApplicationClass
π½ππππ π»πππ |
π
πππ π½πππ |
π΄πππ ππππ |
---|---|---|
π½ππππ π»πππ |
π
πππ π½πππ |
π΄πππ ππππ |
---|---|---|
Demo.mp4
If you feel like support me a coffee for my efforts, I would greatly appreciate it.
Read contribution guidelines for more information regarding contribution.
Feature requests are always welcome, File an issue here.
Support it by clicking the β button on the upper right of this page. βοΈ
This project is licensed under the Apache License 2.0 - see the LICENSE file for details