mercredi 10 février 2016

We should use constants as descriptive variable name to initialize the default value of some field?

I'm doing different examples of the book Head First Design Patterns.

I found an implementation that has this structure (extracted from the book):

public abstract class Pizza {
    String name;
    String dough;
    String souce;

    List<String> toppings = new ArrayList<>();

    //another methods ....

    public String getName() {
         return name;
    }
 }

Example of concrete class (extracted from the book):

public class NYStyleCheesePizza extends Pizza {
    public NYStyleCheesePizza() {
        name = "NY Style Sauce and Cheese Pizza";
        dough = "Thin Crust Dough";
        souce = "Marina Souce";
        toppings.add("Grated Regiano Cheese");
    }
    .......
}

Example of concrete class with my modification :

 public class NYStyleCheesePizza extends Pizza {
    private static final String DEFAULT_NAME = "NY Style Sauce and Cheese Pizza";
    private static final String DEFAULT_DOUGH = "Thin Crust Dough";
    private static final String DEFAULT_SOUCE = "Marina Souce";

    public NYStyleCheesePizza() {
        name = DEFAULT_NAME;
        dough = DEFAULT_DOUGH;
        souce = DEFAULT_SOUCE;
        toppings.add("Grated Regiano Cheese");
    } 
 ......
}

In relation with my modification, I have this question :

We should use constants as descriptive variable name to initialize the default value of some field ?

Aucun commentaire:

Enregistrer un commentaire