Set contents of set in constructor

Harry :

I have an Animal class...

public abstract class Animal {
    private AnimalType type;
    private String noun;
    private String scientificNoun;
    private short minSizeCm;
    private short maxSizeCm;
    private double minWeightGrams;
    private double maxWeightGrams;
    private Set<AnimalColour> animalColour = new HashSet<>();
    private Set<AnimalLocomotion> locomotion = new HashSet<>();
    private Set<AnimalCountry> country = new HashSet<>();
    private Set<AnimalNaturalHabitat> naturalHabitat = new HashSet<>();

    public Animal(AnimalType type, String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
                  double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
                  Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
                  Set<AnimalNaturalHabitat> naturalHabitat) {
        this.type = type;
        this.noun = noun;
        this.scientificNoun = scientificNoun;
        this.minSizeCm = minSizeCm;
        this.maxSizeCm = maxSizeCm;
        this.minWeightGrams = minWeightGrams;
        this.maxWeightGrams = maxWeightGrams;
        this.animalColour = animalColour;
        this.locomotion = locomotion;
        this.country = country;
        this.naturalHabitat = naturalHabitat;
    }
}

I also have a Bird class...

public abstract class Bird extends Animal {
    private BirdBeakShape beakShape;
    private Set<AnimalColour> featherColour;
    private short minWingspanLengthCm;
    private short maxWingspanLengthCm;

    public Bird(String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
                double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
                Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
                Set<AnimalNaturalHabitat> naturalHabitat, BirdBeakShape beakShape, Set<AnimalColour> featherColour,
                short minWingspanLengthCm, short maxWingspanLengthCm) {
        super(AnimalType.BIRD, noun, scientificNoun, minSizeCm, maxSizeCm, minWeightGrams, maxWeightGrams, animalColour,
                locomotion, country, naturalHabitat);
        this.beakShape = beakShape;
        this.featherColour = featherColour;
        this.minWingspanLengthCm = minWingspanLengthCm;
        this.maxWingspanLengthCm = maxWingspanLengthCm;
    }
}

And a Buzzard class...

import java.util.Set;

public class Buzzard extends Bird {
    public Buzzard(String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
                   double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
                   Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
                   Set<AnimalNaturalHabitat> naturalHabitat, BirdBeakShape beakShape, Set<AnimalColour> featherColour,
                   short minWingspanLengthCm, short maxWingspanLengthCm) {
        super("Buzzard", "Buteo buteo", (short) 51, (short) 57, 550, 1300,
                AnimalColour.BROWN);
    }
}

I am trying to set the different colours of Buzzard in the constructor. Like brown, black, grey. The colour enum is...

public enum AnimalColour {
    BROWN,
    BLACK,
    WHITE,
    GREY,
    GREY_BROWN,
    BLACK_STREAKS,
    GREEN,
    CREAM
}

However, I do not know how to set various colours from the enum as a set in the constructor (or any better way of doing it appreciated). Also, there is so much duplication. Is there any way to reduce that? Because I will eventually also have mammal and reptile class, so even now there's too much code repeated

Sweeper :

Buzzard should not be a class in the first place. It should be an instance of Bird. The field values you are giving it are too specific for it to be a class (unless there are some parts of Buzzard that you did not show here).

You should remove your Buzzard class and create an instance of Bird called buzzard instead:

Bird buzzard = new Bird("Buzzard", 
                        "Buteo buteo", 
                        (short) 51, 
                        (short) 57, 
                        550, 
                        1300,
                        EnumSet.of(AnimalColour.BROWN)
                        ... // pass in other parameters
                        );

You can also consider using the builder pattern.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151400&siteId=1
set
set