0% found this document useful (0 votes)
412 views

ABAP Object Design Patterns - Singleton

The document discusses the Singleton design pattern in ABAP. It defines the Singleton pattern as restricting class instantiation to only one object. It provides an example using a CLASS-DATA reference to store the single instance of a class. When get_apps_instance is called, it checks if an instance exists and creates it if not, returning the single instance reference. This allows any code to access the single shared instance rather than creating new instances.

Uploaded by

aloxanh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
412 views

ABAP Object Design Patterns - Singleton

The document discusses the Singleton design pattern in ABAP. It defines the Singleton pattern as restricting class instantiation to only one object. It provides an example using a CLASS-DATA reference to store the single instance of a class. When get_apps_instance is called, it checks if an instance exists and creates it if not, returning the single instance reference. This allows any code to access the single shared instance rather than creating new instances.

Uploaded by

aloxanh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ABAP Object Design Patterns

Singleton
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.

Page Contents

1 What is the Singleton design pattern?


2 Code Lines
3 How to implement Singleton design Pattern in ABAP?
4 Update Singleton Usage
5 Check out all Design Patterns

What is 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 class will have a logic in place which will deny if the application will ask for more than
one instance.

Code Lines
Lets 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 employees salary
account. In program, we are creating this object in a loop. So, what happens if we dont
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

*&--------------------------------------------------------------------*
DATA: lo_application TYPE REF TO lcl_application.
IF lo_application IS BOUND.
CREATE OBJECT lo_application.
ENDIF.

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:

Code Snippet:

*&--------------------------------------------------------------------*
*& Report shows how to use the static data of the class to
*&
implement the design patterns.
*&--------------------------------------------------------------------*
REPORT ztest_singleton_pattern.
*
*---------------------------------------------------------------------*
*
CLASS lcl_application DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_application DEFINITION CREATE PRIVATE.
*
PUBLIC SECTION.
*
Static Method which will return us the object reference
CLASS-METHODS:
get_apps_instance
RETURNING

value(ro_apps) TYPE REF TO lcl_application.


METHODS:

set_v_name

IMPORTING

*
*
*

iv_name TYPE char30,


get_v_name
RETURNING
value(rv_name) TYPE char30.

PRIVATE SECTION.
static class reference to hold the existing object reference
CLASS-DATA: lo_apps TYPE REF TO lcl_application.

DATA: v_name TYPE char30.


*
ENDCLASS.
"lcl_application DEFINITION
*
*
*---------------------------------------------------------------------*
*
CLASS lcl_application IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.
*
* This method will return the object reference to the calling applicat
ion
METHOD get_apps_instance.
IF lo_apps IS INITIAL.
*
creation of the object
CREATE OBJECT lo_apps.
ENDIF.
*
assigning reference back to exporting parameter
ro_apps = lo_apps.
ENDMETHOD.
"get_apps_instance
*
METHOD set_v_name.
me->v_name = iv_name.
ENDMETHOD.
"set_v_name
*
METHOD get_v_name.
rv_name = me->v_name.
ENDMETHOD.
"get_v_name
*
ENDCLASS.
"lcl_application IMPLEMENTATION
*
*
START-OF-SELECTION.
*
*.Reference: 1 .........................................
DATA: lo_application TYPE REF TO lcl_application.

DATA: lv_result TYPE char30.

WRITE: / 'LO_APPLICATION: '.


calling the method which gets us the instance
Statement CREATE OBJECT LO_APPLICATION
would not work as the class LCL_APPLICATION instantiation
is set to PRIVATE
lo_application = lcl_application=>get_apps_instance( ).
* Set the variable and get it back.
lo_application->set_v_name( 'This is first Object' ).
lv_result = lo_application->get_v_name( ).
WRITE: / lv_result.
CLEAR lv_result.
*
*.Reference: 2............................................
DATA: lo_2nd_apps TYPE REF TO lcl_application.
SKIP 2.
WRITE: / 'LO_2ND_APPS : '.
*
calling the method which gets us the instance
* By calling GET_APPS_INSTANCE method again to get the singleton
* object, it would give the same object back and assign it to
* LO_2ND_APPS object reference. This would be varified by
* getting the value of the instance attribute V_NAME
lo_2nd_apps = lcl_application=>get_apps_instance( ).
lv_result = lo_2nd_apps->get_v_name( ).
WRITE: / lv_result.
CLEAR lv_result.
This will generate the output like this:
*
*
*
*

Update Singleton Usage


Recently, I have posted another article on Singleton ABAP Objects Design Patterns
Singleton Usage. This article demonstrates step by step Singleton implementation along
with UML sequence diagram.
SDN Wiki Singleton design pattern

Check out all Design Patterns


You may also want to explore all other Design Patterns in OO ABAP.

ABAP Object Design Patterns Singleton

ABAP Objects Design Patterns Model View Controller (MVC) Part 1

ABAP Objects Design Patterns Model View Controller (MVC) Part 2

ABAP Objects Design Patterns Model View Controller (MVC) Part 3

ABAP Objects Design Patterns Decorator

ABAP Objects Design Patterns Factory Method

ABAP Objects Design Patterns Observer

Case Study: Observer Design Pattern Usage

ABAP Objects Design Patterns Abstract Factory

OO Design Pattern Decorator Why do we need to use helper variable?

ABAP Objects Design Patterns Facade

ABAP Objects Design Patterns Adapter

ABAP Objects Design Patterns Composite

ABAP Objects Design Patterns Iterator

Iterator Design Pattern to access Linked List in ABAP Objects

ABAP Objects Design Patterns Proxy

ABAP Objects Design Patterns Prototype

ABAP Objects Design Patterns Singleton Factory

ABAP Object Oriented Approach for Reports Initial Design

ABAP Object Oriented Approach for Reports Redesign

ABAP Objects Design Patterns Builder

ABAP Objects Design Patterns Singleton Usage

ABAP MVC Model View Controller Discussion

You might also like