473,394 Members | 1,821 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,394 software developers and data experts.

Re : Object Factory Design Pattern by GoF, need help!

<o_******@yahoo.frwrote,
return associations_.insert(AssocMap::value_type(id, creator)).second;
>>

<David Harmonreplied:
As the message says, to help the compiler with template parsing,
>>that must be:

return associations_.insert(
typename AssocMap::value_type(id, creator)
).second;

>>>src\include\objectfactory.h:125: note: say `typename std::map<IdentifierType,AbstractProduct,std::less< IdentifierType>,std::allocator<std::pair<const IdentifierType, AbstractProduct >::value_type' if a type is meant

I add 'typename' as you said, it now understands the expression, but but i have now those complier errors :

src\core\objectfactory.cpp:41: instantiated from here
src\include\objectfactory.h:125: error: no matching function for call to `std::pair<const int, Shape*>::pair(const int&, Shape**(*&)())'

\c++\3.4.2\bits\stl_pair.h:69: note: candidates are: std::pair<const int, Shape*>::pair(const std::pair<const int, Shape*>&)

\c++\3.4.2\bits\stl_pair.h:85: note: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int, _T2 = Shape*]

\c++\3.4.2\bits\stl_pair.h:81: note: std::pair<_T1, _T2>::pair() [with _T1 = const int, _T2 = Shape*]
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Aug 27 '06 #1
4 2208
orel wrote:
I add 'typename' as you said, it now understands the expression, but but i have now those complier errors :
If you have a look in the line where the error occurs, you will find
that you're inserting something into the map that doesn't belong there.

Your newsreader isn't correctly setting the Reference header, can you do
something about it if possible?

Jens
Aug 27 '06 #2

Jens Theisen wrote:
orel wrote:
I add 'typename' as you said, it now understands the expression, but but i have now those complier errors :

If you have a look in the line where the error occurs, you will find
that you're inserting something into the map that doesn't belong there.

Your newsreader isn't correctly setting the Reference header, can you do
something about it if possible?

Jens


OK Jens, sorry for the header.
I saw that I were inserting a ProductCreator instead of a Shape* as a
value in the map. I'm a beginner to design pattern, i understand the
principle of the ObjectFactory, and the use of every template
parameters it needs. But I have a problelm with the ProductFactory, i
don't know what it shoold be. I thought it was a function returning a
pointer to a derived class, so i'm doing like this:

namespace
{
Shape* CreateLine()
{
return new Line;
}
// The ID of class Line
const int LINE = 1;

myFactory.Register(LINE, &CreateLine);
}
Is this the good way to do?

So is the problem coming from the implementation of the ObjezctFactory
i just copy-pasted from the book or (and i think so) i don't kno how to
use it?

Aug 27 '06 #3
Aurelien Rainone wrote:
So is the problem coming from the implementation of the ObjezctFactory
i just copy-pasted from the book or (and i think so) i don't kno how to
use it?
The error is in this line:

return associations_.insert(AssocMap::value_type(id, creator)).second;

creator is of type ProductCreator and the data type of your map is
AbstractProduct - that obviously doesn't fit together.

Presumably your map wants to have ProductCreator as the data type instead.

Jens
Aug 27 '06 #4
Presumably your map wants to have ProductCreator as the data type instead.

Thank you very much to make me open my eyes.... I realized it when i
read you... for a beginer like me, templates are a little bit scary and
i usually don't take the time to decompose each non-trivials portion of
code into various shorter and simpler ones.
Now it's working like i wanted.

For others like me wanting to use a template ObjectFactory, here is the
code of the solution:
/************************************************** *****************************/
/*
/************************************************** *****************************/
file : objfactory.h
*/
#ifndef __OBJFACTORY_H
#define __OBJFACTORY_H
#include <map>

using namespace std;
template
<
class AbstractProduct,
class IdentifierType,
class ProductCreator = AbstractProduct* (*)()
>
class Factory
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.insert( typename AssocMap::value_type(id,
creator)).second;
}
bool Unregister(const IdentifierType& id)
{
return associations_.erase(id) == 1;
}
AbstractProduct* CreateObject(const IdentifierType& id)
{
typename AssocMap::const_iterator i = associations_.find(id);
if (i != associations_.end())
{
return (i->second)();
}
//handle error
}
private:
typedef std::map<IdentifierType, ProductCreator AssocMap;
AssocMap associations_;
};
#endif

/************************************************** *****************************/
and now a little working example. Say I want my factory to a Shape
factory, where i can instance Line, Triangle and others Shape-derived
classes, so

#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>

class Shape
{
public:
virtual void Draw() const {}
virtual void Rotate(double angle){}
virtual void Zoom(double zoomFactor){}
Shape(){cout<<"Shape constructor"<<endl;}

};

class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Line(){cout<<"Line constructor"<<endl;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Triangle(){cout<<"Triangle constructor"<<endl;}
};
#endif // SHAPE_H
/************************************************** *****************************/

Now how i create Shapes with the factory, so simple...

//main.cpp

// i declare a Shape factory where Shape-and-derived types are known as
integers.
Factory<Shape, int>myFactory;
// here i register the Line class
namespace
{
Shape* CreateLine()
{
return new Line;
}
// The ID of class Line
const int LINE = 1;
const bool line_registered = myFactory.Register(LINE, &CreateLine);
}

// here i register the triangle class
namespace
{
Shape* CreateTriangle()
{
return new Triangle;
}
// The ID of class Triangle
const int TRIANGLE = 2;
const bool triangle_registered = myFactory.Register(TRIANGLE,
&CreateTriangle);
}

int main()
{

Line* line= myFactory.CreateObject(LINE);
Triangle* triangle= myFactory.CreateObject(TRIANGLE);

//system("PAUSE"); //only for gcc, to see console output...
return 0;
}

Aug 28 '06 #5

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

Similar topics

5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
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...
6
by: Mike Monagle | last post by:
I'm working on the client side of a distributed application that uses what I call an XML over quasi-HTTP protocol. The client and server are connected with a single persistent socket and exchange...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
3
by: Christer | last post by:
Hi all! We're creating an administration for a web application. This includes severeal aspx pages. Our key entity in the web application is "Profile", which can have a certain type. Based on...
20
by: jernej goricki | last post by:
Hi, Is it possible to do this in vb.net : 1.)To make a function that would have a string parameter ClassName 2.)This function would return an object of the same type as the ClassName parameter...
1
by: orel | last post by:
Please, As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design...
5
by: alexl | last post by:
I have 2 classes, class base { public: some virtual functions } class derived : public base { public:
3
by: suresh_rai | last post by:
Hello I am hoping someone can give me very needed help. I am wanting some C+ + code for 'C++ Abstract Factory'. I need code to be free. I am been learning Design patterns. I found some code on...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.