简体   繁体   English

Java程序组织:如何摆脱这种大规模的案例陈述?

[英]Java Program Organization: How can I get rid of this massive case statement?

I am creating a program that will fill in a given grammar. 我正在创建一个将填写给定语法的程序。 Right now I am modeling the "kinds of 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 pretty much the most ugly code that I can imagine. 这几乎是我能想象到的最丑陋的代码。 I know I can make it a bit better by using a case statement however that is still friggin ugly. 我知道我可以通过使用case语句来使它变得更好,但是仍然很难看。 Here is my question: 这是我的问题:

How can I make it beautiful? 我怎么能让它美丽? I was thinking about making: 我在考虑制作:

  • class WordDescriptor, with subclasses: 类WordDescriptor,带子类:
    • class noun, with subclasses: class noun,带子类:
      • singular 单数
      • plural 复数
    • class verb 类动词
    • class adverb 类副词

But I am not sure if this seems like a great idea either. 但我不确定这是否也是一个好主意。 How can I this out better? 我怎么能更好呢?


Edit: If I did take the second approach I am not even sure what the classes would 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";
    }
}

I would just create a dictionary or map from abbreviation to full description. 我只想创建一个从缩写到完整描述的字典或地图。 Then getKind would look up its input in the mapping and return the result, or other if the input doesn't map to anything. 然后getKind将在映射中查找其输入并返回结果,或者如果输入未映射到任何内容则返回other结果。

You could do something with an enumerated type. 您可以使用枚举类型执行某些操作。

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 a variable. 您现在可以将这些分配给变量。

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

And then you can see what their parts of speech are: 然后你可以看到他们的演讲内容是什么:

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

This solution assumes only a single part of speech per word. 该解决方案假设每个单词只有一个词性。 To allow for modifiers, such as "first person," "third person," "plural," "present," "progressive," etc., you could simply enumerate all of them -- a tedious job but need be done only once. 为了允许修饰语,例如“第一人称”,“第三人称”,“复数”,“现在”,“进步”等,你可以简单地列举所有修饰符 - 这是一项繁琐的工作,但只需要进行一次。 Alternatively, you could adapt the Decorator Pattern , which addresses specifically the need to add attributes to an object dynamically. 或者,您可以调整Decorator模式 ,该模式专门解决了动态向对象添加属性的需要。

Another suggestion is enumerate the modifiers: 另一个建议是枚举修饰符:

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

Then build a class that combines them together: 然后构建一个将它们组合在一起的类:

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

Now you can model a whole word: 现在你可以模拟整个单词:

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

This solution, however, doesn't prevent non-sensical combinations, such as FIRST_PERSON NOUN PAST. 然而,该解决方案不能防止非感性组合,例如FIRST_PERSON NOUN PAST。

您可以使用带有代码的字典(在您的情况下为s )作为键,返回的字符串作为值吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM