Understanding the Composite Pattern
page 2 of 9
by David Simmonds
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35545/ 46

What happens with Composite Pattern?

In the Composite pattern, every participant is referred to as a component. By providing a common interface which components adhere to, we ensure that we can interact with all participants in a uniform way.

The need for uniform interaction is due to the fact that we also have composite-participants and leaf-participants. A leaf is the smallest indivisible component in the pattern. You could think of leaves as being "atomic."

A Composite is simply a component which knows how to aggregate other components by adding and removing other components to and from itself. It knows how to account for them and also knows how to forward messages to each of its components. Any message to a composite will end up with delegation to its aggregated components.

We do not care how each component gets its job done. We simply pass messages to a component at any level of the hierarchy we choose. Most times the message is passed to a component at the top which can be a leaf or composite. If it is a composite, then it automatically forwards the instructions to be carried out to each of its member components. Each component will in turn figure out whether it is a composite or a leaf. If it is a leaf, then it carries out basic operations on itself and returns the result if necessary or simply updates itself. If it is a composite, then it forwards messages to each of its member components in turn. All this transparency is achieved because we treat each component equally. In effect, we are saying to each component, “Take responsibility for yourself. You must figure out whether you are an individual (Leaf) or a group (Composite).” Leaves operate on themselves and represent themselves. Composites pass on the instructions coming from the top to their components (which again might be leaves or composites). In this way, we have a self traversing structure which internally takes responsibility for ensuring that every member is properly instructed or updated.  At the top, we do not care who we are talking to. We know our instructions will be carried out properly regardless.

In the electoral example, each component gets an instruction to return the vote-count for candidates in the constituency. The ballot paper is a leaf and represents the smallest indivisible unit of information in the voting process. In pattern-talk, each vote will get an instruction to return a vote-count. It returns 1 for the person who got that vote and 0 for other candidates. An instruction to the constituency to count itself is forwarded to its aggregated polling stations. The polling station also knows that it is a composite, not a leaf. It therefore forwards vote-count requests to its aggregates, the ballot-papers.

Notice one thing though. In the electoral example, we have a leafy-composite situation. The component (constituency) which aggregates polling stations is itself a leaf. This is necessary since the determination of which party has won and so will provide the cabinet and Prime minister is decided not by a popular vote count, but by a seat count (in the US an Electoral College count). The PM can get 48% of the popular vote-count, but because her party wins the most seats (at the constituency level) she wins the elections and her party goes into power.

The example has been simplified somewhat. Ballots are counted in boxes and sometimes they are spoiled and count for nothing. In large constituencies, there are polling stations within polling divisions. In some countries there are more than two parties and coalitions are formed between several parties. Also, on a ballot where you are able to vote for several positions and propositions at the same time, the ballot itself becomes a composite and the vote on each issue becomes the leaf. But let us keep the example simple for now.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 10:49:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search