简体   繁体   中英

get() methods in Java enum type

I have an enum type (say for arguments sake CarModel ), used throughout an application (numerous classes).

public enum CarModel {
    DIABLO,
    P911,
    DB7;
}

I have various methods that use this CarModel enum type in different ways, and each has a switch statement to set some String variable depending on the enum type, before going on to do other stuff. (eg set the Manufacturer of some model, or set the country of origin etc. These results are static at runtime)

The issue is, if I want to add a new model to the enum, I'd need to go to each method, and extend/modify the switch statement to handle its existence. This could easily lead to human error, and/or code duplication (if various methods use the same switch statements).

Rather than using switch statements all-over, I would like to have static methods, that could be edited in a single location, and would allow for behaviour similar to the following:

String country = CarModel.DIABLO.getCountry() // returns Italy
String manufacturer = CarModel.P911.getManufacturer() // returns  Porsche

Is this possible with an enum, (and is an enum even the 'correct' way to do this?

You can do something like this.

public enum CarModel {
    DIABLO("Lamborghini", "Italy"),
    P911("Porsche", "Germany");

    private String manufacturer;
    private String country;

    private CarModel(String manufacturer, String country) {
        this.manufacturer = manufacturer;
        this.country = country;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getCountry() {
        return country;
    }
}

Yes, absolutely. Enums can have their own methods, and those methods can be value-specific. It looks like this:

enum CarModel {
    P911 {
        public String getManufacturer() { return "Porsche"; }
    },
    DB7 {
        public String getManufacturer() { return "Aston Martin"; }
    },
    ...
    public abstract String getManufacturer();
}

You can add more methods, of course.

If you're going to use enums, I would suggest an abstract method declared in the enum, and then a provided implementation for each enum instance.

That way you don't have switch statements everywhere (from which you can easily omit cases) and you have a more reliable and OO-styled polymorphic approach.

abstract public int getEngineSize();

DIABLO {
   public int getEngineSize() {
      return 6.3; // whatever it really is...
   }
}

See here for more examples/discussions etc.

I would suggest adding this information directly into your enum.

Like this:

public enum CarModel {

    DIABLO("Lambo"),
    P911 ("Porsche");

    private String manufacturer;


    private CarModel(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getManufacturer() {
        return manufacturer;
    }    
}

and in the class you'd only have to use the getManufacturer method

Moreover enums can implement an interface. You can add some get() methods like getMaxSpeed() or getWeight() . Interface can look like

interface car{
public int getMaxSpeed();
public int getWeight();
}

Yes, this is quite easy to do:

public enum CarModel {
    DIABLO("rod"),
    P911("jane"),
    DB7("freddy");

    private String name;
    CarModel(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }
}   

Haha, I recommend you to use "Factory" Design Pattern.

you can make a CarFactory(), to produce new model car.

http://en.wikipedia.org/wiki/Factory_method_pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM