Java enum model question

This is a discussion on Java enum model question within the Object forums in Theory and Concepts category; 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 ...

Go Back   Application Development Forum > Theory and Concepts > Object

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 04-20-2008, 11:37 AM
roman
Guest
 
Default Java enum model question

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.

Reply With Quote
  #2  
Old 04-20-2008, 02:56 PM
Daniel Pitts
Guest
 
Default Re: Java enum model question

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/>
Reply With Quote
  #3  
Old 04-20-2008, 03:25 PM
roman
Guest
 
Default Re: Java enum model question

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.


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:40 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.