public class UserFactory
{
IDictionary<UserType, User> userTemplatesByUserType = new Dictionary<UserType, User>();
public int TotalObjectsCreated
{
get { return userTemplatesByUserType.Count; }
}
public User GetUserFromFactory(UserType userType)
{
User userTemplate = null;
if (userTemplatesByUserType.ContainsKey(userType))
{
userTemplate = userTemplatesByUserType[userType];
}
else
{
switch (userType)
{
case UserType.User:
userTemplate = new User();
userTemplatesByUserType.Add(UserType.User, userTemplate);
break;
case UserType.Administrator:
userTemplate = new Administrator();
userTemplatesByUserType.Add(UserType.Administrator, userTemplate);
break;
case UserType.Guest:
userTemplate = new Guest();
userTemplatesByUserType.Add(UserType.Guest, userTemplate);
break;
default:
throw new Exception($"Unknown user type {userType}.");
}
}
return userTemplate;
}
}
|