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-alone
however, I need to add some properties to the elements of this list to determine the differences between the elements:
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
create implementation for Article – pass through contracted calls to the source object for most properties
create implementation for ArticleTemplate – pass through contracted calls to the source object for most properties
can someone offer improvements, or an entirely better solution?