Elegant object hierarchy

firstly, please forgive my pseudo-code, I think in this case it is more legible than the full code. Assume that the property in the pseudocode is actually a field with the getter and setter method, except ArticleElementwhere it just needs a property accessible from the object either by the direct getter method, or two getter steps (i.e. getArticleSource().getName()).

Say I have a template object:

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

and is used (via its schedule) to create many potential child objects on different dates:

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

some child objects are not created from the parent, they can be autonomous (the template can be null).

. :
a)
) ,
c) -, [/p >

, :

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 , ..

, , , .

!

.

+3
1

, , , :

-, . , , .

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

- ?

0

Source: https://habr.com/ru/post/1776434/


All Articles