lundi 8 mai 2017

Best Practice: Conditional data loading from cache

We have a class Cache that contains some blogposts ArrayList<Post> posts ...

Let's say we have 2 cases.

  1. We want to load all posts
  2. We want to load posts for specific label/tag

What is better:

1st Approach. Cache class will take care for what you wanna load

// tag == null -> means load all posts
// else loadByTag
ArrayList<Post> posts = Cache.getPosts(tag);

//Cache.java
public static ArrayList<Post> getPosts(var tag){
    return tag == null ? getAll() : getByTag(tag); 
} 
private static ArrayList<Post> getAll() {
    ... 
} 
private static ArrayList<Post> getByTag(var tag){
   ... 
}

2nd Approach. You must define outside what you want to load

var tag = null;
if (tag == null)
   Cache.getAllPosts();
else
   Cache.getPostsByTag(tag);

Aucun commentaire:

Enregistrer un commentaire