vendredi 23 septembre 2016

Does this code not follow or violate prototype pattern in any way?

Instead of getting clone object and modifying attributes, I have first modified attributes of the object and then returned its clone. Is there any difference rule-wise and performance-wise? Also, any other suggestions regarding the design would be great. Thanks.

public class Category implements Cloneable {

  private int id;
  private String title;
  private int totalGames;

  // getters and setters
  public Category clone() {
    try {
      return (Category)super.clone();
    } catch(CloneNotSupportedException ex) {
      return null;
    }
  }
}


public class CategoryCache {

  private static Category category = new Category(0, null, 0);

  private CategoryCache() {

  }

  public static Category getCategory(int id, String title, int totalGames) {
    category.setId(id);
    category.setTitle(title);
    category.setTotalGames(totalGames);
    return category;
  }
}


while (<100 times>) {            
  Category category = CategoryCache.getCategory(<var1>, <var2>, <var3>).clone();
  arlCategory.add(category); // add to arraylist
}

Aucun commentaire:

Enregistrer un commentaire