Java program organization: how to get rid of this large-scale case statement?

I'm creating a program that will fill in attributives Now I'm simulating "various words" like this:

public class WordDescriptor {

    public static final String noun = "N";
    public static final String plural = "p";
    public static final String nounPhrase = "h";
    public static final String usuParticipLeverb = "V";
    public static final String transitiveVerb = "t";
    public static final String intransitiveVerb = "i";
    public static final String adjective = "A";
    public static final String adverb = "v";
    public static final String conjunction = "C";
    public static final String preposition = "P";
    public static final String interjection = "!";
    public static final String pronoun = "r";
    public static final String definiteArticle = "D";
    public static final String indefiniteArticle = "I";
    public static final String nominative = "o";
    public static final String defaultTag = "?";

    private String word; // where word is one of the constants defined above.  

    public String getWord(){
        return word;
    }

    public String setWord(){
        return word;
    }

    /** For debugging only.  
     * 
     * @param s
     * @return
     */
    protected static String getKind(String s){
        if(s.equals(noun)){
            return "noun";
        }else if(s.equals(plural)){
            return "plural";
        }else if(s.equals(nounPhrase)){
            return "nounPhrase";
        }else if(s.equals(usuParticipLeverb)){
            return "usuParticipLeverb";
        }else if(s.equals(transitiveVerb)){
            return "transitiveVerb";
        }else if(s.equals(intransitiveVerb)){
            return "intransitiveVerb";
        }else if(s.equals(adjective)){
            return "adjective";
        }else if(s.equals(adverb)){
            return "adverb";
        }else if(s.equals(conjunction)){
            return "conjunction";
        }else if(s.equals(preposition)){
            return "preposition";
        }else if(s.equals(interjection)){
            return "interjection";
        }else if(s.equals(pronoun)){
            return "pronoun";
        }else if(s.equals(definiteArticle)){
            return "definiteArticle";
        }else if(s.equals(indefiniteArticle)){
            return "indefiniteArticle";
        }else if(s.equals(nominative)){
            return "nominative";
        } else if(s.equals(defaultTag)){
            return "defaultTag";
        }else{
            return "other: "+s;
        }
    }

}

This is almost the ugliest code I can imagine I know I can make it better by using case statements, but it's still ugly This is my question:

How can I make it beautiful? I'm thinking about making:

>Class worddescriptor with subclasses:

>Class noun, tape class:

>Singular > plural

>Class verb > class adverb

But I'm not sure if it's a good idea How can I be better?

Editor: if I take the second approach, I'm not even sure what these classes will look like Here is an example:

public abstract class WordDescriptor {

   public String toString();

}

public class Noun extends WordDescriptor {

    public String toString(){
        return "Noun";
    }
}

public class Plural extends Noun{
    public String toString(){
        return "Plural";
    }
}

Solution

You can use enumeration types to perform certain operations

public enum SpeechPart
{
    NOUN ("noun"),PLURAL ("plural"),NOUNPHRASE ("noun phrase"),ADVERB ("adverb"),ADJECTIVE ("adjective"),CONJUNCTION ("conjunction"),VERB ("verb");

    private String english;

    SpeechPart(String inEnglish)
    {
        this.english = inEnglish;
    }

    public String toString() { return english; }
}

You can now assign these to variables

SpeechPart dog = SpeechPart.NOUN;
SpeechPart ran = SpeechPart.VERB;
SpeechPart quickly = SpeechPart.ADVERB;

Then you can see what their speech is about:

System.out.println(dog.toString());
System.out.println(quickly);        // Implicit call to toString()

The solution assumes that each word has only one part of speech To allow modifiers, such as "first person", "third person", "plural", "present", "progress", etc., you can simply list all modifiers - it's a tedious job, but only once Alternatively, you can adjust the decorator pattern, especially for the need of dynamically adding attributes

Another suggestion is the enumeration modifier:

public enum SpeechModifier
{
    SINGULAR,PLURAL,FIRST_PERSON,SECOND_PERSON,THIRD_PERSON,PRESENT,PAST,PERFECT,PROGRESSIVE;
}

Then build a class that combines them:

public class Word
{
    String word;
    SpeechPart part;
    EnumSet<SpeechModifier> modifiers;
}

Now you can simulate the whole word:

Word w1 = new Word();
w1.word = "bouncing";
w1.part = SpeechPart.VERB;
w1.modifiers = EnumSet<SpeechModifier>.of(SpeechModifier.PRESENT,SpeechModifier.PROGRESSIVE);

However, this solution cannot prevent non perceptual combinations, such as first_ PERSON NOUN PAST.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>