Object Factory Design Pattern by GoF, need help!!

O

orel

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*, int> ShapeFactory;

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/ =-
 
D

David Harmon

On Sun, 27 Aug 2006 05:37:46 +0200 in comp.lang.c++, "orel"
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top