Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Property Bubbling

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
Is this a pattern / known-technique?

Say a car has an "OK" attribute, indicating it's in an operable state. Each component of the car has an OK attribute, too. A lug-bolt is OK if it's tight. A Wheel is OK if all it's lug-bolts are OK. A Drive-train is OK if all it's wheels are OK, etc, etc, Finally the whole car is OK if all it's components report being ok.

Does this only work if the heirarchy is a tree and not a graph?

Comments?
 
Off the cuff, and with no particular justification at all, I'd say that it would probably work unless you had a circular reference somewhere. A tree would obviously be OK, but with a graph you might need some mechanism to prevent repeated calculation for objects already visited (kind of the opposite of the 'dirty' flag you might use in a data layer to determine if objects need to be persisted because they have changed)
 
Yes, it is a pattern, actually a textbook example of the Composite pattern.

This is a good UML Composite diagram, with the exception that the diamond touching the right side of the Composite class should be black (composition, not aggregation). Notice that the Composite class both is a component (derives therefrom) and is composed of components (either other composites or leaf classes).

A good visual example is a frame. Frames can contain controls (leaf classes) or other frames (composite classes).

Also, notice that the Operation() method defined in the Component base class represents a metaexample of your "bubbling property" Ok attribute. The composite class's Operation method iterates through each constituent leaf and/or composite object, and performs the Operation method of each. Applied to your example, the Ok method of a Composite object iterates through the Ok methods of each constituent Composite and/or Leaf object, and returns true if they all return true.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top