8.2. Composite Pattern: Parents and Children

[ fromfile: children.xml id: children ]

Figure 8.2.  Composite Pattern

Composite Pattern

Many Qt classes use the Composite pattern: QObject, QWidget, QTreeWidgetItem, QDomNode, QHelpContentItem, QResource. The Composite pattern can be found in just about any tree-based structure.

Figure 8.3.  QObject: Composite and Component

QObject: Composite and Component

Figure 8.4. Suffolk University Organizational Chart

Suffolk University Organizational Chart

class OrgUnit : public QObject {
  public:
    QString getName();
    double getSalary();
  private:
    QString m_Name;
    double m_Salary;
};
double OrgUnit::getSalary() {
  QList<OrgUnit*> childlst = findChildren<OrgUnit*>();
  double salaryTotal(m_Salary);
  if(!childlst.isEmpty())
    foreach(OrgUnit* ouptr, childlst)
      salaryTotal += ouptr->getSalary();
  return salaryTotal;
}