|
-
October 1st, 2002, 06:13 AM
#1
OO design question/problem: abstract Child/Parent!
OK - here's the problem/question.
We have two pure abstract interface classes - no implementation (remember that). One is a Child and one is a Parent. Parent objects can have multiple Child objects added to them and Child objects know their Parent object. Parent derives from Child so that it's possible to add a Parent object to a Parent object - but Child objects on the other hand can't have children. OK, let's see some example (prototype) C++ classes:
// X class name prefix means: abstract, not known (regarding implementation) (btw) ...
class XChild
{
public:
// "knows it's parent"
virtual XParent* getParent() = 0
//... ?
//... ?
};
// Parent is a Child too
class XParent : public XChild
{
public:
virtual void addChild(XChild* child_p) = 0;
virtual void removeChild(XChild* child_p) = 0;
//... ?
//... ?
};
If we say "parent.addChild(X)" the child must (assuming here he has no parent yet) update it's "knowledge" about the parent, and the parent should attach the child to it self. OK, the child could have setParent method - but if you would say "child.setParent(X)" than the parent should also remove a reference to the child.
The question/problem is: How would the abstract interface look like if we wanted NOT to break those abstractions at any cost! That means that there should/could be no assumptions regarding implementation the XParent/XChild.
Also, the potential misuse of methods should be minimized - if possible.
OK, please post your opinions/ideas/...?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|