Similar to previous examples of factories in the case study, the configuration of the PersistenceFacade with a set of IMapper objects can be achieved with a factory object, MapperFactory. However, as a slight twist, it is desirable to not name each mapper with a different operation. For example, this is not desirable:
class MapperFactory { public IMapper getProductSpecificationMapper() {...} public IMapper getSaleMapper() {...} ... }
This does not support Protected Variations with respect to a growing list of mappers—and it will grow. Consequently, the following is preferred:
class MapperFactory { public Map getAllMappers() {...} ... }
where the java.util.Map (probably implemented with a HashMap) keys are the Class objects (the persistent types), and the IMappers are the values.
Then, the facade can initialize its collection of IMappers as follows:
class PersistenceFacade { private java.util.Map mappers = MapperFactory.getInstance().getAllMappers(); ... }
The factory can assign a set of IMappers using a data-driven design. That is, the factory can read system properties to discover which IMapper classes to instantiate. If a language with reflective programming capabilities is used, such as Java, then the instantiation can be based on reading in the class names as strings, and using something like a Class.newInstance operation for instantiation. Thus, the mapper set can be reconfigured without changing the source code.
18.218.201.204