lundi 5 décembre 2016

Is singleton pattern okay

I currently have an app where I use singleton pattern extensively. i.e I have something like

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

Aucun commentaire:

Enregistrer un commentaire