5.4. Composite

The problem is that you may have a set of Primitive objects which you also group into Composite objects, and you want to be able to treat the primitive and the composite objects the same. You also want to be able to have composites made of mixtures of composites and primitives, and so on.

The solution is to have a base class called, say Component, with both the Primitive and Composite classes inheriting from it. Composite has a member, typically an array, which holds any number of Component objects.

When you use a Composite pattern the Component class usually has some virtual method doSomething() that you override in the child classes. Typically the primitive children of Component override doSomething() in various particular kinds ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.