As the SRP says, the way to achieve this principle is divide all into smaller. And what you have just made small, make more smaller. As consequence we got more files and my question is about it, how organize all this new classes that are generated to keep our dirs clean, as the code.
For example. I’m developing an Android app on which I have a functionality about read a rss feed. It is invoked from different screens. It’s obvious that it is a responsibility but to achieve that, I have my sub responsibilities.
To read a RSS Channel I have to…:
- Download XML file from URL.
- Parse the XML file downloaded getting the channel details and all feed items.
So, I will have RSSChannelReader.java
whose public method, something like read()
will invoke NetworkRequest.java
and RSSDocumentParser.java
. As SOLID principles suggest interface segregation, I will use the interfaces INetworkRequest.java
IRSSDocumentParser.java
to operate.
Also, my RSSDocumentParser.java
will use ChannelRSS.java
and ChannelRSSFeedItem.java
that works as vo
for general channel description and feed items values.
So, I will have all this new classes: RSSChannelReader.java
, INetworkRequest.java
, IRSSDocumentParser.java
, NetworkRequest.java
, RSSDocumentParser.java
, ChannelRSS.java
, ChannelRSSFeedItem.java
.
My app dir structure is:
- Activities/: Contains the app screens.
- Models/:
- Business/:
- Libs/:
- […]
Then, where do I put all these new files? I supposed to put RSSChannelReader.java
, IRSSDocumentParser.java
,RSSDocumentParser.java
, ChannelRSS.java
, ChannelRSSFeedItem.java
in business/RSSChannelReader
. There is some problem if I mix the interfaces files with their implementations?
What about ChannelRSS.java
, ChannelRSSFeedItem.java
. Must be there on bussiness/RSSChannelReader
or as they act as value object must be on models
. I have to say that I work with them in the windows/screens of my app. Or maybe to create an adapter to transform the result object generated by my RSSChannelReader.java
to my model object?
INetworkRequest.java
and NetworkRequest.java
should be in a different folder from business/RSSChannelReader
.
My real problem, I think, is that some responsibilities, in this case ReadRSSChannel
can produce other that can actuate as responsibilities for other responsibilities, for example my NetworkRequest
. I can use to read rss but also could be useful for download images.
Do I have to create a new folder for each responsibility?
I’m a little confused because my project is increasing and some files aren’t place in the correct place. In some places my package name is too long with sub levels.
1
I am not an android developer but your question is generic enough so I will take a stab at it.
how organize all this new classes that are generated to keep our dirs
clean
In Java world you will be doing this by placing code in separate packages. For example in your case, you can create a package com.yourcompany.rss
and place all files in there.
As long as com.yourcompany.rss
package contents stick to SRP and define interface only for RSS parsing/consumption it is okay to mix interfaces with implementation. However, if in an app you get dozens of interfaces and implementation, you should consider sub-packages.