i have several enums comply common interface:
interface tablecolumns { string getcolumnname(); int getcolumnindex(); columntype getcolumntype(); boolean iseditable(); int getcolumnwidth(); } a typical implementation is:
enum pointstablecolumns implements trendtablecolumns { points_column("points_column", false, columntype.text, 400, 0); private string columnname; private boolean editable; private columntype columntype; private int columnwidth; private int columnindex; private pointstablecolumns (string columnname, boolean editable, columntype columntype, int columnwidth, int columnindex) { this.columnname = columnname; this.editable = editable; this.columntype = columntype; this.columnwidth = columnwidth; this.columnindex = columnindex; } public boolean iseditable() { return editable; } public int getcolumnindex() { return columnindex; } public string getcolumnname() { return columnname; } public int getcolumnwidth() { return columnwidth; } public columntype getcolumntype() { return columntype; } } i have several implementaions (10+), each having multiple , different values. now, problem see lot of code duplication here, methods in of implementation identical word word. know in java virtually impossible enums can't extend implementation. need here suggestion or different strategy can done in cleaner way. there existing pattern regarding this?
if can live 1 level of indirection, approach present below reduce repeated code minimum.
first, consider following supplier interface, along inner class:
public interface propertiessupplier { properties properties(); public static final class properties { private final int value1; private final string value2; private final double value3; private properties(int value1, string value2, double value3) { this.value1 = value1; this.value2 = value2; this.value3 = value3; } public static properties of(int value1, string value2, double value3) { return new properties(value1, value2, value3); } public int getvalue1() { return this.value1; } public string getvalue2() { return this.value2; } public double getvalue3() { return this.value3; } @override public string tostring() { return "properties [value1=" + this.value1 + ", value2=" + this.value2 + ", value3=" + this.value3 + "]"; } } } nothing magic here. inner class bean private final fields, private constructor initialize them, public getters, factory method , overriden tostring() method. interface defines 1 method returns instance of inner class. note inner class final. idea enforce immutability, properties not allowed change.
then, let's create couple of enums implement interface. let's start myenum1, defines 2 values:
public enum myenum1 implements propertiessupplier { enum_1_const_1(properties.of(1, "hello", 0.123)), enum_1_const_2(properties.of(2, "goodbye", 7.54)); private final properties p; private myenum1(properties p) { this.p = p; } @override public properties properties() { return this.p; } } next comes myenum2, defines 1 value:
public enum myenum2 implements propertiessupplier { enum_2_const_1(properties.of(9, "hey dude", 547.21578)); private final properties p; private myenum2(properties p) { this.p = p; } @override public properties properties() { return this.p; } } as see, both enums implement propertiessupplier interface, must provide implementation properties properties() method. , in order comply this, must encapsulate instance of properties, receive in constructor.
so now, after indirection, code repeated among enums properties field, constructor receives argument , getter method.
this sample showing how enums used:
myenum1 e1 = myenum1.enum_1_const_2; myenum2 e2 = myenum2.enum_2_const_1; system.out.println(e1.name() + " - " + e1.properties()); system.out.println(e2.name() + " - " + e2.properties()); this code produces following output
enum_1_const_2 - properties [value1=2, value2=goodbye, value3=7.54] enum_2_const_1 - properties [value1=9, value2=hey dude, value3=547.21578]
Comments
Post a Comment