Creational Design Patterns Factory Method 7/1/2016 CS 631: Creational Design Patterns 1 Outline • Creational Design Patterns – Definition • Application – Bond • Zero-coupon bond • Coupon bearing bond • Bond pricing – Instrument valuation framework • Factory Method – Design – Analysis – Implementation 7/1/2016 CS 631: Creational Design Patterns 2 Creational Patterns: Definition • Creational patterns abstract the instantiation process. – Help make the system independent of how objects are: • created • composed, or • represented. • Become important as systems evolve to depend more on object composition than inheritance. • Two main themes about creational patterns: – Encapsulate knowledge about which concrete classes the system uses. – Hide how instances of these classes are created and put together. 7/1/2016 CS 631: Creational Design Patterns 3 Zero-Coupon Bond • The n-year zero rate is the rate of interest earned on an investment that starts today and lasts for n years. • The n-year zero-coupon bond pays all interest and principal at the end of n years. • Example: – A five-year 6%-coupon bond for $100 principal will pay $100 (1+0.06)5 = $133.82 in five years. – If in two years the market interest rate is 11%, the bond's price is $133.82 / (1+0.11)3 = $97.84 • Has advantage of being free of reinvestment risk. – However, there is no opportunity to enjoy the effect of a rise in market interest rates. 7/1/2016 CS 631: Creational Design Patterns 4 Coupon Bearing Bond • Most bonds provide coupons periodically. – The owner receives coupon payments. – The owner also receives the principal at maturity. • Example: – A two-year Treasury with a principal $100 provides 6% per annum coupons paid semiannually. – The owner receives: • • • • 7/1/2016 $6 in six months $6 in one year $6 in one year and a half $106 in two years. CS 631: Creational Design Patterns 5 Bond Pricing • Assume the Treasury zero-rates are the following: – – – – 0.5 years: 5.5% 1.0 years: 6.0% 1.5 years: 6.5% 2.0 years: 7% • The present value is calculated by properly discounting all payments: 7/1/2016 $6 1.055 0.5 $6 1.065 1.5 1 $6 1.06 $106 1.07 2 CS 631: Creational Design Patterns $109.54 6 Valuation Framework • Design a framework to evaluate a financial instrument that provides payments to the owner. • The main classes: – Payment to encapsulate the amount and time; – Instrument (abstract class) to provide interface to some financial instrument; – Valuation (abstract) to provide interface and implementation to compute various analytics; – Bond a concrete class encapsulating bond features; – BondValuation a concrete class to valuate bonds. 7/1/2016 CS 631: Creational Design Patterns 7 Valuation Framework: Design 7/1/2016 CS 631: Creational Design Patterns 8 Factory Method: Analysis • Intent – Define an interface for creating an object, but let subclasses decide which class to instantiate. • Applicability – a class can't anticipate the class of objects it must create; – a class wants its subclasses to specify the objects it creates. – classes delegate responsibility to one of several helper classes, and you want localize the knowledge of which helper subclass is the delegate. 7/1/2016 CS 631: Creational Design Patterns 9 Factory Method: Analysis (cont.) • Participants – Product (Instrument) • defines the interface of objects the factory method creates – ConcreteProduct (Bond) • implements the Product interface – Creator (Valuation) • declares the factory method (createInstrument) to manufacture a Product. • may call the factory method in its algorithms. – ConcreteCreator (BondValuation) • overrides the factory method to create a concrete product. 7/1/2016 CS 631: Creational Design Patterns 10 Valuation Framework: Implementation struct Payment { double term; double amount; }; class Instrument { public: Instrument(); virtual ~Instrument() {} virtual vector<Payment> getPayments() {return this->payments;} protected: vector<Payment> payments; }; 7/1/2016 CS 631: Creational Design Patterns 11 Implementation: Bond class Bond : public Instrument { public: Bond(double principal, double maturity, double coupon, double period) { this->maturity = maturity; ... ... fill out vector<Payment> ... } protected: double principal; double maturity; double coupon; double period; }; 7/1/2016 CS 631: Creational Design Patterns 12 Implementation: Valuation class Valuation { public: Valuation() {} virtual ~Valuation() {} virtual double price() { // Call factory method this->instrument = createInstrument(); ... for each payment in this->instrument->getPayments() ... discount according to market rate ... } protected: // Factory method virtual Instrument* createInstrument() = 0; Instrument* instrument; }; 7/1/2016 CS 631: Creational Design Patterns 13 Implementation: BondValuation class BondValuation : public class Valuation { public: BondValuation(double principal, double maturity, double coupon, double period) { this->principal = principal; ... } protected: // Factory method virtual Instrument* createInstrument() { return new Bond(this-principal, this->maturity, this->coupon, this->period); } }; 7/1/2016 CS 631: Creational Design Patterns 14 Implementation: Usage int main() { //Bond portfolio valuation price = 0; for each record { BondValuation val(record.principal, record.maturity, record.coupon, record.period); price += val.price(); } cout << "Total price of portfolio: " << price << endl; } 7/1/2016 CS 631: Creational Design Patterns 15