Abstract Factory Design Patterns Advice
Hello to all, i would like to create a family of object at run time
rather than hard coded in main.
My situation is like this.
ObjectFactory(Abstract) inherits by three classes which are AdminOF,
HumanPersonnelOF and StaffOF.
Moreover, there are another hierarchy which its interface is
concretePosition which inherits by three classes which are Admin,
HumanPersonnel and Staff.
I would like to create the object based on their user name when login
in the following format.
Admin - A0000
HumanPersonnel - H0000
Staff - S0000
Then, they can access their particular function which have option
menu.
Please advise.
Thanks.
EDIT:
If username[0] == 'A' then create Administrator object at run time
else if usernmae[0] = 'H' then create HUMANPersonnel object
else if username[0] == 'S' then create Staff.
I want something like this but i don't want the if statement but i
need them in two class hierarchy with polymorphism.
That's means i receive something in abstract base class and call the
actual type of derived class creator.
Last time, i wrote something similar but using template argument fixed
it at compile time.
Please advise.
Here is my code.
Code:
class objectFactory
{
public:
objectFactory();
void createInstance() = 0;
~objectFactory();
};
class AdministratorFactory : public objectFactory
{
public:
void createInstance();
};
class HRFactory : public objectFactory
{
public:
void createInstance();
};
class StaffFactory : public objectFactory
{
public:
void createInstance();
};
class usernameType
{
private:
login loginSession;
}
I could find any mechanism to determine usernameType and call the
appropriate createInstance().
class login contains a std::string username and password.
There are two approaches in my head right now.
1. Without polymorphism
2. With polymorphism
1. I can just create the object using if statement such as
Code:
if login.username == 'A"
AdministratorFactory instance;
instance.createInstance();
else if login.username == 'H'
HRFactory instance;
instance.createInstance();
else if login.username == 'S'
StaffFactory instance;
instance.createInstance();
I could write this if statement using template too but i would like to
master C++ polymorphism.
2. The difficulties of this approach is to construct the appropriate
type based on the username.
If the login.username is 'A' then create AdminUserType and its member
function will called createInstance() to the appropriate type.
This is my question How to construct usernameType based on
login.username and forward the request to createInstance() method.
I really need your help.
Thanks for your help.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]