© Jennifer M. Kohnke
The COMPOSITE pattern is a very simple pattern that has significant implications. The fundamental structure of the COMPOSITE pattern is shown in Figure 31-1. Here, we see a hierarchy based on shapes. The Shape base class has two derivative shapes: Circle and Square. The third derivative is the composite. CompositeShape keeps a list of many Shape instances. When called on CompositeShape, Draw() delegates that method to all the Shape instances in the list. Figure 31-1. COMPOSITE pattern
Thus, an instance of CompositeShape appears to the system to be a single Shape. It can be passed to any function or object that takes a Shape, and it will behave like a Shape. However, it is really a proxy[1] for a group of Shape instances. Listings 31-1 and 31-2 show one possible implementation of CompositeShape.
Listing 31-1. Shape.cs
Listing 31-2. CompositeShape.cs
|