I don't think a design pattern can be used in this sense. The case scenario is having a base object that has these properties and corresponding getters/setters always defined: (id,name,content). In addition to that there will be objects that have optional properties and corresponding getters/setters (comments, author, deleted). I would like those object to provide the API with the exact properties/methods that I need nothing more and nothing less.
One approach is to have everything in one class which has a bloat of state
class Article {
int id;
int name;
String content;
List<Comment> comments;
Author author;
bool deleted;
//getters and setters omitted
}
The other one is to have multiple classes but that causes bloat of class Names
class Article {
int id;
int name;
String content;
//getters and setters omitted
}
class DeletedArticle : Article {
bool deleted = true;
//getters and setters omitted
}
class ArticleWithAuthor : Article {
Author author;
//getters and setters omitted
}
class ArticleWithComments : Article {
List<Comment> comments;
//getters and setters omitted
}
class DeletedArticleWithAuthor : Article {
bool deleted = true;
Author author;
//getters and setters omitted
}
class DeletedArticleWithComments : Article {
bool deleted = true;
List<Comment> comments;
//getters and setters omitted
}
class DeletedArticleWithAuthorAndComments : Article {
bool deleted = true;
Author author;
List<Comment> comments;
//getters and setters omitted
}
//AND SO ON...
Since all the possible configurations of a Class that always has (id,name,content) and three optional variables is 2^3 I wonder if there is a way to do it with design patterns (hopefully without Reflection). Keep in mind that I know I could use a more relaxed type-wise language or just use JSON/XML but that is not the point :P. Also I am not familiar with partial classes (from C#) if that is relevant at all.
As it was pointed ExpandoObjects could be the way. Could you please provide an example code using my own classes above for ArticleWithComments
and DeletedArticleWithAuthorAndComments
so these are not needed to be defined ?
So for example for ArticleWithComments
I would like to have something like
Article article = new CommentsOnArticle(new Article());
and for DeletedArticleWithAuthorAndComments
I would like to have something like:
Article article = new AuthorOnArticle(new DeletedOnArticle(new CommentsOnArticle(new Article())));
or some other notation like:
Article article = new MergerForArticle();
article.add(CommentForArticle.class);
article.add(DeletedForArticle.class);
article.add(AuthorForArticle.class);
So in other words I want to avoid defining all the possible arrangements of classes and just have a "dynamic class declaration"
Edit: I was thinking of reflection (e.g Java Reflect) too - I don't know if that would be a good practice though..
Edit2: I was also thinking of Anonymous classes and somehow pass the implementations as lambda functions ?? (Java now supports lambda functions) but then again all of the things in the interface will have to be implemented :(
Edit3: It was pointed to use Expando Objects so I changed the question accordingly since there is no design pattern that does this job. Java alterinative could be : https://svn.codehaus.org/groovy/branches/gep-3/src/main/groovy/util/Expando.java