473,804 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

<o_******@yahoo .frwrote,
return associations_.i nsert(AssocMap: :value_type(id, creator)).secon d;
>>

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

return associations_.i nsert(
typename AssocMap::value _type(id, creator)
).second;

>>>src\include\ objectfactory.h :125: note: say `typename std::map<Identi fierType,Abstra ctProduct,std:: less<Identifier Type>,std::allo cator<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\object factory.cpp:41: instantiated from here
src\include\obj ectfactory.h:12 5: error: no matching function for call to `std::pair<cons t int, Shape*>::pair(c onst int&, Shape**(*&)())'

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

\c++\3.4.2\bits \stl_pair.h:85: note: std::pair<_T1, _T2>::pair(cons t _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 2232
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.Regis ter(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_.i nsert(AssocMap: :value_type(id, creator)).secon d;

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_.i nsert( typename AssocMap::value _type(id,
creator)).secon d;
}
bool Unregister(cons t IdentifierType& id)
{
return associations_.e rase(id) == 1;
}
AbstractProduct * CreateObject(co nst IdentifierType& id)
{
typename AssocMap::const _iterator i = associations_.f ind(id);
if (i != associations_.e nd())
{
return (i->second)();
}
//handle error
}
private:
typedef std::map<Identi fierType, 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"<<e ndl;}

};

class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Line(){cout<<"L ine constructor"<<e ndl;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Triangle(){cout <<"Triangle constructor"<<e ndl;}
};
#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.Regis ter(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_regist ered = myFactory.Regis ter(TRIANGLE,
&CreateTriangle );
}

int main()
{

Line* line= myFactory.Creat eObject(LINE);
Triangle* triangle= myFactory.Creat eObject(TRIANGL E);

//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
6165
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
2982
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 Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
6
1465
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 messages that look something like this: Content-type: P51Mustang Content-length: 147 <?xml version="1.0"?> <P51Mustang>
11
4312
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. They are still online at http://www.fluffycat.com/java/patterns.html Since September 2003 I've mainly been using PHP, and now that PHP 5 is becoming more available I am going to try the same thing I did in Java with PHP.
10
6109
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 column which references itself (ie. Adjacency or parent/child model) * I have a public property called 'Parent' which returns me a new reference to a Folder instance containing the data of the parent row.
3
1821
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 the profile type, the administration pages will have some small changes, such as displaying a few extra tabs, or maybe change the available choices in a dropdown.
20
3282
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 was. For example, in my project a have this classes : ClassPerson, ClassCar, ClassXXX...........
1
2345
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 pattern. I'm using gcc 3.4.2 on Mingw. The factory is the same as the one in Modern C++ design (without error checking) , like this :
5
1495
by: alexl | last post by:
I have 2 classes, class base { public: some virtual functions } class derived : public base { public:
3
4631
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 web but dont know which is one is ok. http://programmerjoe.com/2007/03/18/the-abstract-factory-pattern-in-c/ http://www.codeproject.com/KB/architecture/DesignPattern.aspx Loki :in text book http://sourceforge.net/projects/papafactory
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.