factory instance question

M

ma740988

Consider:

// factory.h
#ifndef TEST_FACTORY_H
#define TEST_FACTORY_H
# include <map>
# include <string>

using namespace std; // remove this ..

struct msg_data_stream {
std::string id;
};

class testFactory;
typedef testFactory* (*CreateInstanceFunc)();

class testFactory {
static map<string, CreateInstanceFunc> factoryMap;
public:
static testFactory* instance ( const msg_data_stream&
data_stream );
static bool register_class( const string& class_name,
CreateInstanceFunc createFunction);
};

#endif

// factory.cpp
# include "factory.h"

map<string, CreateInstanceFunc> testFactory::factoryMap;

testFactory*
testFactory::instance ( const msg_data_stream&
data_stream )
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (data_stream.id)) ==
factoryMap.end())
return 0;
return (it->second)();
}


bool
testFactory::register_class( const string& class_name,
CreateInstanceFunc createFunction)
{
map<string, CreateInstanceFunc>::iterator it;
if (( it = factoryMap.find (class_name)) != factoryMap.end())
return false;
factoryMap[class_name] = createFunction;
return true;
}

// main.cpp
# include <iostream>
# include "factory.h"

struct test_class : public testFactory
{
int idx;
static testFactory* createTestClass() {
return new test_class; }
test_class () : idx(99)
{
// registration probably better off within int main
testFactory::register_class( "test_class",
test_class::createTestCla­ss);
}
};


void computeLikeCrazy()
{
test_class *p_test = (test_class *)testFactory::instance(stream);
if ( p_test )
{
// do something
delete p_test;
}
}

Referencing the function 'computeLikeCrazy'. With each call to the
instance function. A pointer to a new object is returned. The pointer
is then deleted. I'd like to rid the allocation/deletion of the
pointer. How would i achieve this?
 
B

BigBrian

A pointer to a new object is returned. The pointer
is then deleted. I'd like to rid the allocation/deletion of the
pointer. How would i achieve this?

The easiest way is the following

void computeLikeCrazy()
{
/*
test_class *p_test = (test_class *)testFactory::instance(stream);
if ( p_test )
{
// do something
delete p_test;
}
*/
}

Another way is to modify the testFactory::instance() so returns an
object instead of a pointer to an object, but then you can't refer to
the objects polymorphically. But it doesn't look like this is the
intent of this factory, which brings me to the question of why even
have it. Usually factories are used so the calling code doesn't care
about the specific type that's created, and thus you can deal with the
objects polymorphically. Also, it seems weird to me that the
testFactory::instance() returns a pointer to testFactory, and the
objects created thus have to derive from the factory. Usually
factories return a pointer to the object created, which aren't derived
from the factory. However in this factory, the types which are created
are hard coded by calling register_class. Why is this any better than
just calling new on the object?
 
M

ma740988

| The easiest way is the following

I didn't see any source/comment?

| Another way is to modify the testFactory::instance() so returns an
object instead of a pointer to an object, but then you can't refer to
| the objects polymorphically. But it doesn't look like this is the
intent of this factory,

I'll probably need to re-think this (though its working fine :)) but
that's the intent. I'll compare the test code posted here to the real
but I belive they're the same.

| Usually factories return a pointer to the object created, which
aren't derived from the factory.
I'm probably missing something so it's time to pull out Modern C++
design and/or do an on-line search again.

| However in this factory, the types which are created are hard coded
by calling register_class.
Isn't registration one of the underlying tenets of a factory? Either
way there's still the need to register the class correct? Maybe I
misunderstood you.
 

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

Forum statistics

Threads
474,297
Messages
2,571,530
Members
48,251
Latest member
Amelia8778

Latest Threads

Top