C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Object Factory Design Pattern by GoF, need help!!

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
orel
Guest





PostPosted: Sun Aug 27, 2006 8:37 am    Post subject: Object Factory Design Pattern by GoF, need help!! Reply with quote



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* (*)()
Quote:

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/ =-
Back to top
David Harmon
Guest





PostPosted: Sun Aug 27, 2006 9:10 am    Post subject: Re: Object Factory Design Pattern by GoF, need help!! Reply with quote



On Sun, 27 Aug 2006 05:37:46 +0200 in comp.lang.c++, "orel"
<o_r_l_25 (AT) yahoo (DOT) fr> wrote,
Quote:
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
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group