This post talks about the session “Introduction to ABAP Object Oriented Patterns” by Thomas Jung. The entire session can be viewed at SAP Community Page.
It can also be viewed on youtube at the link – Introduction to ABAP Object Oriented Patterns
Thomas started with a note that Object Oriented ABAP usage is different than other program languages as the lifetime of the objects in ABAP is much shorter. The patterns explained in the session are considering this fact.
( Note: Click on headers to go to that section of the original video by Thomas. This post is only a summary of what I understood out of the session)
Introduction/Refresher to OO ABAP (Static attribute vs Instance attribute)
This is a very basic class having a Static Internal Table and an instance method to add a record to the internal table and second method to return the value of the internal table.
Then the static data concept is explained with a test class that creates an instance, adds a record to the static internal table, clears the class instance, creates a new instance. The earlier data still exists in the static internal table.
The lifetime of the static data is same as lifetime of the class. It was not be used to pass data between different sessions or different user.
If the static attribute is changed to instance attribute, then the data from only that instance would be available.
Static method vs Instance method with constructor
In this example, a static method is explained.
The use of instance methods produces more readable, simplified code when compared to static methods. This is also called as Facade pattern which means a pattern provides a simplified interface.
In instance scenario, a constructor can be used to get the data when the object is created with the importing parameters which can be stored in class instance attributes and the parameters need not be repeated in each method.
In this pattern instead of allowing any class / program to create an object of our class directly using the constructor of our class, we keep the instance creation with the class.
- In class definition, CREATE PRIVATE is used
- A public static method (usually called factory) is added to return the instance of the class
More instance methods would be used for other functionalities.
Because of CREATE PRIVATE, we would get syntax error if we try to create instance outside the class. However, it would be even better if the constructor is put in PRIVATE SECTION rather than PUBLIC SECTION.
Singleton pattern can be used where the factory class should return the same object even if it is called multiple times. The use of singleton pattern is limited in ABAP language as the class/instance lifetime is limited to calling program / class.
It can be used in scenarios like accessing an application log instance throughout the program.
In addition to the factory, we keep the instance of the class as a static-attribute. In the factory method we create the instance only if it is not yet created.
This used factory pattern with below additions
- Abstract base class from which specific types classes would be inherited. An interface can be used here to have common attributes / methods.
- In the constructor, based on some attribute instance of different classes can be created.
- Required methods are redefined in the specific classes
My Take on the session
I always wondered how and where we would use singleton pattern and I have actually never used it in actual project requirement. Thanks for Thomas, I got my answer today.
The session explains basic abap class, instance vs static and patterns like Facade, Factory, Singleton and Composite which such simple examples that even a beginner would understand it. I loved the session and if you have not watched it, I recommend that you do that now.
Check out Devtoberfest Learning Experience 2022 series to find my take on the ABAP sessions.
If you like the content, please subscribe…
Singleton pattern mostly used in Games.
Same object has been used throughout the game life cycle.
For example: GameManager class in game development
In addition to above patterns, I have come across
1. State machines
A class can be divided into
1. Start state
1. Initialisation logic will be there
2. Triggering logic for next state
2. Update state
1. Processing logic
2. Triggering logic for next state
3. Exit state
1. Ends the process
2. De initialises all the variables
Enumerations can be used to define states
Each state should be independent of other.
States can be declared in an abstact class and can be inherited and defined by the state machine class
Ex: In designing UI where back button logic can be handled easily
In Abap, I can think of handling module pool program with multiple screens where transition between previous and next States(screens) will be easy or in UI handling of Fiori Apps.
2. Behavioural pattern
It is like publisher and subscribers type of pattern
Whenever an event happens the publisher will publish an event, ( you can think as button events ) , subscribers will subscribe(register) to that events and perform some activities
Ex: Input system
1. Keyboard inputs
2. Mouse inputs
3. Touch inputs
4. Voice inputs
5. Camera input
Whenever an input triggered by user, by using above means , then there will be a call which registers to that event and executes the functionality
In Abap, I haven’t used, but can be related to oops interactive alv registering to grid events
1. Hotspot event
2. Button events
3. Checkbox events
Add/correct, if I miss/wrong on the above patterns
LikeLike