object oriented software design ii - real application...

88
Object Oriented Software Design II Real Application Design Christian Nastasi http://retis.sssup.it/~lipari http://retis.sssup.it/~chris/cpp Scuola Superiore Sant’Anna – Pisa April 12, 2012 C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 1 / 40

Upload: others

Post on 30-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Object Oriented Software Design IIReal Application Design

Christian Nastasihttp://retis.sssup.it/~lipari

http://retis.sssup.it/~chris/cpp

Scuola Superiore Sant’Anna – Pisa

April 12, 2012

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 1 / 40

Page 2: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 2 / 40

Page 3: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 3 / 40

Page 4: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Data AcQuisition System

A basic DAQ system

duck-lab: an open-source DAQ framework

Project (temporary) home page:http://retis.sssup.it/~chris/duck-lab

Mercurial revision control: https://rtn.sssup.it/hg/duck-lab(hg clone https://rtn.sssup.it/hg/duck-lab)

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 4 / 40

Page 5: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

duck-lab architecture

...010010110011001...

I/O Library

... 0xFF 0xAF 0x47 ...

Core Library Storage Library

Streamer

Formatter

Data Vector

Storage Vector

D3 DnD2D1 ...

...S1 S2 S3 Sm

XMLConfiguration

We have defined a hierarchy for Data

We have defined the interaction with the Formatter, implemented by theFormatHandler class

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 5 / 40

Page 6: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

duck-lab architecture

FormatHandler

+data_vector: Data[]

-FormatHandler()

+~FormatHandler()

-format(in rawdata:uint8_t*,in len:unsigned long)

Data

#name: std::string

+get_name(): std::string

+format_data(buff:const unit8_t*,len:unsigned long): unsigned long

DataTemplate

+actual_data: T[]

+DataTemplate()

+~DataTemplate()

T:

DataFloat32DataInt8 DataUInt16

<<bind>>

<uint16_t>

<<bind>>

<float>

<<bind>>

<int8_t>

data_vector

Metadata

#type: std::string

#type_size: unsigned long

#length: unsigned long

+get_type(): std::string

+get_type_size(): unsigned long

+get_len(): unsigned long

+get_tot_size(): unsigned long

+~Metadata()

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 6 / 40

Page 7: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The FormatHandler class

We have done Data creation/destruction

We have done Data formatting

class FormatHandler {public:

FormatHandler();~FormatHandler();void format(const uint8_t* rawdata,

unsigned long len);private:

std::vector<Data*> data_vector;};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 7 / 40

Page 8: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The FormatHandler class

We have done Data creation/destruction

We have done Data formatting

class FormatHandler {public:

FormatHandler();~FormatHandler();void format(const uint8_t* rawdata,

unsigned long len);private:

std::vector<Data*> data_vector;};

Switch-based concrete object allocation(bad)

Polymorphic destruction (good!)

Polymorphic formatting with delegation tothe Data classes (good!)

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 7 / 40

Page 9: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The FormatHandler class

We have done Data creation/destruction

We have done Data formatting

class FormatHandler {public:

FormatHandler();~FormatHandler();void format(const uint8_t* rawdata,

unsigned long len);private:

std::vector<Data*> data_vector;};

Switch-based concrete object allocation(bad)

Polymorphic destruction (good!)

Polymorphic formatting with delegation tothe Data classes (good!)

So, given the following configuration (XML)

<message_description><data data_type="uint16" data_name="data_name_1" data_len="2"/><data data_type="float32" data_name="data_name_2" data_len="1"/><data data_type="int8" data_name="data_name_3" data_len="4"/>

</message_description>

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 7 / 40

Page 10: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

FormatHandler::FormatHandler()

DataExample3.cc

FormatHandler::FormatHandler(){

// We create the data vector according to XML configvector<XmlDataElement> config = parse_xml_data();

vector<XmlDataElement>::const_iterator it;for (it = config.begin(); it != config.end(); ++it) {

Data *d;if (it->type == "int8")

d = new DataInt8(it->name, it->len);else if (it->type == "uint16")

d = new DataUInt16(it->name, it->len);else if (it->type == "float32")

d = new DataFloat32(it->name, it->len);else {

cout << "Unknown data type = ’" << it->type <<"’ ! Skipping" << endl;

continue;}data_vector.push_back(d);

}}

