Composite Design Pattern 7/1/2016 CS 631: Composite Pattern 1 Outline • Application – Portfolios of Instruments – Design • Composite Pattern – Design – Analysis – Implementation 7/1/2016 CS 631: Composite Pattern 2 Portfolios • A financial institution holds a portfolio of securities. Each portfolio may contain a set of securities as well as other portfolios. • Different operations may be performed on any portfolio/subportfolio: – Get total balance: • Add principal on all instruments – Get portfolio's market value: • Add present values for all securities • Example: Fannie Mae Portfolio 7/1/2016 CS 631: Composite Pattern 3 Portfolio Hierarchy Example FannieMae Portfolio Assets Fixed Rate Mortgages Mortgage ($100K, 15 year, 6.5%) 7/1/2016 Liabilities Adjustable Rate Mortgages Swap ... ... CS 631: Composite Pattern 4 Basic Design • Create classes for different instruments: – Mortgage – Bond – Swap, etc. • Create a container class: – Portfolio • Problem: – Applications would need to treat Portfolio and Instrument classes differently. • Assuming we only need to know the principal and the value of a portfolio/instrument, they can be treated the same. • The Composite pattern uses recursive composition so that clients do not have to make this distinction. 7/1/2016 CS 631: Composite Pattern 5 Composite Pattern 7/1/2016 CS 631: Composite Pattern 6 Composite Design Pattern: Analysis • Intent – Compose objects into tree structures to represent part-whole hierarchies. • Applicability – To represent part-whole hierarchies of objects. – To enable clients to ignore the difference between compositions of objects and individual objects. • Clients will treat all objects in the composite structure uniformly. 7/1/2016 CS 631: Composite Pattern 7 Participants • Component (Security) – Declares the interface for objects in the composition. – Implements default behavior. – Declares an interface for accessing and managing its child components. • Leaf (Mortgage, Bond, etc.) – Represents leaf objects in the composition. – Defines behavior for primitive objects. • Composite (Portfolio) – Defines behavior for components having children. – Implements child-related operations in the Component interface. 7/1/2016 CS 631: Composite Pattern 8 Implementation: Security Class class Security { public: virtual ~Security() {} virtual double getPrincipal() = 0; virtual double getValue() = 0; virtual void add(Security* sec) {throw "Operation prohibited!";} virtual Security* getChild(int no) { if (no >= this->securities.size()) throw "Child does not exist!"; return this->securities.at(no); } protected: vector<Security*> securities; }; 7/1/2016 CS 631: Composite Pattern 9 Implementation: Mortgage Class // Leaf class class Mortgage : public class Security { public: Mortgage(double bal, double r, double t) : balance(bal), rate(r), term(t) {} virtual double getPrincipal() { return this->balance; } protected: double balance; double rate; double term; }; 7/1/2016 CS 631: Composite Pattern 10 Implementation: Portfolio Class // Composite class class Portfolio : public class Security { public: virtual double getPrincipal() { double sum = 0; for (i=0; i<this->securities->size(); i++) sum += this->securities.at(i)->getPrincipal(); return sum; } virtual void add(Security* sec) { this->securities.push_back(sec); } }; 7/1/2016 CS 631: Composite Pattern 11