× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: RE: switch on String?
  • From: "Fred Kulack" <kulack@xxxxxxxxxx>
  • Date: Wed, 7 Mar 2001 11:00:48 -0600
  • Importance: Normal


On 03/06/2001 at 04:47:49 PM, owner-java400-l@midrange.com wrote:
What really helps me in this case is your explanation followed by a small
snippet of code showing how something is done.   I realize that may not be
so easy with OO, but it helps just the same.  Thanks!
Yeah, I love the code examples too.
Ok here goes.
I didn't compile these and I'm pretty good at typos.

Usually when I find its necessary to do a switch on a string,
that string is associated with some object or its behavior.
I then write the object something like the following

Note the amount of work adding a smell causes.
Most specifically the user of the Smell class (in this
case dispatchSmell() method MUST change.
However, there are no 'extra' object creations.

public class Sniffer {
   protected final static String recognizableSmell[] = {
      "rotten eggs", "skunk", "napalm in the morning" };
   public final static int    SMELL_UNKNOWN = -1
   public final static int    SMELL_MIN     = 0;
   public final static int    SMELL_BADEGG  = 0;
   public final static int    SMELL_SKUNK   = 1;
   public final static int    SMELL_NAPALM  = 2;
   public final static int    SMELL_MAX     = 2;

   public int smellAsInt(String sm) {
      for (int i=0; i<recognizable.length; ++i) {
         if (recognizableSmell[i].equalsIgnoreCase(sm)) {
           return i;
         }
      }
      // A better model is probably the invention of an
      // 'UnknownSmell' exception
      return SMELL_UNKNOWN;
   }
   public String smellAsString(int sm) {
     if (sm < SMELL_UNKNOWN || sm > SMELL_MAX) {
        // A better model is probably the invention of an
        // 'UnknownSmell' exception
        return "unknown";
     }
     return recognizableSmell[sm];
   }

   public void dispatchSmell(String smell) {
      switch (smellAsInt(smell)) {
       case SMELL_BADEGG:
            killTheChicken();
            break;
       case SMELL_SKUNK:
            buyTomatoJuice();
            break;
       case SMELL_NAPALM:
            putOnHelmet();
            break;
       default : {
         // Do whatever
       }
      }
   }
}


The suggestion about subclassing was a good one.

The only problem is that sometimes you can take
the OO model a bit too far. Only with experience
can you judge (i.e. how likely are problems,
how likely will it need to be extended, can object
creation cause critical performance problems in this
area of code?) whether you should do traditional
design for this particular thing or an OO design.

Note the amount of work adding a smell causes.
Most specifically the user of the Smell class (in this
case dispatchSmell() method MUST NOT change, only
the addition
However, there ARE always 'extra' object creations.

public abstract class Smell {
   protected final static String recognizableSmell[] = {
      "rotten eggs", "skunk", "napalm in the morning" };
   protected final static int    SMELL_UNKNOWN = -1
   protected final static int    SMELL_MIN     = 0;
   protected final static int    SMELL_BADEGG  = 0;
   protected final static int    SMELL_SKUNK   = 1;
   protected final static int    SMELL_NAPALM  = 2;
   protected final static int    SMELL_MAX     = 2;

   // smellAsInt() and smellAsString() are still implemented
   // but are shown in the previous example.
   // They are PROTECTED in this class though so users outside
   // cannot build dependancies on the SMELL constants.

   /* This is effectively a smell factory */
   public static Smell getSmell(String sm) {
      switch (smellAsInt(smell)) {
       case SMELL_BADEGG:
            return new BadEggSmell();
       case SMELL_SKUNK:
            return new SkunkSmell();
       case SMELL_NAPALM:
            return new NapalmSmell()
      }
      // A better model is probably the invention of an
      // 'UnknownSmell' exception
      return null;
   }

   public abstract void reactToSmell();

   public void dispatchSmell(String smell) {
       Smell        smellObject = Smell.getSmell(smell);
       smellObject.reactToSmell();
   }
}

public class BadEggSmell() extends Smell {
      public void reactToSmell() {
         // Code to kill the Chicken
      }
}
public class SkunkSmell() extends Smell {
      public void reactToSmell() {
         // Code to buy tomato juice
      }
}
public class NapalmSmell() extends Smell {
      public void reactToSmell() {
         // Code to put on a helmet
      }
}



"The stuff we call "software" is not like anything that human society
  is used to thinking about. Software is something like a machine, and
  something like mathematics, and something like language, and
  something like thought, and art, and information...
  but software is not in fact any of those other things."
Bruce Sterling - The Hacker Crackdown

Fred A. Kulack  -  AS/400e  Java and Java DB2 access, Jdbc, JTA, etc...
IBM in Rochester, MN  (Phone: 507.253.5982   T/L 553-5982)
mailto:kulack@us.ibm.com   Personal: mailto:kulack@bresnanlink.net
AOL Instant Messenger: Home:FKulack  Work:FKulackWrk

+---
| This is the JAVA/400 Mailing List!
| To submit a new message, send your mail to JAVA400-L@midrange.com.
| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner: joe@zappie.net
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.