[ fromfile: factory.xml id: factory ]
The idea of a factory is to separate object creation from object usage.
This section will discuss several design patterns that use factories, and show examples of these design patterns.
When the responsibility for heap object creation is delegated to a virtual
function, we call this a Factory method.
By making the factory method pure virtual
and writing concrete derived factory classes, this becomes an abstract factory.
By imposing creation rules that prevent direct instantiation of a class, we can force clients to use Factory methods to create all instances.
For example, there may be times when it is necessary to restrict the number of objects of a particular class that can exist at one time.
In fact, making use of classes that can be instantiated exactly once is quite common.
We have already seen and worked with a singleton discussed in Section 8.3.
A Qt application that uses an event loop instantiates a QApplication first, creates some other objects, and then calls QApplication::exec()
to start the event loop.
To refer to the object elsewhere in the program, you can make use of the qApp
macro, which returns a pointer to the singleton QApplication.[65]
Section 16.1.3 discusses singletons further.
Object Creation.
Consider three ways to create an instance of the QObject-derived class Customer
:
Customer* c1 = new Customer(name);
QMetaObject meta = Customer::staticMetaObject; Customer* cust = qobject_cast<Customer*>(meta.newInstance());
Customer* cust = CustomerFactory::instance()->newCustomer(name);
In the first case, the class name is hard-coded in the constructor call.
Hard-coded class names in client code can limit the reusability/flexibility of the code.
In the second case, QMetaObject::newInstance()
is called. This is an abstract factory, overridden in each QMetaObject derived class, generated by moc
.
newInstance()
returns a QObject pointer to a new instance of Customer
.
In the third case, a Customer
object is constructed indirectly using a specialized Factory method called CustomerFactory::newCustomer()
.
This interface might be more convenient to use than the Qt library, but it is simply a wrapper around another factory.
Before QMetaObject::newInstance()
,[66] it was necessary to write a switch
statement to handle each supported class in a factory.
[65] QApplication is derived from QCoreApplication. When you #include <QApplication> you are including the macro #define qApp (static_cast<QApplication*>(QCoreApplication::instance()))
QCoreApplication::instance() returns a pointer to the already defined instance or null
, if no instance has been created.
[66] Introduced in Qt 4.5
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |