Jonny
Jonny

Reputation: 3955

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. (e.g. 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?

Upvotes: 15

Views: 13719

Answers (7)

Brian Agnew
Brian Agnew

Reputation: 272247

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.

Upvotes: 3

meadlai
meadlai

Reputation: 935

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

Upvotes: 1

Korgen
Korgen

Reputation: 5399

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

Upvotes: 2

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

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.

Upvotes: 7

Jesper
Jesper

Reputation: 206786

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;
    }
}

Upvotes: 28

codebox
codebox

Reputation: 20254

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;
    }
}   

Upvotes: 1

gkuzmin
gkuzmin

Reputation: 2474

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();
}

Upvotes: 1

Related Questions