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



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,
but you can certainly do it with non-`enum' enums.

Or you could use a Map<Entity,PanelGetter>, where the
PanelGetter class is subclassed appropriately.

Whether these amount to "transferring complexity elsewhere"
is hard to assess. The decision to use CountryControl in one
place and CurrencyControl in another can't be made to evaporate
altogether, so in that sense complexity is conserved. What it
comes down to, I think, is manageability: Will you find it easier
to deal with `switch' statements, or to manage what might become
a lengthy stretch of initialization code? Chacun à son goût.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • 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)
  • 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: mail merge if statement
    ... Word MVP web site http://word.mvps.org ... If you only have the one country code to consider then ... If you just want to add the switch to some of your numbers based on ... You could even add a country code to your data source and then merge ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Long switch statements (and Swing application structural design)
    ... 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, ... override it with a concrete method in each constant." ... Any time you find yourself switching or iffing on a type in order to determine which of several versions of the same method you need, you have subverted polymorphism. ...
    (comp.lang.java.programmer)