dimanche 12 février 2017

What are the ways of creating objects which depends on string value?

I have different types of providers. This providers supplying data from different sources. Also i have a table in the database in which i can find provider to use for specific team.

Team 1 - "FirstProvider"

Team 2 - "SecondProvider" and so on.

Depending on the team i want to use appropriate provider to get specific data for the team.

string providerType = ...
IProvider provider;
if(providerType == "FirstProvider") 
{
    provider = new FirstProvider();
} 
else if(providerType == "SecondProvider")
{
    provider = new FirstProvider;
}

What is the best patter/way of doing this? Maybe some objects factory...

Aucun commentaire:

Enregistrer un commentaire