Part.1/4) OO Capitalization: How to create dynamically a method into a class (SAP 740 mini.)
This topic is the first one of a serie of articles coming soon, concerning a global approach of abap oriented-object in order to capitalize your development in a long term.
To introduce basically, often when we code a custom OO framework, this framework is only used for the current project. What do you think, if your framework can be more global, can grow up without any direct action by you and finally, can be usable for several projects and several clients?
That is why, it could be relevant to know that the creation of some methods dynamically is possible, in order to give more flexibility, more factorization, better evolution and artificial intelligence design of your framework. Let’s it begin…
After some investigations resulting in successful tests and obsolete codes found, I would like to share with you regarding my point of view, the best way to improve dynamically a class during the runtime, This snippet is based on the framework used by SAP for the version 740, including security and a minimum of lines of code.
Well, for this example, I will create a CLASS “ZCL_TEST1” that will create a method into the class “ZCL_TEST2” dynamically. The process is below:
1) Call a MF to create a method into the target class
2) Call a MF to include the implementation of the fresh method
3) Call a MF to regenerate the sections of the target class
4) Call the dynamic method of the target class
Here we go… 🙂
1) In SE24, create the class “ZCL_TEST2“. This class will see their own methods created through the “ZCL_TEST1“.
2) In SE24, create the class “ZCL_TEST1” and create a CONSTRUCTOR to simplify our example. This class will create method into the “ZCL_TEST2” class during the instantiation.
3) In the CONSTRUCTOR, you have to call 3 MF (complete source code in attachment) succinctly:
3.1) Variables initialization
3.2) Call the right MF to create a method in the target class
3.2) Init & Call the right MF to create an implementation into the target class
3.3) Call the right MF to regenerate the sections of the target class (Private,Protected & Public Section)
(source code in attachment)
At this time, your class ZCL_TEST1 is able to interact onto the class ZCL_TEST2.
Finally, execute many times ZCL_TEST1 class locally with F8 and check out the result in ZCL_TEST2.
That is done!
For the next topic, I will enjoy to share with you in which context this kind of development can be useful and how is it exciting to enhance your custom framework including several design patterns, in order to improve dynamically your architectural layers during the runtime.
The final goal will be to decrease your lines code, increase the reusability, be able to call generic methods that do not exist yet during your writing code and at the same time, thinking about how can you grow up your custom framework by adding these generic methods freshly created throughout your miscellanous projects.