elegant object hierarchy
firstly, pardon my pseudo-code, i think in this case it is more legible than full code. Please assume that a property in the pseudo-code is in fact a field with a getter & setter method, except for the ArticleElement
where it just needs be a property accessible from the object either by a direct getter method, or a two step getter method (ie getArticleSource().getName()
).
Say i have a template entity:
ArticleTemplate
Long id;
String name;
String description;
Integer amount;
Schedule schedule;
and it is used (via its schedule) to create many potential children entities on different dates:
Article
Long id;
String name;
String description;
Integer amount;
Date date;
Boolean complete;
ArticleTemplate template;
some children entities are not created from a parent, they can be stand-alone (template can be null).
for my UI I want to create a sorted & merged list of :
a) potential children entities from parent entities b) real children entities previously created from parent entities c) orphan children entities created stand-alonehowever, I need to add some properties to the elements of this list to determine the differences between the element开发者_开发百科s:
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;
Clearly I'm going to have at least 3 classes but there's a few different approaches with interfaces etc.
I'd like to avoid cloning and property copying wherever possible, and use inheritence wherever beneficial.
suggestions appreciated!
p.
Here's my current solution, and i'm not sure I like it, but i haven't come up with anything better just yet:
firstly, i'll leave Article and ArticleTemplate alone. I could make them implement an interface describing their similarities but it doesn't add much benefit for this case.
create the UI contract
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();
}
create implementation for Article - pass through contracted calls to the source object for most properties
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;
}
}
create implementation for ArticleTemplate - pass through contracted calls to the source object for most properties
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;
}
}
can someone offer improvements, or an entirely better solution?
精彩评论