16.1.2.  Abstract Factories and Libraries

[ fromfile: factory.xml id: componentfactory ]

Figure 16.1. Libraries and factories

Libraries and factories

Example 16.5. src/libs/customer/customerfactory.h

[ . . . . ]
class CUSTOMER_EXPORT CustomerFactory : 
                             public QObject, public ObjectFactory {
  public:
    static CustomerFactory* instance();                      1
    Customer* newCustomer(QString name, QObject* parent=0);  2
    Address* newAddress(QString countryType = "USA", QObject* parent=0);
  private:
    CustomerFactory(QObject* parent=0);
};
[ . . . . ]

1

Singleton factory method.

2

Regular factory method, does not require a typecast.


Example 16.6. src/libs/customer/customerfactory.cpp

[ . . . . ]


CustomerFactory::CustomerFactory(QObject* parent) : QObject(parent) {
    m_knownClasses["Customer"] = Customer::staticMetaObject;
    m_knownClasses["CustomerList"] = Customer::staticMetaObject;
}