Re: What to do after the creation of an object with a factory?
Kevin Niu wrote:
Hi,
I am doing a project and in this project we have something similar to an
example in the "Modern C++ Design", which is the one in the Factory
Pattern
chapter.
After we saved the objects drived from shape (points, polylines ect) in to
a
xml file, we can read them back using the factory pattern. My question is
:
After an object has been created by a factory, How To Set Its Status?
Say if a SpacialShape has a dozen data members, what is the proper method
to
set them.
For example,
calss SpacialShape : public class Shape
{
......
private:
string a,b,c,d,e,f,g,h,i,j,k,l,m,n;
};
After I created an object of type SpacialShape, I need to set all the data
members( a to n).
I can read from xml file a vector of pair<string, string> of name and
value,
in my programm
I have to use some thing like
if(p->first == "a")
obj.a = p->second;
else if(p->first == "b")
obj.b = p->second;
.....
As you can see, this is ugly and when I add new data members I have to add
more if else.
Can we do better than this?
Well, it doesn't look very manageable to have 15 similar attributes
in a class.
In case that they are not frequently used, you can write instead:
// in the class definition
std::map<std::string,std::string> data;
...
obj.data[p->first] = p->second;
Michael
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]