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

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 FactoryErrorPolicy<IdentifierType, AbstractProduct>
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.insert(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, 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.Register(LINE, &CreateLine);
myFactory.Register(TRIANGLE, &CreateTriangle);
}

Compiler listing is :

src\core\objectfactory.cpp:24: instantiated from here
src\include\objectfactory.h:125: error: dependent-name ` std::map<IdentifierType,AbstractProduct,std::less< IdentifierType>,std::allocator<std::pair<const IdentifierType, AbstractProduct >::value_type' is parsed as a non-type, but instantiation yields a type

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
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Aug 27 '06 #1
1 2316
On Sun, 27 Aug 2006 05:37:46 +0200 in comp.lang.c++, "orel"
<o_******@yahoo.frwrote,
return associations_.insert(AssocMap::value_type(id, creator)).second;
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

Aug 27 '06 #2

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

Similar topics

3
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...
0
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 {...
4
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...
7
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 =...
1
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
by: diwakar09 | last post by:
i am using vc++ #include<iostream> using namespace std; class esteem; class zen; class maruti { char name; public:
3
by: SarojKumarRout | last post by:
Will any body help me to know what is factry methods in C++ & how its implemented?
2
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...
6
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! ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.