0

I currently have an app where I use singleton pattern extensively. i.e I have something like This. FeedHandler handles stuff relating to feeds like getting feeds(Posts) from a REST service, getting comments associated with the posts e.t.c.

public class FeedHandler{
  public FeedHandler feedHandler=null;
  public FeedHandler get(){
    if(feedHandler==null){
        feedHandler=new FeedHandler();
     }
  return feedHandler;
}

Then I make my network calls here with volley and publish the result with EventBus to subscribers.

 public void  getSingleFeedFromNetwork(final boolean force, final String feedId){
    VolleyHandler.get().makeJsonObjectRequest("feeds/single?id=" + feedId, Request.Method.GET, null, force, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Feed feed=GsonUtils.getGson().fromJson(response.toString(),Feed.class);
            EventHandler.postEventOnMainThread(feed);
            CommentHandler.get().getCommentsForPost(feed.getId(),true);
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            EventHandler.postEventOnMainThread(new Feed());
        }
    });
}

I want to know if their is anything wrong with this pattern or if their is a better pattern I can use because I've heard a lot about singleton patterns not being a good pattern and I don't know how. But it works for me. This way I believe I'm not tying any network calls to the FeedsActivity or FeedsFragment to avoid memory leaks

  • try reading this http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – Pavneet_Singh Dec 06 '16 at 04:54
  • I've voted to close this as an opinion-based question. Most people dislike the singleton pattern (for good reason). – byxor Dec 06 '16 at 04:55
  • 1
    It's an anti-pattern and has certain uses, but you should not abuse it. Why do you *need* a `FeedHandler`? What does it *do*? Related - http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton?rq=1 or maybe http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons?rq=1 – OneCricketeer Dec 06 '16 at 04:55
  • 1
    It is ok as long as you don't keep any reference to the view/context that can lead to memory leak – HendraWD Dec 06 '16 at 04:57
  • @cricket_007 I need it to get Feeds from a Rest Api. I have edited my question to reflect that – McLeroy Ibe Dec 06 '16 at 05:01
  • Personally, I might try replacing Volley for Retrofit + Gson, then RxJava can complement your EventBus usage. I have not seen any recent usage of singletons for Retrofit. I could understand having one for the `Gson` object. – OneCricketeer Dec 06 '16 at 05:02
  • @cricket_007 I used Retrofit + Gson before Volley, I preferred Volley because it gave me more control. Over cache, response e.t.c – McLeroy Ibe Dec 06 '16 at 05:09
  • Okay, that's fine. You should know that the documentation covers how to create a [`GsonReqest`](https://developer.android.com/training/volley/request-custom.html) for Volley – OneCricketeer Dec 06 '16 at 05:12
  • @cricket_007 yeah I do. Thanks – McLeroy Ibe Dec 06 '16 at 05:13
  • private FeedHandler feedHandler=null; object should be private – Surya Prakash Kushawah Dec 06 '16 at 05:44
  • Singleton pattern in android can cause memory leak by passing activityContext to it instead of applicationContext. Other than that it fully depends on your requirement whether it suits singleton function there or not. – Vinodh Dec 06 '16 at 05:49

0 Answers0