Re: Long switch statements (and Swing application structural design)



In article <hvOdnd8Th9iVNW3VnZ2dnUVZ_g-dnZ2d@xxxxxxxxxxx>,
Lew <noone@xxxxxxxxxxxxx> wrote:

Eric Sosman wrote:
RedGrittyBrick wrote:

In one application I have a long switch statement that looks like this

// Entity is an enum
private JPanel selectView(Entity entity) {
switch (entity) {
case Country:
return new CountryControl().getJPanel();
case Currency:
return new CurrencyControl().getJPanel();

// and so on, dozens of other similar cases

default:
return null;
}
}

Q1)

I can't help feeling that I ought to be able to find a way to
eliminate the switch statement. Ideally one that doesn't just involve
transferring complexity elsewhere.

Any ideas?

You might consider making selectView() an abstract method
of Entity, subclassing it appropriately for each instance.
I don't know off-hand whether you can make an `enum' abstract,

You cannot.

Correct, but "You can declare [a] method abstract in the enum type and
override it with a concrete method in each constant."

<http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html>

I found the syntax a little hard to read for many/long concrete
methods. Alternatively, the enum's constructor can initialize a
reference to an abstract class with a concrete implementation:

<http://sites.google.com/site/drjohnbmatthews/enumerated-functions>

Instead of abstract functions of doubles, could your enum hold abstract
GUIs of MVCs?

but you can certainly do it with non-`enum' enums.

But you can put behavior in the enum.

entity.getControl().getJPanel();

Any time you find yourself switching or iffing on a type ('instanceof') in
order to determine which of several versions of the same method you need, you
have subverted polymorphism.

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
.



Relevant Pages

  • Re: enums, using methods as initializers
    ... f.extract should NOT have a switch statement, ... Say someone wants to add a custom field extractor, its easy to change these enums into a regular class, and have the extract be a method in an interface. ... and I would agree that polymorphism is much more elegant. ... To me, an enum and a switch work nicely together, especially when the alternative is to write different versions of a big, complicated method in the definition of an enum. ...
    (comp.lang.java.programmer)
  • Re: Newbie: Problems Picking up integer values (dereferencing problem?) Really Trying To ReadTheGosh
    ... rather than just omitting it. ... the colon after the label must be followed by a statement ... the default label is at the very end of the switch statement. ... the enum, or a default. ...
    (comp.lang.c)
  • Re: Binding a switch to an Enum at compile time?
    ... System.Diagnostics.Debug.Fail("unhandled enum value"); ... > Adam Blair wrote: ... >> Is it possible to bind a switch statement to an Enum such that a ... > That compiler option does not exist. ...
    (microsoft.public.dotnet.languages.csharp)
  • Binding a switch to an Enum at compile time?
    ... Is it possible to bind a switch statement to an Enum such that a compile-time ... MyEnum myVar; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: implementing Singleton using enums
    ... Singleton's pattern via the enum... ... Each enum constant identifies a single instance of the enum type, ... a single instance named INSTANCE with a single method. ... home dot woh dot rr dot com slash jbmatthews ...
    (comp.lang.java.programmer)