Monday, October 1, 2007

Example of Composite Pattern

Throughout my computer science career (freshman year through senior year) I have dealt with several APIs, but I had not recognized the fact that they are implemented with certain design patterns in mind. A good example of the composite pattern can be found in the swing API. JPanel for example, is a component that can contain within itself other components such as JButton, JLabel, etc... What makes JPanel follow the composite pattern is the fact that it can also contain other JPanels, which makes up a composition of components. The top level component doesn't have to know about what's contained within it's contents. Let's say a top level JPanel contains another JPanel which contains several other JComponents, the top level JPanel calls the paint() function, which calls paint() on its composition.

No comments: