Hi,
After having a few problems designing a command-based system for my RPG, I decided to do some research into design patterns in general and managed to borrow a copy of Design Patterns - Elements of Reusable Object Oriented Software, in order to strengthen my knowledge in this area.
Instead of just finding the pattern I needed for my command system I've decided go through the whole thing. The book is going into detail about how to use the Composite pattern and it says the following:
Quote:
The subclasses Line, Rectangle, and Text, define primitive graphical objects. These classes implement Draw to draw lines, rectangles, and text, respectively. Since primitive graphics have no child graphics, none of these subclasses implements child-related operations.
It's describing Graphic as an abstract base class, with the following methods:
Draw()
Add(Graphic)
Remove(Graphic)
GetChild(int)
The classes, Line, Rectangle and Text, all derive from Graphic but only implement the Draw method. This is where my question comes in. How do you properly go about hiding specific methods from the child, as it states above?
Here's what I've come up with as a little test, which seems to work, but I was wondering if this is the correct approach:
class Parent
{
public:
virtual void Draw() = 0;
virtual void Add(/* blah */) = 0;
virtual void Remove(/* blah */) = 0;
};
class Child : public Parent
{
private:
void Add(/* blah */) {}
void Remove(/* blah */) {}
public:
void Draw() { cout << "Drawing child." << endl; }
};
Then, using the following:
int main()
{
Child child1;
child1.Draw();
child1.Add();
return 0;
}
The call to Add generates an error stating that the method is private which seems like the right behaviour. Is this the correct way to do this?
Thanks. :)
[Edited by - SeymourClearly on February 7, 2008 8:04:21 PM]