Item 30. Factory Method

A high-level design often requires the creation of an object of the “appropriate” type, based on the type of an existing object. For example, we may have a pointer or reference to an Employee object of some kind, and we need to generate the appropriate kind of HRInfo object for that type of Employee, as shown in Figure 8.

Figure 8. Pseudoparallel hierarchies. How should we map an employee to its corresponding human resources information?

image

Here we have almost parallel Employee and HRInfo hierarchies. Salary and Hourly employees require the generation of an StdInfo object whereas a Temp requires a TempInfo object.

The high-level ...

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.