Unmanageable: adding new new data requires adding else if.

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 8 / 40

Page 11: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

FormatHandler::∼FormatHandler()

DataExample3.cc

FormatHandler::~FormatHandler(){

// We have to destroy the data vector properlyvector<Data*>::iterator it;for (it = data_vector.begin(); it != data_vector.end(); ++it)

delete *it;}

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 9 / 40

Page 12: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

FormatHandler::format()DataExample3.cc

void FormatHandler::format(uint8_t* rawdata, unsigned long len){

vector<Data*>::iterator it;for (it = data_vector.begin(); it != data_vector.end(); ++it) {

unsigned long required_len = (*it)->get_tot_size();if (required_len > len) {

cout << "Not enough bytes to read Data. Returning" <<endl;

return;}if ((*it)->get_type() == "int8") {

DataInt8 *d = static_cast<DataInt8*>(*it);// memcpy if fine with int8memcpy(d->actual_data, rawdata, required_len);

} else if ((*it)->get_type() == "uint16") {DataUInt16 *d = static_cast<DataUInt16*>(*it);// Fix endianess before memcpy!memcpy(d->actual_data, rawdata, required_len);

} else if ((*it)->get_type() == "float32") {DataFloat32 *d = static_cast<DataFloat32*>(*it);// Fix endianess before memcpy!memcpy(d->actual_data, rawdata, required_len);

} else {cout << "Unknown data type = ’" << (*it)->get_type() <<

"’ ! Skipping" << endl;continue;

}len -= required_len;rawdata += required_len;

}}

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 10 / 40

Page 13: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

FormatHandler::format()

