One of the great benefits of using factories is the ability to substitute one implementation of a factory for another. In this way, you can substitute families of objects within an application. For example, imagine an application that had to adapt to many different database implementations. In our example, let's assume that the users can either use flat files or purchase an Oracle adapter. We might use the PROXY pattern to isolate the application from the database implementation.[2] We might also use factories to instantiate the proxies. Figure 29-3 shows the structure.
Figure 29-3. Substitutable factory
Note the two implementations of EmployeeFactory. One creates proxies that work with flat files, and the other creates proxies that work with Oracle. Note also that the application does not know or care which implementation is being used. |