|OOP| Learn “Factory Pattern” from Android Telephony

Factory pattern is a basic pattern in grant software framework.
We create object, parent, with common interface to create new type of object.
The common interface is a static member-function (static factory method) which creates & returns instances, hiding the details of children.

The following will show a snappiest code of Android Q.

In the class “PhoneFactory”, the interfaces of telephone starts from here, makeDefaultPhone which creates phones and interfaces.
Then refer to createPhone …

createPhone will create real phone according to their type.
Notice this method, it returns “Phone”.


private static Phone createPhone(Context context, int phoneId) {
    int phoneType = TelephonyManager.getPhoneType(RILConstants.PREFERRED_NETWORK_MODE);
    Rlog.i(LOG_TAG, "Creating Phone with type = " + phoneType + " phoneId = " + phoneId);
    // We always use PHONE_TYPE_CDMA_LTE now.
    if (phoneType == PHONE_TYPE_CDMA) phoneType = PHONE_TYPE_CDMA_LTE;
    TelephonyComponentFactory injectedComponentFactory =
    return injectedComponentFactory.makePhone(context,
            sCommandsInterfaces[phoneId], sPhoneNotifier, phoneId, phoneType,

Look at “makePhone”, return “GsmCdmaPhone” !!


public Phone makePhone(Context context, CommandsInterface ci, PhoneNotifier notifier,
        int phoneId, int precisePhoneType,
        TelephonyComponentFactory telephonyComponentFactory) {
    return new GsmCdmaPhone(context, ci, notifier, phoneId, precisePhoneType,


Another example,

#include <iostream> 
using namespace std; 
enum PhoneType { 
    Gsm,    Cdma,    Ims 

// Library classes 
class Phone { 
    virtual void printType() = 0; 
    static Phone* Create(PhoneType type); 
class GsmPhone : public Phone { 
    void printType() { 
        cout << "I am GsmPhone" << endl; 
class CdmaPhone : public Phone { 
    void printType() { 
        cout << "I am CdmaPhone" << endl; 
class ImsPhone : public Phone { 
    void printType() { 
        cout << "I am ImsPhone" << endl; 

// !!This is the key!!
// Factory method to create objects of different types. 
// Change is required only in this function to create a new object type 
Phone* Phone::Create(PhoneType type) { 
    if (type == Gsm) 
        return new GsmPhone(); 
    else if (type == Cdma) 
        return new CdmaPhone(); 
    else if (type == Ims) 
        return new ImsPhone(); 
    else return NULL; 

// PhoneProxy class 
class PhoneProxy { 
    // PhoneProxy doesn't explicitly create objects 
    // but passes type to factory method "Create()" 
    PhoneProxy(PhoneType pType) { 
        PhoneType type = pType; 
        pPhone = Phone::Create(type); 
    ~PhoneProxy() { 
        if (pPhone) { 
            delete[] pPhone; 
            pPhone = NULL; 
    Phone* getPhone()  { 
        return pPhone; 
    Phone *pPhone; 

// Driver program 
int main() { 
    PhoneProxy *pPhoneProxy = new PhoneProxy(Ims); 
    Phone * pPhone = pPhoneProxy->getPhone(); 
    pPhone->printType(); //I am ImsPhone
    return 0;