class Data : public Metadata {//...public:

virtual unsigned long format_data(const uint8_t* buff, unsigned long len) = 0;//...};

DataExample4.cc

void FormatHandler::format(uint8_t* rawdata, unsigned long len){

vector<Data*>::iterator it;for (it = data_vector.begin(); it != data_vector.end(); ++it) {

unsigned long used_bytes = (*it)->format_data(rawdata, len);len -= used_bytes;rawdata += used_bytes;

}}

template<class T>class DataTemplate : public Data {//...public:

unsigned long format_data(const uint8_t* buff, unsigned long len){

unsigned long required_len = get_tot_size();if (required_len > len) {

std::cout << "Not enough bytes to read Data." << std::endl;return 0;

}memcpy(actual_data, buff, required_len);return required_len;

};//...};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 11 / 40

Page 14: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

What’s next

We’ll see how to solve the Data Vector creation problem

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 12 / 40

Page 15: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

What’s next

We’ll see how to solve the Data Vector creation problem

Using delegation in FormatHandler::FormatHandler()

Using a design pattern: Factory Method

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 12 / 40

Page 16: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 13 / 40

Page 17: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 14 / 40

Page 18: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Class Factory

Creational design pattern known as Factory Method Pattern

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 15 / 40

Page 19: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Class Factory

Creational design pattern known as Factory Method Pattern

A factory is the location or a concrete class in the code at which objects are constructed.

The intent in employing the pattern is to insulate the creation of objects from their usage.

New derived types can be introduced with no change to the code that uses the base class.[wikipedia]

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 15 / 40

Page 20: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

UML class diagram

The Factory Method in UML

Product

ConcreteProduct

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 16 / 40

Page 21: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

UML class diagram

The Factory Method in UML

Product

ConcreteProduct

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

Product

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 16 / 40

Page 22: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

UML class diagram

The Factory Method in UML

Product

ConcreteProduct

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

Product

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

Product

ConcreteProduct Factory

+factoryMethod(): Product

create

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 16 / 40

Page 23: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

UML class diagram

The Factory Method in UML

Product

ConcreteProduct

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

Product

Factory

+factoryMethod(): Product

ConcreteFactory

+factoryMethod(): Product

create

Product

ConcreteProduct Factory

+factoryMethod(): Product

create

Product Factory

+factoryMethod(): Product

create

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 16 / 40

Page 24: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Factory Method Pattern

“Define an interface for creating an object, but let the classes which implement the interfacedecide which class to instantiate. The Factory method lets a class defer instantiation tosubclasses.”

[Design Patterns: Elements of Reusable Object-Oriented Software]

Main benefit?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 17 / 40

Page 25: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Factory Method Pattern

“Define an interface for creating an object, but let the classes which implement the interfacedecide which class to instantiate. The Factory method lets a class defer instantiation tosubclasses.”

[Design Patterns: Elements of Reusable Object-Oriented Software]

Main benefit?

Delegation: the user does not have access to creation information.

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 17 / 40

Page 26: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Other benefits

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 18 / 40

Page 27: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Other benefits

Named constructor idiom: factory methods rather than constructorsclass Complex {

Complex(double a, double b) { /*...*/ };public:

static Complex* fromCartesian(double r, double i) { return new Complex(r, i); }

static Complex* fromPolar(double m, double a){return new Complex(m * cos(a), m * sin(a)); }

}Complex* c1 = Complex.fromPolar(1, 3.14); // creates z = -1Complex* c2 = Complex.fromCartesian(1, 3.14); // creates z = 1 + j3.14

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 18 / 40

Page 28: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Other benefits

Named constructor idiom: factory methods rather than constructorsclass Complex {

Complex(double a, double b) { /*...*/ };public:

static Complex* fromCartesian(double r, double i) { return new Complex(r, i); }

static Complex* fromPolar(double m, double a){return new Complex(m * cos(a), m * sin(a)); }

}Complex* c1 = Complex.fromPolar(1, 3.14); // creates z = -1Complex* c2 = Complex.fromCartesian(1, 3.14); // creates z = 1 + j3.14

Encapsulating creation: factory hides creation logicclass ClassFactory {public:

static Product* create(const std::string& file_name){

std::string type = get_type_from_file(file_name);if (type == "type_1") {

new ConcreteProduct1(/*might take params*/);} else if (type == "type_2") {

new ConcreteProduct2(/*might take other params*/);} // etc...

}}Product* p = ClassFactory.create("config.xml");

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 18 / 40

Page 29: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 19 / 40

Page 30: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Family of factories

Sometimes factories might belong to the same familyExample: the family of vehicles

CarsMotorcycles...

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 20 / 40

Page 31: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Family of factories

Sometimes factories might belong to the same familyExample: the family of vehicles

CarsMotorcycles...

The abstraction of set of factories belonging to the same family iscalled Abstract Factory.

It’s a creational design pattern

“Provide an interface for creating families of related or dependent objectswithout specifying their concrete classes”

[Design Patterns: Elements of Reusable Object-Oriented Software]

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 20 / 40

Page 32: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Abstract Factory Pattern

Given a set of related factories...

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 21 / 40

Page 33: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Abstract Factory Pattern

Given a set of related factories...

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 21 / 40

Page 34: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Abstract Factory Pattern

...create and abstraction (family) of them.

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 22 / 40

Page 35: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Abstract Factory Pattern

...create and abstraction (family) of them.

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 22 / 40

Page 36: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

UML class diagram

The Abstract Factory in UML

[wikipedia]

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 23 / 40

Page 37: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Outline

1 Previously...

2 FactoriesFactory MethodAbstract FactoryFactory Method in Practice

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 24 / 40

Page 38: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Example

Let’s consider the Disney’s cartoons problem:

The user wants to animate cartoons

The user does not care about their creation process

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 25 / 40

Page 39: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Example

Let’s consider the Disney’s cartoons problem:

The user wants to animate cartoons

The user does not care about their creation process

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 25 / 40

Page 40: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Disney’s cartoons problem

A possible solution (UML class diagram)

DisneyProduct

-name: std::string

#DisneyProduct(name:std::string)

+get_name(): std::string

Goofy

DisneyFactory

+create_goofy(): DisneyProduct

+create_pluto(): DisneyProduct

<<bind>>

"goofy"

Pluto

<<bind>>

"pluto"

<<create>>

<<create>>

DisneyUser

-cartoons: DisneyProduct[]

+animate()

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 26 / 40

Page 41: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

The Disney’s cartoons problem

A possible solution (UML class diagram)

DisneyProduct

-name: std::string

#DisneyProduct(name:std::string)

+get_name(): std::string

Goofy

DisneyFactory

+create_goofy(): DisneyProduct

+create_pluto(): DisneyProduct

<<bind>>

"goofy"

Pluto

<<bind>>

"pluto"

<<create>>

<<create>>

DisneyUser

-cartoons: DisneyProduct[]

+animate()

Let’s try to implement in C++

Some requirements:

The creation of object is exclusively managed by the FactoryDelegating as much as we can (i.e. hiding to the User)

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 26 / 40

Page 42: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: DisneyProduct

FactoryExample1.h

class DisneyProduct {public:

virtual ~DisneyProduct() {};inline std::string get_name(void) const { return name; };

protected:DisneyProduct(const std::string& n) : name(n) {};

private:DisneyProduct(const DisneyProduct&);DisneyProduct &operator=(const DisneyProduct&);std::string name;

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 27 / 40

Page 43: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: DisneyProduct

FactoryExample1.h

class DisneyProduct {public:

virtual ~DisneyProduct() {};inline std::string get_name(void) const { return name; };

protected:DisneyProduct(const std::string& n) : name(n) {};

private:DisneyProduct(const DisneyProduct&);DisneyProduct &operator=(const DisneyProduct&);std::string name;

};

protected constructor: only derived class can call it

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 27 / 40

Page 44: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: DisneyProduct

FactoryExample1.h

class DisneyProduct {public:

virtual ~DisneyProduct() {};inline std::string get_name(void) const { return name; };

protected:DisneyProduct(const std::string& n) : name(n) {};

private:DisneyProduct(const DisneyProduct&);DisneyProduct &operator=(const DisneyProduct&);std::string name;

};

protected constructor: only derived class can call it

why virtual destructor?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 27 / 40

Page 45: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: DisneyProduct

FactoryExample1.h

class DisneyProduct {public:

virtual ~DisneyProduct() {};inline std::string get_name(void) const { return name; };

protected:DisneyProduct(const std::string& n) : name(n) {};

private:DisneyProduct(const DisneyProduct&);DisneyProduct &operator=(const DisneyProduct&);std::string name;

};

protected constructor: only derived class can call it

why virtual destructor?

why other private members?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 27 / 40

Page 46: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: Goofy, Pluto and DisneyFactory

class Goofy : public DisneyProduct {Goofy() : DisneyProduct("goofy") {};

};

class Pluto : public DisneyProduct {Pluto() : DisneyProduct("pluto") {};

};

class DisneyFactory {public:

DisneyProduct* create_goofy(void) { return new Goofy(); }DisneyProduct* create_pluto(void) { return new Pluto(); }

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 28 / 40

Page 47: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: Goofy, Pluto and DisneyFactory

class Goofy : public DisneyProduct {Goofy() : DisneyProduct("goofy") {};

};

class Pluto : public DisneyProduct {Pluto() : DisneyProduct("pluto") {};

};

class DisneyFactory {public:

DisneyProduct* create_goofy(void) { return new Goofy(); }DisneyProduct* create_pluto(void) { return new Pluto(); }

};

Problem: does not compile, why?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 28 / 40

Page 48: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: Goofy, Pluto and DisneyFactory

class Goofy : public DisneyProduct {Goofy() : DisneyProduct("goofy") {};

};

class Pluto : public DisneyProduct {Pluto() : DisneyProduct("pluto") {};

};

class DisneyFactory {public:

DisneyProduct* create_goofy(void) { return new Goofy(); }DisneyProduct* create_pluto(void) { return new Pluto(); }

};

Problem: does not compile, why?

Goofy::Goofy() and Pluto::Pluto() are private, hidden toDisneyFactory.

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 28 / 40

Page 49: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: Goofy, Pluto and DisneyFactory

class Goofy : public DisneyProduct {Goofy() : DisneyProduct("goofy") {};

};

class Pluto : public DisneyProduct {Pluto() : DisneyProduct("pluto") {};

};

class DisneyFactory {public:

DisneyProduct* create_goofy(void) { return new Goofy(); }DisneyProduct* create_pluto(void) { return new Pluto(); }

};

Problem: does not compile, why?

Goofy::Goofy() and Pluto::Pluto() are private, hidden toDisneyFactory.

Solution: using friendship

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 28 / 40

Page 50: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: friendly Goofy and Pluto

After all, Goofy and Pluto are Disney’s friends :)FactoryExample1.h

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};friend class DisneyFactory;

};

This is now valid!

class DisneyFactory {public:

DisneyProduct* create_goofy(void) { return new Goofy(); }DisneyProduct* create_pluto(void) { return new Pluto(); }

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 29 / 40

Page 51: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: the DisneyUser

FactoryExample1.h

enum DisneyCharacterID {GOOFY_ID = 0,PLUTO_ID = 1,

};

FactoryExample1.h

class DisneyUser {public:

DisneyUser();~DisneyUser();void animate(void);

private:DisneyProduct* create_cartoon(int id);

DisneyProduct** toons;size_t len_toons;

};

DisneyUser::DisneyUser(){

const int config[] = {GOOFY_ID, PLUTO_ID,PLUTO_ID, 1234/*invalid*/};

len_toons = 4;toons = new DisneyProduct*[4];for (size_t i = 0; i < 4; i++)

toons[i] = create_cartoon(config[i]);}

DisneyUser::~DisneyUser(){

for (size_t i = 0; i < len_toons; i++)if (toons[i])

delete toons[i];delete[] toons;

}

Equivalent to a configuration file

Allocation of the cartoon container: array ofDisneyProduct*

Delete each cartoon from the container

Delete the container itself

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 30 / 40

Page 52: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

C++: the DisneyUser

FactoryExample1.cc

DisneyProduct* DisneyUser::create_cartoon(int id){

DisneyFactory factory;switch(id) {case GOOFY_ID:

return factory.create_goofy();case PLUTO_ID:

return factory.create_pluto();}return NULL;

}

void DisneyUser::animate(void){

for (size_t i = 0; i < len_toons; i++)if (toons[i])

cout << "Hullo, my name is " <<toons[i]->get_name() << endl;

}

The full example is:./examples/09.real-application_factories-examples/FactoryExample1.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 31 / 40

Page 53: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvement

Of course we don’t like the first implementation...

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 32 / 40

Page 54: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvement

Of course we don’t like the first implementation...

The DisneyProduct* container is so old-fashioned

class DisneyUser {private:

DisneyProduct** toons;size_t len_toons;

//...};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 32 / 40

Page 55: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvement

Of course we don’t like the first implementation...

The DisneyProduct* container is so old-fashioned

class DisneyUser {private:

DisneyProduct** toons;size_t len_toons;

//...};

Part of the creation logic is on the User side

DisneyProduct* DisneyUser::create_cartoon(int id){

DisneyFactory factory;switch(id) {case GOOFY_ID:

return factory.create_goofy();case PLUTO_ID:

return factory.create_pluto();}return NULL;

}<<<<<<< HEAD

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 32 / 40

Page 56: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvementSolution:

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 33 / 40

Page 57: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvementSolution:

Let’s use an STL container...

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 33 / 40

Page 58: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvementSolution:

Let’s use an STL container... std::vector

class DisneyUser {std::vector<DisneyProduct*> toons;

//...};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 33 / 40

Page 59: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

First improvementSolution:

Let’s use an STL container... std::vector

class DisneyUser {std::vector<DisneyProduct*> toons;

//...};

Let’s encapsulate the create_cartoon() in the Factory

FactoryExample2.cc

DisneyProduct* DisneyFactory::create(int id){

switch(id) {case GOOFY_ID:

return new Goofy();case PLUTO_ID:

return new Pluto();}return NULL;

}

FactoryExample2.cc

DisneyUser::DisneyUser(){

const int config[] = {GOOFY_ID, PLUTO_ID,PLUTO_ID, 1234/*invalid*/};

DisneyFactory factory;

for (size_t i = 0; i < 4; i++) {DisneyProduct *p = factory.create(config[i]);if (p)

toons.push_back(p);}

}

The full example is:./examples/09.real-application_factories-examples/FactoryExample2.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 33 / 40

Page 60: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Can we get rid of the switch-based implementation?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 34 / 40

Page 61: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Can we get rid of the switch-based implementation?

Use a look-up table

FactoryID

GOOFY_ID

... ...

PLUTO_ID Pluto Creator

Goofy Creator

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 34 / 40

Page 62: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Can we get rid of the switch-based implementation?

Use a look-up table

FactoryID

GOOFY_ID

... ...

PLUTO_ID Pluto Creator

Goofy Creator

Q: What shall we put on the right side of the table?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 34 / 40

Page 63: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Can we get rid of the switch-based implementation?

Use a look-up table

FactoryID

GOOFY_ID

... ...

PLUTO_ID Pluto Creator

Goofy Creator

Q: What shall we put on the right side of the table?

A: Creator function

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 34 / 40

Page 64: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Can we get rid of the switch-based implementation?

Use a look-up table

FactoryID

GOOFY_ID

... ...

PLUTO_ID Pluto Creator

Goofy Creator

Q: What shall we put on the right side of the table?

A: Creator functionenum DisneyCharacterID {

GOOFY_ID = 0,PLUTO_ID,MAX_CHARACTERS

};//...typedef DisneyProduct* (*creator_type_t)(void);//...creator_type_t lookup_table[MAX_CHARACTERS];

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 34 / 40

Page 65: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 66: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 67: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 68: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

A: Either in the Factory or in theProducts.

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};DisneyProduct* create(void)

{ return new Goofy(); };friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};DisneyProduct* create(void)

{ return new Pluto(); };friend class DisneyFactory;

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 69: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

A: Either in the Factory or in theProducts.

Problem: Does not compile.Why?

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};DisneyProduct* create(void)

{ return new Goofy(); };friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};DisneyProduct* create(void)

{ return new Pluto(); };friend class DisneyFactory;

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 70: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

A: Either in the Factory or in theProducts.

Problem: Does not compile.Why?

Solution: The creator functionmust be static!

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};static DisneyProduct* create(void)

{ return new Goofy(); };friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};static DisneyProduct* create(void)

