473,791 Members | 3,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Factory Design Pattern by GoF, need help!!

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 :


template
<
class AbstractProduct ,
typename IdentifierType,
typename ProductCreator = AbstractProduct * (*)()
>
class Factory
// : public FactoryErrorPol icy<IdentifierT ype, AbstractProduct >
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.i nsert(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, AbstractProduct AssocMap;
AssocMap associations_;
};


and for the example i want to compile i have a simple class hierarchy like that:
class Shape
{
public:
virtual void Draw() const;
virtual void Rotate(double angle);
virtual void Zoom(double zoomFactor);
Shape* operator()() {return new Shape;} // i thought this was necessary but...

};

class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};


So what i'm doing is :

const int LINE = 1;
const int TRIANGLE = 2;

typedef Factory<Shape*, intShapeFactory ;

Line* CreateLine()
{
return new Line;
}
Triangle* CreateTriangle( )
{
return new Triangle;
}
void TEST_FUNCTION ()
{
ShapeFactory myFactory;

myFactory.Regis ter(LINE, &CreateLine) ;
myFactory.Regis ter(TRIANGLE, &CreateTriangle );
}

Compiler listing is :

src\core\object factory.cpp:24: instantiated from here
src\include\obj ectfactory.h:12 5: error: dependent-name ` std::map<Identi fierType,Abstra ctProduct,std:: less<Identifier Type>,std::allo cator<std::pair <const IdentifierType, AbstractProduct >::value_type ' is parsed as a non-type, but instantiation yields a type

src\include\obj ectfactory.h:12 5: 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
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Aug 27 '06 #1
1 2344
On Sun, 27 Aug 2006 05:37:46 +0200 in comp.lang.c++, "orel"
<o_******@yahoo .frwrote,
return associations_.i nsert(AssocMap: :value_type(id, creator)).secon d;
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\ob jectfactory.h:1 25: 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

Aug 27 '06 #2

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

Similar topics

3
3151
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
0
1546
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms { protected: typedef std::deque<double> DDEQUE; // need to make this even more generic to support floats .. i.e float and double public:
4
2232
by: orel | last post by:
<o_r_l_25@yahoo.frwrote, As the message says, to help the compiler with template parsing, 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:...
7
4339
by: sunny | last post by:
Hai, can anyone implemen this problem.I am unable to implement.i tried but i am going out of the design pattern. Implement the Factory Design Pattern for the following scenario: Base class = Quad Derived classes = Square, Rectangle Take command line arguments for length & width. Depending upon no. of arguments,
1
1365
by: sunny | last post by:
Hai, can anyone provide me with documents related to factory desing pattern and Abstract factory pattern.where i can gain good understanding on the thesee design patterns. Regards, Sunny
1
1249
by: diwakar09 | last post by:
i am using vc++ #include<iostream> using namespace std; class esteem; class zen; class maruti { char name; public:
3
1464
by: SarojKumarRout | last post by:
Will any body help me to know what is factry methods in C++ & how its implemented?
2
2567
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html. While I've read about implementing Data Access Objects by Factory Method Pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/images09/figure09_07.gif) and Abstract Factory Pattern...
6
1610
by: csharpula csharp | last post by:
Hello, I am building multi platform application and I would like to ask are there any links which would provide me code with factory design pattern implementaion in c# for this? Thanks! *** Sent via Developersdex http://www.developersdex.com ***
0
9517
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
10428
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
9997
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...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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.