Java中的多重继承设计问题 [英] Multiple inheritance design issue in Java

查看:143
本文介绍了Java中的多重继承设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理在java中只有一个继承?这是我的具体问题:

How do you deal with having only single inheritance in java? Here is my specific problem:

我有三个(简化)课程:

I have three (simplified) classes:

public abstract class AbstractWord{
    String kind;    // eg noun, verb, etc

    public String getKind(){ return kind; }

}

public class Word extends AbstractWord{
    public final String word;

    ctor...

    public void setKind(){
        // based on the variable word calculate kind..
    }
}

public class WordDescriptor extends AbstractWord{

    ctor..

    public void setKind(String kind){this.kind = kind;}

}

这是我认为我最基本的实现,但我想做其他实现。

This is what I consider my most basic implementation but I want to make other implementations.

说我想添加一个新的变量说wordLength,但是我想使用继承来添加它。意思我不想修改原来的AbstractWord类。就是这样一些事情:

Lets say that I want to add a new variable say wordLength but I want to add it using inheritance. Meaning I do NOT want modify that original AbstractWord class. Ie something along the lines of this:

public class Length{
    private int length;

    public int getLength(){return length};
}

public class BetterWord extends AbstractWord AND Length{

    public void setLength(){
        // based on the variable word calculate Length..
    }
}

public class BetterWordDescriptor extends AbstractWord AND length{

    public void setLength(int length){this.length = length;}
}

我知道java不让我这样做,但它使我的代码非常丑陋。现在每当我添加一个字段时,我只是将其添加到AbstractWord,但是我需要重命名AbstractWord(和Word和WordDescriptor)。 (我不能仅仅因为向后兼容而将其添加到另外一个,所以它等于这样的方法和东西)。

I know that java does not let me do this but it has made my code very ugly. Right now whenever I add a field I am just adding it to AbstractWord but then I either need to rename that AbstractWord (and Word and WordDescriptor). (I can't just add the field to the other one because of backwards compatibility, it break equals methods and stuff like that).

这似乎是一个很常见的设计问题,但我已经摆脱了我的头,我不能想出任何美丽的解决方案。

This seems like a pretty common design issue but I have racked my head and I cannot come up with any beautiful solutions.

有没有解决这个问题的设计模式?我有一些潜在的解决方案,但我想看看有没有我失踪的东西。

Is there a design pattern that addresses this? I have some potential solutions but I wanted to see if there was something that I was missing.

谢谢,
Jake

thanks, Jake

更新:长度是指单词中的音节数(对于缺乏清晰度感到遗憾)

Update: Length refers to the number of syllables in the word (sorry about the lack of clarity)

推荐答案

喜欢组合超过继承

解决方案考虑到可能会有另一种类型的单词可能需要WordLengthSupport。

Solution takes into consideration that there could be another type of word that may need WordLengthSupport.

同样可以创建和实现其他接口,各种字类型可以混合和匹配那些接口。

Similarly other interfaces could be created and implemented and various word types can have mix and match of those interfaces.

public class WordLength {
    private int length = 0;
    public int getLength(){return length};
    public void setLength(int length){this.length = length};
}

public interface WordLengthSupport {
    public WordLength getWordLength();
}

public class BetterWord extends AbstractWord 
        implements WordLengthSupport {
    WordLength wordLength;
    public WordLength getWordLength() {
        if(wordLength==null) {
            // each time word changes 
            // make sure to set wordLength to null
            calculateWordLength(); 
        }
        return wordLength;
    }
    private void calculateWordLength() {
        // This method should be 
        //    called in constructor 
        //    or each time word changes 
        int length = // based on the variable word calculate Length..
        this.wordLength = new WordLength();
        this.wordLength.setLength(length);
    }
}

public class BetterWordDescriptor extends AbstractWord  
        implements WordLengthSupport {
    WordLength wordLength;
    public WordLength getWordLength(return wordLength);
    public void setWordLength(WordLength wordLength) {
        // Use this to populate WordLength of respective word
        this.wordLength = wordLength;
    }
}

策略模式定义了一系列算法,封装每个算法,并使它们互换。策略允许算法与使用它的客户端不同。

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

此解决方案不使用策略模式,但可以重构相同。

This solution does not use strategy pattern but can be refactored for same.

这篇关于Java中的多重继承设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