Extendable factory

M

Mark

I wish to develop an extendable object factory for a library I am
writing. The library is to provide a simplified interface to a
messaging application. Most of the work is done within the library
but the user has to override a class and provide the implementation to
a single virtual function (to read data).

It's easy to provide factory methods for the built in classes but I am
struggling to think how to provide a factory method for classes that
are added later.

I know I could allow them to create the objects directly but I would
rather avoid this because it would expose some unneccessary details of
the implementation of the library itself.
 
L

Luca Risolia

I wish to develop an extendable object factory for a library I am
writing. The library is to provide a simplified interface to a
messaging application. Most of the work is done within the library
but the user has to override a class and provide the implementation to
a single virtual function (to read data).

It's easy to provide factory methods for the built in classes but I am
struggling to think how to provide a factory method for classes that
are added later.

I know I could allow them to create the objects directly but I would
rather avoid this because it would expose some unneccessary details of
the implementation of the library itself.

There exist various creational patterns other than the Factory Method:
Abstract Factory, Builder, Prototype just to name a few. Look at "Design
Patterns" by GoF for more informations.
 
G

Guest

I wish to develop an extendable object factory for a library I am
writing. The library is to provide a simplified interface to a
messaging application. Most of the work is done within the library
but the user has to override a class and provide the implementation to
a single virtual function (to read data).

It's easy to provide factory methods for the built in classes but I am
struggling to think how to provide a factory method for classes that
are added later.

I know I could allow them to create the objects directly but I would
rather avoid this because it would expose some unneccessary details of
the implementation of the library itself.

have the new classes register themselves with the factory and provide a static method the factory can call to create them. Another way is to have objects clone themselves.
 
M

Mark

There exist various creational patterns other than the Factory Method:
Abstract Factory, Builder, Prototype just to name a few. Look at "Design
Patterns" by GoF for more informations.

I've seen many examples of these. From what I can see I need a
special kind of Factory Method -- where it can create any derived
class. Builders, Abstract Factories, Prototypes are for different
purposes.
 
L

Luca Risolia

I've seen many examples of these. From what I can see I need a
special kind of Factory Method -- where it can create any derived
class. Builders, Abstract Factories, Prototypes are for different
purposes.

One way is to register your factories (pointers to function) in a map
that you give to the user, i.e. :

shapes["circle"] = Circle::create; // registration
shapes["triangle"] = Triangle::create;

Shape* s = shapes["circle"](); // creation

f(s); // use

where f() might be:

void f(Shape* s) { // accept any shape
s->rotate() // rotate() is virtual
}

See Stroustrup "The C++ Programming Language", chapter 25 for more
informations. Look this to have an idea:

http://www.linux-projects.org/listing/cpp_solutions/25.1/main.cpp
 
M

Mark

I've seen many examples of these. From what I can see I need a
special kind of Factory Method -- where it can create any derived
class. Builders, Abstract Factories, Prototypes are for different
purposes.

One way is to register your factories (pointers to function) in a map
that you give to the user, i.e. :

shapes["circle"] = Circle::create; // registration
shapes["triangle"] = Triangle::create;

Shape* s = shapes["circle"](); // creation

f(s); // use

where f() might be:

void f(Shape* s) { // accept any shape
s->rotate() // rotate() is virtual
}

See Stroustrup "The C++ Programming Language", chapter 25 for more
informations. Look this to have an idea:

Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
date anyway now?

Thanks for this. I think I can adapt this approach. Please keep
watching this thread though :)
 
I

Ian Collins

Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
date anyway now?

It'll only be out of date if and when there's a new edition!

Some books never date.
 
M

Mark

I've seen many examples of these. From what I can see I need a
special kind of Factory Method -- where it can create any derived
class. Builders, Abstract Factories, Prototypes are for different
purposes.

One way is to register your factories (pointers to function) in a map
that you give to the user, i.e. :

shapes["circle"] = Circle::create; // registration
shapes["triangle"] = Triangle::create;

Shape* s = shapes["circle"](); // creation

f(s); // use

where f() might be:

void f(Shape* s) { // accept any shape
s->rotate() // rotate() is virtual
}

See Stroustrup "The C++ Programming Language", chapter 25 for more
informations. Look this to have an idea:

Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
date anyway now?

Thanks for this. I think I can adapt this approach. Please keep
watching this thread though :)

I'm still struggling with this. If I create an example can someone
look at it, please?
 
M

Mark

On 29/05/2012 16:32, Mark wrote:
I've seen many examples of these. From what I can see I need a
special kind of Factory Method -- where it can create any derived
class. Builders, Abstract Factories, Prototypes are for different
purposes.

One way is to register your factories (pointers to function) in a map
that you give to the user, i.e. :

shapes["circle"] = Circle::create; // registration
shapes["triangle"] = Triangle::create;

Shape* s = shapes["circle"](); // creation

f(s); // use

where f() might be:

void f(Shape* s) { // accept any shape
s->rotate() // rotate() is virtual
}

See Stroustrup "The C++ Programming Language", chapter 25 for more
informations. Look this to have an idea:

Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
date anyway now?

Thanks for this. I think I can adapt this approach. Please keep
watching this thread though :)

I'm still struggling with this. If I create an example can someone
look at it, please?

I've solved it now so no need to worry. Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top