Factory pattern is one of the creational design patterns in object oriented design. This blog explains one of the approaches on how this pattern can be extended and implemented in ABAP Object Oriented Programming (OO).
In factory pattern, different implementations of an interface are instantiated based on a configuration. Now, let us take a case where there are multiple implementations for multiple interfaces. The factory class would now have to choose specific implementation for a specific interface.
You can find an example class diagram in the file "factory_pattern.png".
E type configuration table
Interface_Name | Class_Name |
IF_GENERIC | CL_SPECIFIC |
IF_GENERIC_1 | CL_SPECIFIC_1 |
|
|
C type configuration table
Interface_Name | Class_Name |
IF_GENERIC_1 | ZCL_SPECIFIC_1 |
|
|
Algorithm within the class “CL_CLASS_FACTORY”
- Based on the interface, read customer’s configuration table to get the class name.
- If class name is not maintained in (1), read the configuration from SAP delivered configuration table to get class name from interface name.
For the above example, for interface IF_GENERIC, class CL_GENERIC is picked up from the configuration table whereas for the interface IF_GENERIC_1, class CL_GENERIC_1 is picked up from the configuration.