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



John B. Matthews wrote:
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>

Exactly. Proviso: every constant must provide a concrete implementation.

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 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.

As people are showing, there are idioms besides polymorphism that can work.

Or ones that refactor the polymorphism into interesting places.

You guys have given me a riddle: what are the relative strengths and weaknesses of these idioms?

--
Lew
Why is a raven like a writing desk?
.



Relevant Pages

  • Why cant switch be used for objects
    ... The Jit could certainly optimize this case because the addresses of static ... The thing is that is often used my own enum classes because I usually want ... public Color[] GetGreenLikeColors.. ... I see no technical reason why switch shouldn' be allowed for objects. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reflection and variable selection? Late binding?
    ... If your variables are all integers (or any of the supported base Types for Enums) then use an Enum as in my second example. ... If your variables are other Types such as strings or complex Types then you should use the switch statement. ... when each case is doing something very similar: going from string "x" to ...
    (microsoft.public.dotnet.framework)
  • Re: Long switch statements (and Swing application structural design)
    ... case Country: ... case Currency: ... I can't help feeling that I ought to be able to find a way to eliminate the switch statement. ... I don't know off-hand whether you can make an `enum' abstract, ...
    (comp.lang.java.programmer)
  • Re: Using enums to avoid using switch/if
    ... My point was that in the given example, replacing if ladders with switch would buy little in terms of bloat, and that taking advantage of enums is more likely to. ... Looking at run-time, switch statements typically use much faster low-level instructions than a series of if statements, with less impact on branch prediction. ... Each enum has an execute method that returns an enum for the "next" state. ...
    (comp.lang.java.programmer)
  • Re: Magic Numbers dangerous?
    ... The crucial problem with type-safe enum kludges is getting a decently ... The Java enum implementors went to considerable ... shenanigans to implement the switch with a plain old jump table using ... // use an ordinary int switch, using pre-mapped ordinals to sort the ...
    (comp.lang.java.programmer)