Long switch statements (and Swing application structural design)

Discussion in 'Java' started by RedGrittyBrick, Oct 11, 2008.

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



    --
    RGB
    RedGrittyBrick, Oct 11, 2008
    #1
    1. Advertising

  2. 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
    Patricia Shanahan, Oct 11, 2008
    #2
    1. Advertising

  3. In article <>,
    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.


    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
    John B. Matthews, Oct 11, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    2
    Views:
    1,251
    Jonathan Bromley
    Oct 22, 2003
  2. Francisco

    pure structural design

    Francisco, Dec 12, 2004, in forum: VHDL
    Replies:
    8
    Views:
    667
    Rob Dekker
    Dec 30, 2004
  3. mimi

    application structural design

    mimi, Jul 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    344
  4. Hybr1dz
    Replies:
    0
    Views:
    404
    Hybr1dz
    Feb 27, 2008
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,719
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page