java - How to use enum for singleton design pattern? -
i understand following piece of code gives same instance , can not modified , best recommended single/multi-threaded use.
enum colors { red, blue, green; }
but want know how use enum applying singleton pattern custom classes, mean initializations , return instance. citing example of great help.
you can encapsulation , initialization in class.
here's simple example:
public enum color{ instance(10, 30, 50); private int red, green, blue; private color(int red, int green, int blue){ this.red = red; this.green = green; this.blue = blue; } public int getred(){ return red; } public int getgreen(){ return green; } public int getblue(){ return blue; } }
in case have thread safe singleton instance of enum type color.
Comments
Post a Comment