| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi guys, I was modeling some kind of States domain, but I came up with two solutions and I want to check with you this approaches. The problem (very simple): Theres a fixed set of States (no more than 7). Every state has a description and some of them could be final. Approach 1: public enum State { STATE1("State1", false), STATE2("State2", false), STATE3("State3", true), STATE4("State4", false), STATE5("State5", true); private String description; private boolean isFinal; private State(String description, boolean isFinal) { this.description = description; this.isFinal = isFinal; } public boolean isFinal() { return isFinal; } } Approach 2: public enum State { STATE1("State1", false), STATE2("State2", false), STATE3("State3") { public boolean isFinal() { return true; } }, STATE4("State4", false), STATE5("State5") { public boolean isFinal() { return true; } }; private String description; private State(String description) { this.description = description; } public boolean isFinal() { return false; } } I want to know your opinions about this two approaches (eg what is the intent of each approach, what is it modeling, is there a third approach that you think is better than the previous two?, etc) Bye. |
|
#2
| |||
| |||
| roman wrote: > Hi guys, > I was modeling some kind of States domain, but I came up with two > solutions and I want to check with you this approaches. > > The problem (very simple): > Theres a fixed set of States (no more than 7). Every state has a > description and some of them could be final. > > Approach 1: > public enum State { > STATE1("State1", false), > STATE2("State2", false), > STATE3("State3", true), > STATE4("State4", false), > STATE5("State5", true); > > private String description; > private boolean isFinal; > > private State(String description, boolean isFinal) { > this.description = description; > this.isFinal = isFinal; > } > > public boolean isFinal() { > return isFinal; > } > } > > Approach 2: > public enum State { > STATE1("State1", false), > STATE2("State2", false), > STATE3("State3") > { public boolean isFinal() { > return true; > } > }, > STATE4("State4", false), > STATE5("State5") > { public boolean isFinal() { > return true; > } > }; > > private String description; > > private State(String description) { > this.description = description; > } > > public boolean isFinal() { > return false; > } > } > > > I want to know your opinions about this two approaches (eg what is the > intent of each approach, what is it modeling, is there a third > approach that you think is better than the previous two?, etc) > > Bye. > For your *exact* example, I would use a variation on the first one: public enum State { STATE1("State1"), STATE2("State2"), STATE3("State3", true), STATE4("State4"), STATE5("State5", true); private final String description; private final boolean isFinal; private State(String description) { this(description, false); } private State(String description, boolean isFinal) { this.description = description; this.isFinal = isFinal; } public boolean isFinal() { return isFinal; } } For more complicated examples, I may choose some combination. For states that have multiple transitions, I might have a method that is overridden by each. Any more complicated, and I'd probably abandon the enum pattern and move on to full classes implementing a State interface and/or extending an AbstractState base class. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> |
|
#3
| |||
| |||
| Fe de errata. In Approach 2: STATEX("StateX", false) would be STATEX("StateX"), Sorry. On Apr 20, 12:37 pm, roman <mario.ro...@gmail.com> wrote: > Hi guys, > I was modeling some kind of States domain, but I came up with two > solutions and I want to check with you this approaches. > > The problem (very simple): > Theres a fixed set of States (no more than 7). Every state has a > description and some of them could be final. > > Approach 1: > public enum State { > STATE1("State1", false), > STATE2("State2", false), > STATE3("State3", true), > STATE4("State4", false), > STATE5("State5", true); > > private String description; > private boolean isFinal; > > private State(String description, boolean isFinal) { > this.description = description; > this.isFinal = isFinal; > } > > public boolean isFinal() { > return isFinal; > } > > } > > Approach 2: > public enum State { > STATE1("State1", false), > STATE2("State2", false), > STATE3("State3") > { public boolean isFinal() { > return true; > } > }, > STATE4("State4", false), > STATE5("State5") > { public boolean isFinal() { > return true; > } > }; > > private String description; > > private State(String description) { > this.description = description; > } > > public boolean isFinal() { > return false; > } > > } > > I want to know your opinions about this two approaches (eg what is the > intent of each approach, what is it modeling, is there a third > approach that you think is better than the previous two?, etc) > > Bye. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.