Aug/11

01

Bye bye TypeSafeEnum pattern... Welcome "enum"!

TypeSafeEnum coding pattern was a big time favorite in the past since Java did not have "enum". So people have to invent a way to implement enums. Anyways, Java introduced enum in the recent versions (v1.5+).
So now one can use enum the similar way it was done in TypeSafeEnum patterns.
Few ways to define enums in Java.
Option 1: Simple enum
public enum Action {
update,
write,
read,
delete
};

Option 2: enum with a constructor
public enum EmployeeType {

    FullTime("Regular Full Time employee"),
    PartTime("Contractor or Part time employee");

    private String desc;

    EmployeeType(String desc) {
        this.desc = desc;
    }
}


Option 3: enum with an abstract method

public enum EmployeeType {

    FullTime {
        public String getDesc() {
            return ("Regular Full Time employee");
        }
    },

    PartTime {
        public String getDesc() {
            return ("Contractor or Part Time employee");
        }
    };


    public abstract String getDesc();
}

Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details

Get in touch

you can also contact us at :
(+1) 408 988 2000
InfoObjects Inc.
2041 Mission College Boulevard, #280
Santa Clara, CA 95054
U.S.A.
Live Chat