优雅的对象层次结构

3
首先,请原谅我的伪代码,我认为在这种情况下使用伪代码比完整代码更易读。请假设伪代码中的属性实际上是带有getter和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;

有些子实体并非由父实体创建,它们可以是独立的(模板可以为空)。

对于我的用户界面,我想创建一个排序和合并列表:
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个类,但是有一些不同的方法和接口相关。

我希望尽可能避免克隆和属性复制,并在有益处时使用继承。

欢迎提出建议!

p.


2
你需要澄清一些术语。children-parent关系是什么意思?Container-content或者creator-result呢?还有potential是什么意思? - khachik
“潜在性”需要特别澄清。 - Tom Anderson
抱歉,我所说的“子父关系”是指“创作者结果”的意思,即从模板(父)创建文章(子)。而“潜在”的意思是指尚未创建/持久化但正在通过模板“建议”的文章 - 它有可能成为一篇文章...希望这样更清楚了? - pstanton
1个回答

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;
    }
}

有没有人能提供改进,或者完全更好的解决方案?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接