Instantiating an Apex class based on dynamic string name with Type class

Somtimes we need to instantiate a Apex class by dynamic value in Salesforce, which can make our program more modular and more readable, such as the below case :

1. Each Contact has its socail fields : “Type__c” and “AccessToken__c”
2. Needs to request its profile via socail api in Apex

First we need to create an interface class, e.g. SocialApi, as the following source :
※A parent class with keyword “virtual” is workable as well.

Then create an Apex class implements SocialApi named “FacebookApi”.

Now we can instantiate “FacebookApi” class and call its method with the value of Contact.Type__c as the following source.

Enjoy it!