Oct.12

Clean Architecture

Clean architecture divide software in different layers like onion and defines definition of dependency rule among those layers e.g. source code dependencies can only point inwards.

Clean architecture in general have four layers, but we can have less or more layers if necessary as long as we follow dependency rule strictly. 😃

Dependency Rule: Outer layers are mechanism and inner layers are policies. Inner layers can not know anything about outer layers e.g. anything declared in outer layers can not be mentioned in inner layers including variables, functions, classes or other entities. This rule is simple and says that source code dependencies can only point inwards.

Checkout: 😃 Photo Album 😍 (SOLID, MVVM, CLEAN)

Photo Album show usages of dependency rule. We may need more layers based on complexities and sizes of projects, but as long as we follow dependency rule, projects fall under clean architecture.

EntitiesCore Libraries: Entities encapsulate most general and high-level rules. Entities represent classes, functions, etc which are used by other outer layers. Operational or changes in outer layers should not effect entities. Examples: ServiceManager.

Use CasesModels: Use cases contain application specific business rules and orchestrate flow of data to and from entities, and direct those entities to use their rules to achieve goals of those use cases. Examples: AlbumListModel.

GatewaysViewModels: Gateways convert data from format most convenient for the use cases and entities, to format most convenient for some external agency such user interfaces. Examples: AlbumListActivityModel.

UIViews: UI mainly contain views, frameworks and tools. Generally we don’t write much code in this layer other than glue code that communicates to the next circle inwards. Examples: AlbumListActivity.

Remember: We can have as much layers as necessary but we have to follow dependency rule. Clean architecture is nothing but following dependency rule strictly. 🙂

Architecture,Principle