Abstract Factory Pattern Advice

Started by
16 comments, last by Peter_APIIT 14 years, 10 months ago
Well, why would a collection not be suitable?
Basically, you'd initialize the collection with the appropriate factory for each string, i.e.
map.put('A', <factoryA>);map.put('B', <factoryB>);...

Then you'd just retrieve the factory by map.get(username[0]).

One advantage of that approach would be that you're able to add more factories at runtime.

However, like Ravyne said, the client should not know about the concrete factories.
Instead it might know about a meta factory (or factory method), i.e. a factory that knows how to parse the user name and calls the appropriate concrete factory.

Then you would get something like this (heavily simplified):

//in your login codeUser user = metaFactory.create(username);...class MetaFactory{  private:    map<char, conreteFactory> factories;  public:    User create(string username, /*more construction args*/)     {      return factories.get(username[0]).create(/*construction args, e.g. username*/);    }}

If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Advertisement
Quote:Original post by Peter_APIIT
How abstract factory hides construction details ?


Say X is the information needed to decide which class to instantiate.

Instead of interpreting X yourself, you pass X to the abstract factory, and the abstract factory interprets X. The construction details are "hidden" in that they are encapsulated in the factory.

Quote:
I get suggestion in google usenet group.

   void createUser(const std::string& name)    {     // this method shoud return something different than "void",but I'm not sure what the     // objectFactory::createInstance method should return



It should return a shared_ptr to whatever is the base class of the things created by the factory.

Quote:I don't know whether this is what you mean.


This is one common approach, yes.


Quote:
Why create a lot of classes will end up with prototypes ?


He said, if you have a lot of codes ("Admin", "Staff" etc.), then you can use prototypes to avoid writing out all the if-statements. That's basically what the other code you suggested does: each UserFactory does the work of a prototype.

With the actual prototype approach, you give the base User class a virtual 'clone' function, which makes a shared_ptr to a new copy of the object. That way, you put Users into your map instead of UserFactories, and call clone() instead of createInstance().
Quote:
He said, if you have a lot of codes ("Admin", "Staff" etc.), then you can use prototypes to avoid writing out all the if-statements. That's basically what the other code you suggested does: each UserFactory does the work of a prototype.

With the actual prototype approach, you give the base User class a virtual 'clone' function, which makes a shared_ptr to a new copy of the object. That way, you put Users into your map instead of UserFactories, and call clone() instead of createInstance().


I probably understand the statement. Since there are many factory that have same state. Hence, we can use prototype to clone it.

Am i correct ?
Quote:
My solution is like this. Basically, there are two hierarchy which are ObjecFactory which have three derived classes which are AdminFactory, HRFactory and StaffFactory. Another class hierarchy is wraps login session inside usernameType which have three derived classes also same as above.

I was thinking the difficulties in creating concrete usernameType then forward to concrete objectFactory to create the object. Now, i have solution which is using factory method in usernameType hierarchy. Is this possible ?

Thanks.

Let concentrated back to my problem where i have a solution but i need expert advice.
Please comment the above statement. It's really urgent.

I bag your pardon if seems rude.
Sorry Peter, I don't understand which question you need the answer to.
This is my question.

My solution is like this. Basically, there are two hierarchy which are ObjecFactory which have three derived classes which are AdminFactory, HRFactory and StaffFactory. Another class hierarchy is wraps login session inside usernameType which have three derived classes also same as above.

I was thinking the difficulties in creating concrete usernameType then forward to concrete objectFactory to create the object. Now, i have solution which is using factory method in usernameType hierarchy. Is this possible ?

My class hierarchy is like this.

   objectFactory                  |AdminFactory  HRFactory StaffFactory         usernameType  (Contain reference to login)                  |AdminType  HRType  Stafftype  (Forward call to concrete factory)class login;             Human                |Admin      Hr     Staff


Thanks.
What do AdminFactory, HRFactory and StaffFactory do when creating the object? ie do they just call the constructor for the object(or clone method on an instance) or do something more substantial?
Using the code from the page I linked to
typedef Human User_type;typedef Abstract_factory<User_type,std::string> User_factory;int main(){  User_factory factory;  factory.register_key<Admin>("admin");  factory.register_key<Hr>("hr");  factory.register_key<Staff>("staff");  //get key a login type  std::string key;  ...  User_type* user( factory.create(key).release());}
Quote:What do AdminFactory, HRFactory and StaffFactory do when creating the object? ie do they just call the constructor for the object



It just called the constructor to dynamic allocate memory.

Actually, i have coded the class hierarchy but i want to learn your approach too.

What approach the code you using ?

Thanks.

This topic is closed to new replies.

Advertisement