{ return new Pluto(); };friend class DisneyFactory;

};

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 71: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

A: Either in the Factory or in theProducts.

Problem: Does not compile.Why?

Solution: The creator functionmust be static!

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};static DisneyProduct* create(void)

{ return new Goofy(); };friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};static DisneyProduct* create(void)

{ return new Pluto(); };friend class DisneyFactory;

};

There is also another issue:

how many instances of the Factory can we have?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 72: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvement

Q: Where shall we put thelook-up table?

A: In the Factory, of course.

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);DisneyProduct* create(int id);

private:creator_type_t lookup_table[MAX_CHARACTERS];

};

Q: Where shall we put theconstructor functions?

A: Either in the Factory or in theProducts.

Problem: Does not compile.Why?

Solution: The creator functionmust be static!

class Goofy : public DisneyProduct {private:

Goofy() : DisneyProduct("goofy") {};static DisneyProduct* create(void)

{ return new Goofy(); };friend class DisneyFactory;

};

class Pluto : public DisneyProduct {private:

Pluto() : DisneyProduct("pluto") {};static DisneyProduct* create(void)

{ return new Pluto(); };friend class DisneyFactory;

};

There is also another issue:

how many instances of the Factory can we have?

The Factory is meant to be a singleton!

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 35 / 40

