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.

Factory Pattern

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

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.

Composite Pattern

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…

Join 4,032 other subscribers

Discovering ABAP YouTube Channel