Object Factory - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Object Factory

Description:

Each instrument will be represented by different format, for example: ... A factory delivers some product in the form of an object. Abstract Product (Instrument) ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 16
Provided by: ValuedGate2244
Learn more at: https://cs.gmu.edu
Category:
Tags: factory | format | object

less

Transcript and Presenter's Notes

Title: Object Factory


1
Object Factory
  • C Implementation

2
Outline
  • Application
  • Market Simulator
  • Instrument Reader
  • The Need for Object Factories
  • Implementation
  • Classes and Objects
  • Basic Implementation
  • Using Pointers to Functions
  • Generalization

3
Market Simulator
  • Market simulator may need to generate multiple
    trades (instruments), evaluate such trades, and
    execute them.
  • Assume instrument definitions are stored in a
    file. Each instrument will be represented by
    different format, for example
  • Forward contract (type, asset, maturity, delivery
    price)
  • 1 GOLD 1 100
  • Bond (type, principal, maturity, coupon, period)
  • 2 100 10 0.6 0.5
  • The main class will need to create different
    instrument classes based on description.

4
Classes and Objects
// Instrument, Bond are classes instrument is an
object // Classes are created by programmers
objects by the program Instrument instrument
new Bond(...) // When classes are objects, they
can be created at runtime. // If C allowed
it Class read(const char line) // this is not
C! class InstrumentReader void load(const
char fileName) for each line in
fileName Class theClass
read(line) Instrument instrument new
theClass ...
5
Basic Implementation
// a unique ID for each instrument type namespace
InstrumentType const int FORWARD 1,
BOND 2 void InstrumentReaderload(stdifst
ream inFile) char s256
while(inFile.getline(s, 256))
stringstream line(s) // read object type
int instrumentType line gtgt
instrumentType
6
Basic Implementation Reading Instrument
Instrument pCurrentInstrument switch
(instrumentType) using namespace
InstrumentType case FORWARD
pCurrentInstrument new ForwardContract
break case BOND pCurrentInstrument
new Bond break default ...
handle error unknown type ... // read
the instrument's contents by invoking a virtual
// function pCurrentInstrument-gtread(line.str())
... add the object to container, etc. ...
7
Basic Implementation Drawbacks
  • It performs switch based on a type tag, which is
    exactly what object-oriented programs try to
    eliminate.
  • One class (single source file) has knowledge
    about all Instrument-derived classes.
  • It must include all headers of all possible
    instruments.
  • It is hard to extend. Creating another type of
    instrument involves
  • add a distinct integral constant to
    InstrumentType
  • output that constant when saving the instrument
  • add a new label to the switch statement

8
InstrumentFactory Class
class InstrumentFactory public // Define a
callback function pointer. // The function will
create a concrete instrument pointer. typedef
Instrument (CreateInstrumentCallback)() private
// Define a map that keeps a unique
instrument type // associated with a callback
function pointer. typedef stdmapltint,
CreateInstrumentCallbackgt CallbackMap public
// It is a Singleton! static
InstrumentFactory getInstance() ... //
Instrument registration true if successful
bool register(int id, CreateInstrumentCallback
fn) // The function that "manufactures" an
instrument Instrument create(int
instrumentId) private CallbackMap
callbacks
9
Instrument Registration
// In Bond.cpp file ... // Registration code in
an anonymous namespace to make the // function
invisible from other modules. namespace //
Trivial callback function Instrument
createBond() return new Bond const int BOND
2 // This will be created as a static
variable const bool registered
InstrumentFactorygetInstance().register(
BOND, createBond) // In ForwardContract.cpp
file, similar registration ...
10
Implementing InstrumentFactory
bool InstrumentFactoryregister(int
instrumentId, CreateInstrumentCallback
createFn) // Simply add to the map. (The
insert method returns a // pair, with the
second element being a success flag.) return
this-gtcallbacks.insert( CallbackMapvalue_typ
e(instrumentId, createFn)).second // Create
an instrument pointer based on id Instrument
InstrumentFactorycreate(int id)
CallbackMapconst_iterator i
this-gtcallbacks.find(id) if (i
this-gtcallbacks.end()) throw
stdruntime_error("Unknown Instrument Id")
// invoke the creation function return
(i-gtsecond)()
11
InstrumentReader Implementation
// Loading different instruments from a file void
InstrumentReaderload(stdifstream inFile)
char s256 while(inFile.getline(s, 256))
stringstream line(s) // read object
type int instrumentType line gtgt
instrumentType Instrument
pCurrentInstrument InstrumentFactoryget
Instance().create(instrumentType) ...
process the instrument ...
12
Generalization Object Factory Elements
  • Concrete Product (Bond, ForwardContract)
  • A factory delivers some product in the form of an
    object.
  • Abstract Product (Instrument)
  • All products inherit from a base class.
  • Product Type Identifier
  • Need to have because of the static C type
    system.
  • Product Creator
  • The function or functor specialized for creating
    exactly one type of object.

13
Generic Object Factory
template lt class AbstractProduct, typename
IdentifierType, typename ProductCreator gt class
Factory private typedef stdmapltIdentifier
Type, ProductCreatorgt AssocMap AssocMap
associations
14
Generic Object Factory Implementation
public bool register(const IdentifierType
id, ProductCreator creator) return
this-gtassociations.insert(
AssocMapvalue_type(id, creator)
AbstractProduct create(const IdentifierType
id) typename AssocMapconst_iterator i
this-gtassociations.find(id) if (i !
this-gtassociations.end()) return
(i-gtsecond)() throw (...)
15
Generic Object Factory Usage
// Loading different instruments from a file void
InstrumentReaderload(stdifstream inFile)
typedef Instrument (CreateInstrumentCallback)()
// Use SingletonHolder to ensure singleton
behavior typedef SingletonHolderltFactorylt
Instrument,int,CreateInstrumentCallbackgt gt
InstrumentFactory char s256
while(inFile.getline(s, 256))
stringstream line(s) int instrumentType
line gtgt instrumentType Instrument
pCurrentInstrument InstrumentFactoryget
Instance().create(instrumentType) ...
process the instrument ...
Write a Comment
User Comments (0)
About PowerShow.com