Clean architecture pattern Android
Asked Answered
L

4

6

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 ?

Lindholm answered 10/3, 2016 at 0:55 Comment(2)
Check this github.com/android10/Android-CleanArchitectureLyonnesse
I have already seen the example and the Author has mentioned in the related blog post to not follow his design. This example has Layers at the top level, whereas the author proposes to have 'features' at the top levelLindholm
A
8

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.

Armed answered 13/3, 2016 at 4:41 Comment(4)
I agree that it might be an overkill but to implement clean architecture with dependency injection and RxJava feels like a very feasible choice in implementing tests (unit, instrumented and UI) effectively. Its very clear which test goes in which part of the code. Which is not so achievable if MVC, MVVM or MVP architecture is usedLindholm
@Lindholm Agreed about MVC being a pain to test - but you shouldn't have too much problem testing with MVVM or MVP, since much of the logic is just in POJO's. As long as you move your logic outside of the framework classes (Activity, Fragment, Service, etc), then it becomes trivial to unit test.Armed
Thanks for your answer, will wait for a few more views else will accept yours :) I don't really know why someone would downvote such a specific questionLindholm
I have written a blog post about android clean architecture. You might find it helpful. subash.com.au/android-clean-architectureSemibreve
C
2

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

Counterstatement answered 19/3, 2016 at 1:15 Comment(0)
B
2

There is no architecture, clean or not to rule them all. There is no 1 correct way to do things.

My best advice is to use these different ideas in your own projects and continuously iterate and improve on them until you have a good base that works for you. Try to implement something, then take a step back and review your work, see what works and what could be better.

I could give you a bunch of links, but it sounds like you already will have read most of them. You can't learn how to ride a bike by reading, get coding, and write about your findings!

Billionaire answered 19/3, 2016 at 20:41 Comment(1)
that was pretty eloquent but what I am trying to do here is to discuss a particular approach. I am in one of those iterations and I need validation through healthy discussion. A lot of time is consumed in those iterations and I am not willing to just jump into changing projects at production level.Lindholm
C
0

There are a lot of information around clean architecture/code etc.

Uncle Bob introduced Clean Architecture

The main idea is to not be depend on third-party dependencies(UI, frameworks, DB) to test it easy. It can be done using layers and following Dependency Rule(inner layers are independent of outer layers)

There are design patterns(MVC, MVP, MVVM, VIPER)[About] which usually solve a UI part

I would recommend to read the series of articles about Clean Architecture by Fernando Cejas:

Ciceronian answered 9/4, 2020 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.