Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I solve this composition design problem?

Status
Not open for further replies.

ldeveloperxl

Programmer
Oct 11, 2007
1
CA
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 have a few questions.

1. When you say draw a containment relationship are you talking about what UML calls a composition relationship, shown by a black diamond?
2. Can a cateogry contain:
a. Multiple categories?
b. Both categories and posts?
3. When you say that a post can contain categories, can it contain any number of categories, each one of which can contain up to one category of its own?
4. Can a post contain categories that themselves contain the post that contains the category? (That would scare me.)
5. You're saying that a comment can contain any number of comments, and be contained by any number of comments as well?

Now, after clarifying these points for both of us, examine this page, which is a UML diagram of the Composite pattern: This may be considered a metadiagram of (i. e. considered to represent one level of abstraction from) your problem domain. It looks like you're doing a lot of this already, but I have the feeling that you have done it intuitively and that the diagram itself will suggest some refinements to your thinking. Then, understand that you may put any sort of constraints that you like on the diagram. <<constraint>> is a well-known and accepted UML extension.

The point of the Composite pattern is that all classes, both composites (containers) and leaves (non-containers) derive from the same abstract class. That allows for composites to be composed of members of the same base class to which they belong, which allows the behaviors and attributes common to both composites and leaves to be generalized in the base class from which they all derive.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top