Comments (7)
Creating additional static methods for creating Observables could be a good idea. It's definitely better than making the whole class a Singleton.
from reactivenetwork.
Hi,
First of all, thank you for asking the question.
I think, in this case, we should avoid Singleton. It's not only my opinion. Here's the quote, which explains this well:
What is so bad about Singleton?
Paraphrased from Brian Button:
- They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a code smell.
- They violate the single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
- They inherently cause code to be tightly coupled. This makes faking them out under test rather difficult in many cases.
- They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other.
Source: http://stackoverflow.com/a/138012/1150795
I wouldn't avoid Singletons at all. There may be cases when they're useful, but I think, we shouldn't use them too much and try to avoid this pattern when possible.
In the case of an Android application, you have only one instance of Application
class, which can be Singleton, so you can create a new object of ReactiveNetwork or anything else there to be sure that there's only one instance of this class. It may be useful when you are using instance this class in a different Activities and you don't want to create too many objects. It's better idea and more flexible solution than forcing library class to be a Singleton. If we have too many Singletons, the application will consume a lot of memory and we want to avoid that. Especially when an app is running on a mobile device, which has limited resources and battery life. Moreover, we are reducing app testability.
from reactivenetwork.
I get your points. But for me, I prefer to use static factory method like create()
or newInstance()
according Effective Java - Item 1: Consider static factory methods instead of constructors
. Just my two cents.
from reactivenetwork.
Yeah. In some cases, it may be good option, but I'd rather leave classical constructor without any singleton here. If you want or need to have a static factory method, you can optionally create additional wrapper class (facade) and create a static factory method for this library there.
from reactivenetwork.
Why not make the ReactiveNetwork methods static like the normal rxjava.Observable classes?
from reactivenetwork.
Shall I make an issue to do this? I think this should be a high priority since it would add some nice syntax parity to the rxjava project. That would also imply that you should add the @Deprecated
tag to the public ReactiveNetwork constructors to tell users to use the static methods to instantiate an ReactiveNetwork object
from reactivenetwork.
Static methods are added in the similar manner like in RxJava library. They'll be available in the next release.
from reactivenetwork.
Related Issues (20)
- "NetworkCallback was already unregistered" if re-using Observable HOT 4
- Migrate CI from Travis to GitHub Actions
- connection speed HOT 5
- fix error prone config for the RxJava2.x branch after bumping its version HOT 1
- Release 4.0.0-rx3 HOT 11
- [Question] What is a reason to use http instead of https protocol in your library internal requests? HOT 1
- How know which connection type is better ? HOT 1
- NetworkInfo.State Deprecated HOT 1
- Rx Java 3 support
- BroadCast Receiver stopped working HOT 5
- Google pixel wifi change event is received in 30 sec delay HOT 5
- java.lang.IllegalArgumentException: Too many NetworkRequests filed in Android Nougat(7.1.1). HOT 7
- MarshmallowNetworkObservingStrategy.java line 80 HOT 4
- isConnectedToInternet always false
- Internet connectivity issue: Could not establish connection with WalledGardenStrategy HOT 1
- ReactiveNetwork(RN) usage of IPv6 and DNS results prioritization HOT 1
- Will there be a version for Kotlin Flow ? HOT 2
- Fix Robolectric tests HOT 1
- How to check Internet Connection in Background continuously even if App is Running or Not/Closed.. HOT 1
- Th
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactivenetwork.