473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

factory instance question


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?

Jul 23 '05 #1
2 1266
>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?

Jul 23 '05 #2
| 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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob | last post by:
Hello! And first of all thanks to all php gurus who helped me a lot last time on my question! I have another problem now! I have taken a Database Connection Factory class to use in my website....
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
8
by: Craig Buchanan | last post by:
I've seen design patterns for class factories that work well to create (fetch) objects, but I haven't seen anything about how to persist the class' data when it has changed. Is this done thru the...
8
by: Jason MacKenzie | last post by:
Is there a way to prevent classes from being instantiated by methods other than my factory pattern? I have a couple of classes and want to force the factory class to be used as the "entry...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
6
by: GarrettD78 | last post by:
Accidently posted this to the wrong group so I am reposting. This is probably a newbie question but I am a little confused about how to go next with my code. I think I want to use a factory pattern...
6
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.