ldeveloperxl
Programmer
Hi everyone,
I'm developing a blog system as my first big project, and I've encountered the following design issue.
I have classes Category, Post, Page, and Comment, and they all share the same or similar attributes such as id, title, content, level(for limiting level of recursive composition) etc. and behaviors such as addToBin(), getContainedItems() etc. Since they share common data and behavior, I've decided to put them into a hierarchy relationship. Namely, I create an abstract class RecursiveBin as the generalization/base class which contains all of the common data and behavior. Also because they can contain themselves as well as others e.g. a Category can contain another category as well as posts which in term contain their own comments, so it seems logical to use a "recursive composition" design pattern.
However,the problem is that there are containment constraints i.e.
-a Category can only contain other categories and posts, and it can be contained by <=1 category.
-a Page can only contain other pages and posts
-a Post can only contain comments, and it can be contained by >=1 categories.
-a Comment can only contain other comments, and it can be contained by one and only one Page or Post and >=0 Comment.
e.g. if I draw a containment relationship between the Category class and the RecursiveBin class, it would seem to imply that the Category can contain any of the classes derived from RecursiveBin, which is not the case. Moreover, there are also constraints on the number of objects from RecursiveBin it can contain and it can be contained by according to their classes. e.g. a Comment can contain >=0 comments but it might only be contained by one Page/Post and >=0 Comment.
how can I solve this problem?
Thank you
I'm developing a blog system as my first big project, and I've encountered the following design issue.
I have classes Category, Post, Page, and Comment, and they all share the same or similar attributes such as id, title, content, level(for limiting level of recursive composition) etc. and behaviors such as addToBin(), getContainedItems() etc. Since they share common data and behavior, I've decided to put them into a hierarchy relationship. Namely, I create an abstract class RecursiveBin as the generalization/base class which contains all of the common data and behavior. Also because they can contain themselves as well as others e.g. a Category can contain another category as well as posts which in term contain their own comments, so it seems logical to use a "recursive composition" design pattern.
However,the problem is that there are containment constraints i.e.
-a Category can only contain other categories and posts, and it can be contained by <=1 category.
-a Page can only contain other pages and posts
-a Post can only contain comments, and it can be contained by >=1 categories.
-a Comment can only contain other comments, and it can be contained by one and only one Page or Post and >=0 Comment.
e.g. if I draw a containment relationship between the Category class and the RecursiveBin class, it would seem to imply that the Category can contain any of the classes derived from RecursiveBin, which is not the case. Moreover, there are also constraints on the number of objects from RecursiveBin it can contain and it can be contained by according to their classes. e.g. a Comment can contain >=0 comments but it might only be contained by one Page/Post and >=0 Comment.
how can I solve this problem?
Thank you