Today we will try to explore the design patterns in the ABAP Objects. We will start with the Singleton design pattern, which is the simplest of its family of design patterns.
What is the concept of the Singleton design pattern?
The concpet of restricting the instantiation of the a class to only and only to one object is called Singleton. As name suggests, it will restrict to create only one instance of a class. The calss will have a logic in place which will deny if the application will ask for more than one instance.
Let's try to understand with an example:
We have an application which will bring the pay stub of an employee for current month. In a standarad system, there will be only one active salary account for the employee with the company. Because of this fact, we should only create one object of the employee's salary account. In program, we are creating this object in a loop. So, what happens if we don't have a design pattern which will restrict it to create more than one object. Application will create, rather overwrite an instance of the class.
In ABAP, generally we check if the instance is created or not, like:
Code Snippet to check instance |
|
But, if we take another reference to the class and create a instance of that, it will definatly allow. So, we need to have Singleton design pattern implemented.
How to implement Singleton design Pattern in ABAP?
We can use the CLASS-DATA(static data) to save the created instance within the class and check with that instance, if application asks for a new instance.
We will look at this example.
UML diagrm for the example:
This will generate the output like this:
Code Snippet for Design Patterns: Singleton |
|
This will generate the output like this:
architectSAP specialize in sap abap development services across the SAP Business Suite. Their team of expert SAP ABAP consultants has prior experience in all core modules of SAP ERP (FI, CO, SD, MM, PP, WM, PM...), SAP BW, SAP SCM (APO), SAP CRM, SAP Retail and SAP SRM to deliver SAP ABAP development services.
This website contains fundamental language elements of ABAP Objects and the usage of the respective ABAP Workbench tools. The tutorial is designed for developers who have had little or no experience with ABAP and ABAP Objects until now.
- ELEMENTARY DATA TYPES - Initial Values, Syntax & Properties.
- FIELD SYMBOLS - Introduction, Syntax & Examples.
- PARAMETERS - Introduction, Syntax & Examples.
- SELECT-OPTIONS - Introduction, Syntax & Examples.
- SELECTION-SCREEN - Introduction, Syntax & Examples.
- SSCRFIELDS - The Screen Fields Table.
EVENTS
- ABAP EVENTS During Runtime Of A Report Program.
- INITIALIZATION Event - Introduction With A Sample Code.
- AT SELECTION-SCREEN Event - Introduction With A Sample Code.
- AT SELECTION-SCREEN OUTPUT Event - Introduction With A Sample Code.