Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Long switch statements (and Swing application structural design)

Reply
Thread Tools

Long switch statements (and Swing application structural design)

 
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-11-2008

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/testM...ryControl.java
http://redgrittybrick.org/java/testM...ntryModel.java
http://redgrittybrick.org/java/testM...untryView.java
http://redgrittybrick.org/java/testMVC2/Currency.java
http://redgrittybrick.org/java/testM...cyControl.java
http://redgrittybrick.org/java/testM...encyModel.java
http://redgrittybrick.org/java/testM...rencyView.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?



--
RGB
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      10-11-2008
Lew 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.
>
>> 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.
>


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

Patricia
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      10-11-2008
In article <(E-Mail Removed)>,
Lew <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swing is dead! Long live Swing. Knute Johnson Java 32 02-29-2012 05:10 PM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
application structural design mimi ASP .Net 1 07-21-2005 10:13 PM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments