Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean architecture pattern Android

I have read several documents over clean architecture in general and Android specific as well.

I totally like the idea of creating a separate module for each new feature but my concern is how do I organize my data layer objects ? As I want them to be re-usable.

Should the data layer be a separate module alongside all the feature modules or the data layer should be broken down into components as modules ? eg. separate module for network, database etc ?

like image 787
geekoraul Avatar asked Mar 10 '16 00:03

geekoraul


People also ask

What is Android Clean Architecture?

Clean architecture is a method of software development in which you should be able to identify what a program performs merely by looking at its source code. The programming language, hardware, and software libraries needed to achieve the program's goal should be rendered obsolete.

What is the best architecture pattern for Android?

The most popular android architectures used by developers are the following: MVC (Model — View — Controller) MVP (Model — View — Presenter) MVVM (Model — View — ViewModel)

What is Clean Architecture pattern?

In a clean architecture, the application's dependency is designed for stability purposes. Since there will be components that aren't stable enough, they should depend on the more stable components. So, the code used in this pattern is easy to maintain, reuse, and decoupling.

What is MVVM Clean Architecture Android?

MVVM is just part of the clean architecture in the presentation layer . It just a set of rules on how to display the data from UseCase. One of the benefits of using clean architecture is we can change our design pattern in presentation layer without changing domain layer or use case.


2 Answers

Check out this project for a clean architecture framework for Android. https://github.com/Karumi/Rosie. To answer your question though - I personally separate the network components for each module, and inject them into the appropriate feature modules that need them. For example, imagine I'm creating some sort of twitter client - I might have a class FeedManager, which exposes methods to fetch the feed, and TweetManager, which exposes methods to create a new tweet. It's a little overkill for this example though, since FeedManager and TweetManager might be very small.

Be wary of over-architecting too early on. Having a single Network module that has methods for every network request in the app is a code smell and becomes hard to maintain as your app grows. But, if your app is small, having multiple Network classes that each do a tiny thing might also be overkill, and you'd likely benefit from having only a single networking module.

Also - don't feel like you have to go whole hog on Clean Architecture - it's ok to merge multiple layers into a single layer if it suits your app. I made this mistake while trying to implement VIPER (a clean architecture derivative) 'by the book', and ended up having several extra classes for each feature that basically did nothing but pass the data onto the next layer, and it became a huge hassle to maintain. Clean Architecture may be a godsend for a large complex project where separation of concerns to the extreme is necessary, but for most Android apps I've seen, the much simpler MVC, MVVM, or MVP will be good enough.

like image 52
eliasbagley Avatar answered Oct 17 '22 06:10

eliasbagley


First thing I would like to correct that your package architecture should be based on feature not the module architecture as you mentioned. Second, to accomodate all database related stuffs, just create a dedicated pakcage for the same.

Here is the screenshot of one of the project. May be it would help you. Here is the screenshot of one of the project. May be it would help you.

Ref & more detail

like image 21
Irfan Raza Avatar answered Oct 17 '22 06:10

Irfan Raza