Usage scenarios and examples of enumerations in java

In Java, the enumeration (Enum) type is used to represent a fixed number of constant values. They are typically used to represent a set of related values, such as days of the week, months, colors, etc. Enumeration types provide a type-safe way to limit the values ​​of variables.

Here is an example of a simple enumeration type representing the seven days of the week:

public enum Day {
    
    
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

You can use enums like this:

Day day = Day.MONDAY;

switch (day) {
    
    
    case MONDAY:
        System.out.println("Mondays are hard.");
        break;
    case FRIDAY:
        System.out.println("Fridays are better.");
        break;
    // You can have case statements for the other days as well
    default:
        System.out.println("Midweek days are so-so.");
        break;
}

Enumeration types can also have constructors, methods, and fields. For example, suppose you have an enumeration representing months, each with a specific number of days:

public enum Month {
    
    
    JANUARY(31),
    FEBRUARY(28),
    MARCH(31),
    APRIL(30),
    MAY(31),
    JUNE(30),
    JULY(31),
    AUGUST(31),
    SEPTEMBER(30),
    OCTOBER(31),
    NOVEMBER(30),
    DECEMBER(31);

    private final int days;

    Month(int days) {
    
    
        this.days = days;
    }

    public int getDays() {
    
    
        return days;
    }
}

Then, you can use it like this:

Month month = Month.FEBRUARY;
System.out.println("February has " + month.getDays() + " days.");

This will print "February has 28 days."

In Java, enumeration types can be used in more complex scenarios, such as implementing design patterns (such as singleton pattern, strategy pattern) or as part of a state machine.

Here is an example of the singleton pattern implemented using enumerations:

public enum Singleton {
    
    
    INSTANCE;

    public void execute (String arg) {
    
    
        // perform operation here
    }
}

Then, you can use it like this:

Singleton singleton = Singleton.INSTANCE;
singleton.execute("Do something");

In this example, the enumeration type ensures the global access point of the singleton, and due to the characteristics of the enumeration type, the singleton is thread-safe.

Another complex usage scenario is implementing the Strategy pattern. Suppose you have an application that can be sorted in a number of different ways. You can use enums to implement these different sorting strategies:

public enum SortStrategy {
    
    
    ASCENDING {
    
    
        public void sort(int[] array) {
    
    
            Arrays.sort(array);
        }
    },
    DESCENDING {
    
    
        public void sort(int[] array) {
    
    
            Arrays.sort(array);
            int length = array.length;
            for (int i = 0; i < length / 2; i++) {
    
    
                int temp = array[i];
                array[i] = array[length - 1 - i];
                array[length - 1 - i] = temp;
            }
        }
    };

    public abstract void sort(int[] array);
}

Then, you can use it like this:

int[] array = {
    
    3, 1, 4, 1, 5, 9};
SortStrategy strategy = SortStrategy.DESCENDING;
strategy.sort(array);
System.out.println(Arrays.toString(array));

This will sort the array in descending order and print the result.

Guess you like

Origin blog.csdn.net/orton777/article/details/131328470