Long switch statements (and Swing application structural design)

R

RedGrittyBrick

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?



This is actually part of a small test application I am playing around
with. My aim is to explore ways to organise the classes of a Swing
application that allows the user to view data from dozens of database
tables.

I'd welcome comments on that too.

It's small but a bit long to post here. 14 classes averaging 45 lines
each (none longer than 82 lines).

http://redgrittybrick.org/java/testMVC2/App.java
http://redgrittybrick.org/java/testMVC2/Controller.java
http://redgrittybrick.org/java/testMVC2/Country.java
http://redgrittybrick.org/java/testMVC2/CountryControl.java
http://redgrittybrick.org/java/testMVC2/CountryModel.java
http://redgrittybrick.org/java/testMVC2/CountryView.java
http://redgrittybrick.org/java/testMVC2/Currency.java
http://redgrittybrick.org/java/testMVC2/CurrencyControl.java
http://redgrittybrick.org/java/testMVC2/CurrencyModel.java
http://redgrittybrick.org/java/testMVC2/CurrencyView.java
http://redgrittybrick.org/java/testMVC2/FormView.java
http://redgrittybrick.org/java/testMVC2/Log.java
http://redgrittybrick.org/java/testMVC2/Model.java
http://redgrittybrick.org/java/testMVC2/View.java

I've abstracted CountryControl and CurrencyControl to the point I don't
need them (App.java half reflects this) but they keep the switch
statement shorter - at the expense of extra classes - I'm torn.

Q2)
Are there major ways I can simplify things or make it more elegant?
 
P

Patricia Shanahan

Lew said:
You cannot.


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.

Also consider java.util.EnumMap<Entity,JPanel>.

Patricia
 
J

John B. Matthews

Lew said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top