Page 73: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Second improvementHow to do a singleton of the DisneyFactory class?FactoryExample3.h

class DisneyFactory {public:

typedef DisneyProduct* (*creator_type_t)(void);

static DisneyProduct* create(int id);private:

static creator_type_t lookup_table[MAX_CHARACTERS];};

FactoryExample3.cc

DisneyFactory::creator_type_t DisneyFactory::lookup_table[MAX_CHARACTERS] = {Goofy::create, // GOOFY_ID = 0Pluto::create, // PLUTO_ID = 1

};

DisneyProduct* DisneyFactory::create(int id){

if (id >= MAX_CHARACTERS || id < 0)return NULL;

return lookup_table[id]();}

The full example is:./examples/09.real-application_factories-examples/FactoryExample3.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 36 / 40

Page 74: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 75: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 76: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

Example: "goofy", "pluto"

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 77: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

Example: "goofy", "pluto"const int config[] = {GOOFY_ID, PLUTO_ID, PLUTO_ID, 1234/*invalid*/};

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 78: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

Example: "goofy", "pluto"const char* config[] = {"goofy", "pluto", "pluto", "invalid_id"};

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 79: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

Example: "goofy", "pluto"const char* config[] = {"goofy", "pluto", "pluto", "invalid_id"};

Q: How do we change the look-up table?No silly answers: conversion function from string to enumeration.

