优雅的对象层次

时间:2010-11-23 11:49:45

标签: java inheritance

首先,原谅我的伪代码,我认为在这种情况下它比完整代码更清晰。请假设伪代码中的属性实际上是具有getter&amp ;;的字段。 setter方法,除了ArticleElement,它只需要一个可以通过直接getter方法从对象访问的属性,或者两步getter方法(即getArticleSource().getName())。

假设我有一个模板实体:

ArticleTemplate
    Long id;
    String name;
    String description;
    Integer amount;
    Schedule schedule;

并使用(通过其计划表)在不同日期创建许多潜在的子实体:

Article
    Long id;
    String name;
    String description;
    Integer amount;
    Date date;
    Boolean complete;
    ArticleTemplate template;

某些子实体不是从父级创建的,它们可以是独立的(模板可以为null)。

对于我的UI我想要创建一个排序&合并清单:
a)来自母体实体的潜在儿童实体
b)先前从父实体创建的真实子实体
c)孤儿子实体独立创建

但是,我需要在此列表的元素中添加一些属性,以确定元素之间的差异:

ArticleElement
    // actual value if from Article, null if from potential from ArticleTemplate
    Long id;
    // actual value if from Article or ArticleTemplate
    String name;
    // actual value if from Article or ArticleTemplate
    String description;
    // actual value if from Article or ArticleTemplate
    Integer amount;
    // actual value if from Article, simulated if from potential from ArticleTemplate
    Date date;
    // actual value if from Article, false if from potential from ArticleTemplate
    Boolean complete;
    // actual value (nullable) if from Article, self if from potential from ArticleTemplate
    ArticleTemplate template;
    // false if from Article, true if from potential from ArticleTemplate
    Boolean templateSimulation;
    // once the list is sorted, a running tally of this.amount is to be stored on this object
    Integer runningTally;
    // would be type of interface if Article and ArticleTemplate implement same
    Object source;

显然,我将至少有3个班级,但是有一些不同的接口等方法。

我希望尽可能避免克隆和财产复制,并在任何有益的地方使用继承。

建议赞赏!

1 个答案:

答案 0 :(得分:0)

这是我目前的解决方案,我不确定我喜欢它,但我还没有想出更好的方法:

首先,我将单独留下Article和ArticleTemplate。我可以让它们实现一个描述它们相似之处的界面,但它并没有为这种情况带来太多好处。

创建UI合约

public interface UiElement<T>
{
    T getSource();
    Class<T> getType();
    // redundant - refer to source
    // Long getId();
    String getName();
    String getDescription();
    Integer getAmount();
    Date getDate();
    Boolean getComplete();
    // redundant - not needed anymore
    // ArticleTemplate getTemplate();
    // redundant - replaced by getType()
    // Boolean getTemplateSimulation(); 
    Integer getRunningTally();
}

为文章创建实现 - 将合同调用传递给大多数属性的源对象

public class ArticleUiElement implements UiElement<Article>
{
    private Article source;
    private Integer tally;

    public ArticleUiElement(Article source) {
        this.source = source;
    }

    public Article getSource() {
        return source;
    }

    public Class<Article> getType() {
        return Article.class;
    }

    public String getName() {
        return source.getName();
    }

    public String getDescription() {
        return source.getDescription();
    }

    public Integer getAmount() {
        return source.getAmount();
    }

    public Date getDate() {
        return source.getDate();
    }

    public Boolean getComplete() {
        return source.getComplete();
    }

    public String getRunningTally() {
        return tally;
    }

    public void setRunningTally(String tally) {
        this.tally = tally;
    }
}

为ArticleTemplate创建实现 - 将对大多数属性的签约调用传递给源对象

public class ArticleTemplateUiElement implements UiElement<ArticleTemplate>
{
    private ArticleTemplate source;
    private Integer tally;
    private Date date;

    public ArticleTemplateUiElement(ArticleTemplate source) {
        this.source = source;
    }

    public ArticleTemplate getSource() {
        return source;
    }

    public Class<ArticleTemplate> getType() {
        return ArticleTemplate.class;
    }

    public String getName() {
        return source.getName();
    }

    public String getDescription() {
        return source.getDescription();
    }

    public Integer getAmount() {
        return source.getAmount();
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Boolean getComplete() {
        return false;
    }

    public String getRunningTally() {
        return tally;
    }

    public void setRunningTally(String tally) {
        this.tally = tally;
    }
}

有人可以提供改进,还是一个更好的解决方案?

相关问题