Java: creating multiple instances of an object in a class or restructuring

I'm a java beginner and wondering how best to structure a cooking program. I have a class called Ingredient, this class looks like this:

public class Ingredient {

    private String identifier;
    private double ingredientFactor;
    private String titleInterface;

    public Ingredient(String identifier, double ingredientFactor,String titleInterface) {
        this.identifier = identifier;
        this.ingredientFactor = ingredientFactor;
        this.titleInterface = titleInterface;
    }

      

I want to initialize multiple objects (about 40) with specific values โ€‹โ€‹as instance variables and store them in the map like

Map<String, Ingredient> allIngredients = new HashMap<String, Ingredient>();
allIngredients.put("Almonds (ground)", new Ingredient("Almonds (ground)", 0.7185, "Almonds (ground)");

      

Later, I want to get all these objects as Map / HashMap in another class. I'm not sure how best to proceed, initialize all these objects in the Ingredient class itself, or provide a method that initializes it, or would it be better to create a superclass (AllIngredients or something?) That has a map with ingredients as variables instance?

Happy for any suggestions, thanks in advance :)

+3


source to share


2 answers


Please do not initialize all of these objects in the Ingredient class itself. This would be bad practice for oops.

Just think that your class is a template from which you create copies (objects) with different values โ€‹โ€‹for the attributes. In the real world, if your class is a toy plane model that you would use to create multiple toy planes, but each with a different name and color, consider how such a system would be designed. You will have a model (class). Then a system (another class) to get the required color and name from different colors and names (e.g. in database, files, properties file), etc.

Regarding your situation.

  • Where predefined values โ€‹โ€‹store values โ€‹โ€‹in a text file, properties file, database, constants in a class, etc. depending on the sensitivity of the data.
  • Create class Ingredient with constructors
  • Create a class that will have methods to initialize the Ingredient class using predefined values, update the values โ€‹โ€‹as needed, store the values โ€‹โ€‹in the -database text file, etc. and in your case, return as a card.


Also check the links below

http://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

+1


source


It seems to me what you are looking for static Map

.



public class Ingredient {

    private String identifier;
    private double ingredientFactor;
    private String titleInterface;

    public Ingredient(String identifier, double ingredientFactor, String titleInterface) {
        this.identifier = identifier;
        this.ingredientFactor = ingredientFactor;
        this.titleInterface = titleInterface;
    }

    static Map<String, Ingredient> allIngredients = new HashMap<String, Ingredient>();

    static {
        // Build my main set.
        allIngredients.put("Almonds (ground)", new Ingredient("Almonds (ground)", 0.7185, "Almonds (ground)"));
    }
}

      

0


source







All Articles