static creator_type_t lookup_table[MAX_CHARACTERS];

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 80: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

The enumeration-based object identifiers are not exciting...

We would like to have string based IDs

Example: "goofy", "pluto"const char* config[] = {"goofy", "pluto", "pluto", "invalid_id"};

Q: How do we change the look-up table?No silly answers: conversion function from string to enumeration.

static creator_type_t lookup_table[MAX_CHARACTERS];

A: Associative container: std::map<> of course!

static std::map<std::string, creator_type_t> lookup_table;

The full example is:./examples/09.real-application_factories-examples/FactoryExample4.cc

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 37 / 40

Page 81: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Third improvement

FactoryExample4.cc

map<std::string, DisneyFactory::creator_type_t> DisneyFactory::lookup_table;

DisneyProduct* DisneyFactory::create(const string& id){

if (lookup_table.empty()) {lookup_table["goofy"] = Goofy::create;lookup_table["pluto"] = Pluto::create;

}if (lookup_table.find(id) == lookup_table.end())

return NULL;return lookup_table[id]();

}

DisneyUser::DisneyUser(){

const char* config[] = {"goofy", "pluto", "pluto", "invalid_id"};

for (size_t i = 0; i < 4; i++) {DisneyProduct *p = DisneyFactory::create(config[i]);if (p)

toons.push_back(p);}

}

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 38 / 40

