Behavioral Design Patterns Template Method 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 1 Outline • Behavioral Design Patterns – Overview • Template Method – Application • Forward contracts • Implementation – Design – Policy Implementation 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 2 Behavioral Design Patterns • Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects. • They describe not just patterns of objects or classes but also the patterns of communication between them. • Behavioral patterns characterize complex control flow that is difficult to follow at run-time. • They shift the developer's focus away from the flow of control to concentrate on the way objects are interconnected. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 3 Forward Contracts • A forward contract is an agreement to buy or sell an asset at a certain future time for a certain price. – A spot contract is an agreement to buy an asset today. • One of the parties assumes a long position to buy the asset. • The other party assumes a short position to sell the asset. • Example: forward contracts on foreign exchange. – Exchange rate between the British pound (GBP) and the U.S. dollar. – Useful to hedge foreign currency risk. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 4 Forward Contract: Example Quotes for USD-GBP exchange rate Bid Spot 6-month forward 1-year forward 1.4452 1.4353 1.4262 Offer 1.4456 1.4359 1.4268 The bank is prepared to buy GBP immediately at $1.4452 per GBP, and sell it immediately at $1.4456. The bank is prepared to buy GBP in one year at $1.4262 and sell it at $1.4268. When a U.S. corporation knows it will pay £1 million in six months, it may enter the 6-month forward contract at $1.4359. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 5 Forward contract: Payoff • If the spot rate rises to 1.5000 in six months, the contract will be worth $64,000 (= $1,500,000 $1,435,900) to the corporation. • If it falls to 1.4000 , the contract will have a negative value to the corporation. • The payoff from the long position in the forward contract is (ST – K), where ST is the spot price of the asset at the maturity of the contract, and K is the delivery price. • The payoff from a short position in the forward contract is (K - ST). 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 6 Forward Contract: Evaluation Assume we know the spot price ST of the asset at the time of maturity. Also, let r be the current risk-free interest rate. Then the value of a long forward contract today is: f ( ST K )(1 r ) T The value of a short forward contract today is: f ( K ST )(1 r ) T In other words, the value of a forward contract is: f PT (1 r ) T where PT is a payoff at the time of maturity T. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 7 Forward Contract: Implementation class ForwardContract { public: ForwardContract() {} virtual ~ForwardContract() {} double value() { Market& market = Market::getInstance(); double r = market.getRate(maturity); return getPayoff()* pow(1+r, (-1.0*this->maturity)); } ... 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 8 Forward Contract Class protected: virtual double getPayoff() const = 0; protected: double maturity; double deliveryPrice; double spotPrice; }; 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 9 Forward Contract: Long Position class LongForwardContract : public ForwardContract { protected: virtual double getPayoff() const { return spotPrice-deliveryPrice; } }; 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 10 Forward Contract: Short Position class ShortForwardContract : public ForwardContract { protected: virtual double getPayoff() const { return deliveryPrice-spotPrice; } }; 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 11 Template Method: Design 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 12 Template Method: Analysis • Intent – Define an algorithm in a method, deferring some steps in subclasses. • Applicability – to implement invariant parts of an algorithm; – to refactor and localize the common behavior among subclasses to avoid code duplication. • Participants – AbstractClass (ForwardContract) • defines abstract primitive operations to be implemented by subclasses. • implements an algorithm as a template method. – ConcreteClass (LongForwardContract) • implements the primitive operations. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 13 Policy Implementation template <class POSITION> class ForwardContract { public: ForwardContract() {} double value() { double r = Market::getInstance().getRate(this->maturity); POSITION position(this->deliveryPrice, this->spotPrice); double payoff = position.getPayoff(); return payoff*pow(1+r,(-1.0*this->maturity)); } ... }; 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 14 Position Policies class LongPosition { public: LongPosition(delivery, spot) : deliveryPrice(delivery), spotPrice(spot) {} double getPayoff() { return this->spotPrice – this->deliveryPrice; } protected: double deliveryPrice; double spotPrice; }; 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 15 Client Code ... ForwardContract<LongPosition> lfc; ... lfc.setMaturity(...); lfc.setDeliveryPrice(...); lfc.setSpotPrice(...); cout << lfc.value(); 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 16 Policy Implementation: Analysis • Advantages – Compile-time checking – Flexibility • Position policy can be used with other classes. – Efficiency • Classes are compiled only if needed. • Disadvantages – Potential for code bloating – Some redundancy • The same data are used in the class and policies. 7/1/2016 CS 631: Behavioral Design Patterns: Template Method 17