Re: Swing is dead! Long live Swing.
On 2/29/12 12:00 AM, Lew wrote:
Wanja Gayk wrote:
markspace wrote:
... For situations where I might be tempted to just use strings,
I try to substitute enums. For example, instead of
bind( someComponent, "event-name" );
I'd use this:
bind( someComponent, Events.NAME );
It provides automatic syntax checking, and is much easier to refactor if
names need to be changed or moved around later.
Any thoughts on this idea?
I think the same way.
I'm even going further and strongly propose preferring Enums to boolean
parameters and this is why:
http://brixomatic.wordpress.com/2010/02/24/boolean-harmful/
+1
This might irritate those who already find Java verbose, since 'String's
are more compact to declare, but type safety and refactorability [sic]
is a payoff in many situations.
I'm even worse, because I pump a "friendly" string representation into
the enum.
<http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#toString()>
??An enum type should override this method when a more
"programmer-friendly" string form exists.??
I'm even worse than that. My designs often include replacing a boolean
with an Enum that has *logic* in it. Flyweight strategy pattern.
public enum FooStrategy {
ONE_WAY {
public void handle(Foo foo) {
foo.doItOneWay();
}
},
ANOTHER {
public void handle(Foo foo) {
foo.doItAnother();
}
}
;
public abstract void handle(Foo foo);
}
This is more verbose but ends up being more flexible and easier to
maintain. Enums can even implement interfaces, so you could add
additional layer of abstraction which allows custom or statefull
implementations alongside the the enum implementations.