Page 82: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Consideration

What about extensibility?

Adding new DisneyProduct requires the following modifications:

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 39 / 40

Page 83: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Consideration

What about extensibility?

Adding new DisneyProduct requires the following modifications:1 definition of the new DisneyProduct (e.g. DonalDuck)

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 39 / 40

Page 84: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Consideration

What about extensibility?

Adding new DisneyProduct requires the following modifications:1 definition of the new DisneyProduct (e.g. DonalDuck)2 adding a new entry in the look-up table...

if (lookup_table.empty()) {lookup_table["goofy"] = Goofy::create;lookup_table["pluto"] = Pluto::create;lookup_table["donal duck"] = DonalDuck::create;

}

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 39 / 40

Page 85: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Consideration

What about extensibility?

Adding new DisneyProduct requires the following modifications:1 definition of the new DisneyProduct (e.g. DonalDuck)2 adding a new entry in the look-up table...

if (lookup_table.empty()) {lookup_table["goofy"] = Goofy::create;lookup_table["pluto"] = Pluto::create;lookup_table["donal duck"] = DonalDuck::create;

}

... in the DisneyFactory class implementation

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 39 / 40

Page 86: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Consideration

What about extensibility?

Adding new DisneyProduct requires the following modifications:1 definition of the new DisneyProduct (e.g. DonalDuck)2 adding a new entry in the look-up table...

if (lookup_table.empty()) {lookup_table["goofy"] = Goofy::create;lookup_table["pluto"] = Pluto::create;lookup_table["donal duck"] = DonalDuck::create;

}

... in the DisneyFactory class implementation

We can avoid step 2!

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 39 / 40

Page 87: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Exercise

Implement the Factory Method in the duck-lab.

Start from code in ./example/DataExample5/

Use the factory pattern for the Data hirarchy

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 40 / 40

Page 88: Object Oriented Software Design II - Real Application Designretis.sssup.it/~lipari/courses/oosd2011-2/09.real... · 2013-10-19 · Outline 1 Previously... 2 Factories Factory Method

Exercise

Implement the Factory Method in the duck-lab.

Start from code in ./example/DataExample5/

Use the factory pattern for the Data hirarchy

Can we generalize the solution using templates?

C. Nastasi (Scuola Superiore Sant’Anna) Factories April 12, 2012 40 / 40