Title: Creational Design Patterns
1Creational Design Patterns
2Outline
- Creational Design Patterns
- Definition
- Application
- Bond
- Zero-coupon bond
- Coupon bearing bond
- Bond pricing
- Instrument valuation framework
- Factory Method
- Design
- Analysis
- Implementation
3Creational 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.
4Zero-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 (10.06)5 133.82 in five years. - If in two years the market interest rate is 11,
the bond's price is 133.82 / (10.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.
5Coupon 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
- 6 in six months
- 6 in one year
- 6 in one year and a half
- 106 in two years.
6Bond 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
7Valuation 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.
8Valuation Framework Design
9Factory 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.
10Factory 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.
11Valuation Framework Implementation
struct Payment double term double
amount class Instrument public
Instrument() virtual Instrument()
virtual vectorltPaymentgt getPayments()
return this-gtpayments protected
vectorltPaymentgt payments
12Implementation Bond
class Bond public Instrument public
Bond(double principal, double maturity,
double coupon, double period)
this-gtmaturity maturity ... ... fill
out vectorltPaymentgt ... protected
double principal double maturity double
coupon double period
13Implementation Valuation
class Valuation public Valuation()
virtual Valuation() virtual double
price() // Call factory method
this-gtinstrument createInstrument() ...
for each payment in this-gtinstrument-gtgetPaymen
ts() ... discount according to market rate
... protected // Factory method
virtual Instrument createInstrument() 0
Instrument instrument
14Implementation BondValuation
class BondValuation public class
Valuation public BondValuation(double
principal, double maturity, double coupon,
double period) this-gtprincipal
principal ... protected //
Factory method virtual Instrument
createInstrument() return new
Bond(this-principal, this-gtmaturity,
this-gtcoupon, this-gtperiod)
15Implementation 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 ltlt "Total price of
portfolio " ltlt price ltlt endl